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?

5
  • 2
    Your code is wrong. return cannot be used like that. what is your requirement. Commented Apr 13, 2016 at 9:48
  • what exactly do you want to return? Commented Apr 13, 2016 at 9:49
  • it depends on the purpose. if you always search for four, then an object with a key four would make sense. Commented Apr 13, 2016 at 9:49
  • Don't use same variable in nested loops for iteration in your case i; Commented Apr 13, 2016 at 9:51
  • Structure as you want (if you are looking for values that are not unique and good enough to be used as hashmap keys), you are going through all the elements one by one. Commented Apr 13, 2016 at 9:53

5 Answers 5

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

Sign up to request clarification or add additional context in comments.

6 Comments

Looks good initially. Is this the most efficient way to store and retrieve multi-dimensional information like this though?
Make sure to also use obj.hasOwnProperty(x) and obj[x].hasOwnProperty(y)
I'm not aware of any other way which could give you a significant performance boost over this one.
What is the benefit of hasOwnProperty?
@JohnDoe It'll check only for the own properties and not for the inherited properties. See hasOwnProperty Also see this SO post
|
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

Comments

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;
}

Comments

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");
    }
  }
}

Comments

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");
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.