0

I have an array defined like so:

var numeric =  [{ Value: "0" }, { Value: "1" }, { Value: "2" }, { Value: "3" }];

I'm trying to determine if a particular value exists in this array. I've tried all of the following lines which all return -1.

numeric.indexOf(1);
numeric.indexOf("1");
numeric.indexOf({Value: "1"});

Assume I have no control over how the array is defined. How can I determine if a value exists in this particular kind of array?

tnw
  • 13,521
  • 15
  • 70
  • 111
  • 3
    Write your own function that loops through the array and checks if the `Value` property of your objects equals the parameter you supply. `indexOf` only checks if the array contains the value, it's not "complex" or smart – meder omuraliev Oct 15 '14 at 18:36
  • 2
    Keep in mind that `new Object === new Object` returns `false`, objects are equal only if the reference is the same. – Etheryte Oct 15 '14 at 18:37
  • @meder Yeah, I'm just trying to avoid adding a little helper function to keep my code as clean as possible. If it isn't no problem (this isn't homework with arbitrary requirements) – tnw Oct 15 '14 at 18:38
  • http://debugmode.net/2013/02/19/how-to-find-index-of-an-item-in-javascript-object-array/ – j08691 Oct 15 '14 at 18:39
  • As the dupe indicates, there a several ways to do this. You can use a simple `for` loop to compare items in the list to your target or you could use something like `.filter`. Sadly [Array.find](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/find) is not widely supported yet. – Matt Burland Oct 15 '14 at 18:47
  • 1
    @tnw: The whole point of helper functions is to give you clean code. –  Oct 15 '14 at 18:47
  • @squint Agreed, but I only use this once and was hoping to use a clever one liner (though one liners can get messy too I suppose!) – tnw Oct 15 '14 at 18:48
  • @tnw If you are looking for a oneliner have a look at my answer. – Martin Ernst Oct 15 '14 at 19:06
  • If you want clean code, use `.some()`, like `numeric.some(function(obj) { return obj.Value === "1" });` This will break the loop as soon as a match is found. –  Oct 15 '14 at 19:16

3 Answers3

2

You can loop throught object with a loop:

var numeric = [{
    Value: "0"
}, {
    Value: "1"
}, {
    Value: "2"
}, {
    Value: "3"
}];

for (var key in numeric) {
    var value = numeric[key];
    if (value.Value == "1") {
        console.log("ok");
    }
}

After @MattBurland comment you can use regular for too:

var numeric = [{
    Value: "0"
}, {
    Value: "1"
}, {
    Value: "2"
}, {
    Value: "3"
}];

for (var i = 0; i < numeric.length; i++) {
    var value = numeric[i];
    if (value.Value == "1") {
        console.log("ok");
    }
}
Alex Char
  • 32,879
  • 9
  • 49
  • 70
  • 2
    Don't use `for...in` to iterate arrays. Use a regular `for` loop. – Matt Burland Oct 15 '14 at 18:40
  • @MattBurland Why? Curious – tnw Oct 15 '14 at 18:40
  • 1
    In this case I would have gone for a `while` instead of a `for` altogether – Alvaro Montoro Oct 15 '14 at 18:42
  • 2
    @tnw: A couple of reason. First, see [MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for...in). It should never be used where the order is important. Second, it will iterate over properties that you might not expect. Like ones inherited from prototype unless you check `hasOwnProperty` – Matt Burland Oct 15 '14 at 18:43
  • @AlvaroMontoro: Or you should at least `break` from the `for` loop after finding the matching item. – Matt Burland Oct 15 '14 at 18:44
  • Take a look here [why-is-using-for-in-with-array-iteration-such-a-bad-idea](http://stackoverflow.com/questions/500504/why-is-using-for-in-with-array-iteration-such-a-bad-idea) – Alex Char Oct 15 '14 at 18:46
  • True, or a `for` with a `break` when the item is found. But (and this maybe just me being picky) I consider the general use of `break` like a "bad practice" – Alvaro Montoro Oct 15 '14 at 18:48
2

You would have to iterate through the array and check for the property.

var numeric =  [{ Value: "0" }, { Value: "1" }, { Value: "2" }, { Value: "3" }];

var index=-1;
for(var i = 0; i<numeric.length; i++)
    if(numeric[i].Value === "2") { 
        index = i; 
        break; 
    }
console.log(index);
Peter Rasmussen
  • 16,474
  • 7
  • 46
  • 63
  • Accepting as the best answer per discussion below (use of `break` and normal `for` loop) – tnw Oct 15 '14 at 18:50
2

Since numeric is an array you can use .findIndex():

var search = 1;
var found = numeric.findIndex(function(n) {return n.value == search});

found will be index of item with value == 1, if thats not found it will be -1. Reference here.

If you need a boolean result it's better to use .some():

var found = numeric.some(function(n) {return n.value == search;});

Reference here. Note that both functions are not supported by older browsers.

Martin Ernst
  • 3,199
  • 1
  • 12
  • 12
  • You should let people know that this is from the forthcoming standard, which is not finalized yet and will need to be shimmed in most browsers. Either way, why not use ES5's `.some()` instead? It would make more sense since a boolean result is wanted anyway. –  Oct 15 '14 at 19:18
  • @sqint Thanks, I didn't noticed that OP want's a boolean result. I will update my answer. – Martin Ernst Oct 15 '14 at 19:31