0

So I have an object like this:

{
  "apples": [
    "one",
    "two"
  ],
  "oranges": [
    "three",
    "four"
  ]
}

How do I look through this object and find four for example? Something like:

for (var i=0; i < obj.length; i++) {
  for (var y=0; y <obj.childObj.length; y++ {
    obj.childObj[i] === 'four' ? return : null;
  }
}

Or is there a better way to structure this data?

j_d
  • 2,818
  • 9
  • 50
  • 91

5 Answers5

2
for(var x in obj)
 if(obj.hasOwnProperty(x)) {
  for(var y in obj[x])
   if(obj[x].hasOwnProperty(y)) {
    obj[x][y] === 'four' ? doSomething() : doSomethingElse();
   }
  }

EDIT : Improvement as suggested by Matthew Herbst

Aditya
  • 80
  • 6
  • Looks good initially. Is this the most efficient way to store and retrieve multi-dimensional information like this though? – j_d Apr 13 '16 at 09:51
  • 2
    Make sure to also use `obj.hasOwnProperty(x)` and `obj[x].hasOwnProperty(y)` – Matthew Herbst Apr 13 '16 at 09:51
  • I'm not aware of any other way which could give you a significant performance boost over this one. – Aditya Apr 13 '16 at 09:53
  • What is the benefit of hasOwnProperty? – j_d Apr 13 '16 at 09:57
  • @JohnDoe It'll check only for the own properties and not for the inherited properties. See [hasOwnProperty](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Object/hasOwnProperty) Also see [this SO post](http://stackoverflow.com/questions/2600085/hasownproperty-in-javascript) – Tushar Apr 13 '16 at 09:58
  • The check prevents inherit properties from being iterated over, If there are any. If you have an object that inherits too many properties and you don't want them being iterated over, Object.keys could be a better alternative. – Aditya Apr 13 '16 at 10:03
0

You can use for (x in y) statemment:

var data = {
  "apples": [
    "one",
    "two"
  ],
  "oranges": [
    "three",
    "four"
  ]
};

for (var key in data) {
    var obj = data[key];
    for (var i=0; i <obj.length; i++) {
        // obj[i] === 'four' ? return : null;
        console.log(obj[i]);
    }
}

This will print:

one
two
three
four
martin
  • 93,354
  • 25
  • 191
  • 226
0

If you want to find out if this object has four somewhere then

var isFourAvailable = Object.keys(obj).filter(function(val){ return obj[val].indexOf("four") != -1 }).length > 0;

Making it more generic

function findX(x)
{
   return Object.keys(obj).filter(function(val){ return obj[val].indexOf(x) != -1 }).length > 0;
}
gurvinder372
  • 66,980
  • 10
  • 72
  • 94
0

Try this ;)

Modified your code;

for (var i = 0; i < obj.length; i++) {
  for (var y = 0; y < obj[i].length; y++ {
    if(obj[i][y] === 'four'){
      console.log("It's four");
    }
  }
}
itzmukeshy7
  • 2,669
  • 1
  • 21
  • 29
0

You can use indexOf

   var x={
      "apples": [
        "one",
        "two"
      ],
      "oranges": [
        "three",
        "four"
      ]
    }
    for (var e in x) {
      if (x[e].indexOf("four") > -1)
        console.log("found Four");
    }
Rohit Agre
  • 1,528
  • 1
  • 14
  • 25