-1

I have a set of object of object. For example

{
   "123":{
      id:123, 
      name:"abc"
   }, 
   "456":{
      id:456, 
      name:"def"
   }, 
   "789":{
      id:789, 
      name:"ghi"
   }
}

I would like to know how to loop over my object and check if the value "def" exist in the object list? Can I know how to loop through every iteration and only do decision ?? For example first iteration is abc then next is def then next is ghi . because abc and def is not same but when come to def and def it is same .Can I do action or logic after finish loop through every iteration ?

  • Does this answer your question? [How do I loop through or enumerate a JavaScript object?](https://stackoverflow.com/questions/684672/how-do-i-loop-through-or-enumerate-a-javascript-object) – Crocsx Nov 30 '19 at 05:16
  • Please read [ask]. And create a [mcve] with your best try. – Roko C. Buljan Nov 30 '19 at 05:27

2 Answers2

0

Use a for loop on the object to check that value exist in name property or not:

var obj = {
  123: {
    id: 123,
    name: 'abc'
  },
  456: {
    id: 456,
    name: 'def'
  },
  789: {
    id: 789,
    name: 'ghi'
  }
};

var checkVal = 'def';

let match = false;
for(var objKey in obj) {
  if(obj[objKey].name === checkVal) {
    match = true;
  }
}
console.log('found ', match);
Ankit Agarwal
  • 30,378
  • 5
  • 37
  • 62
  • Can I know how to loop through every iteration and only do decision ?? For example first iteration is abc then next is def then next is ghi . because abc and def is not same but when come to def and def it is same .Can I do action or logic after finish loop through every iteration ? – You Unknown Nov 30 '19 at 05:56
  • you can simply do whatever you want with the current object inside the `if` statement. – Jhecht Nov 30 '19 at 05:59
0

There are a couple of ways to loop through an object in Javascript, depending on what version you are using.

The basics of it Ankit talked about in his answer:

const search = {
   "123":{
      id:123, 
      name:"abc"
   }, 
   "456":{
      id:456, 
      name:"def"
   }, 
   "789":{
      id:789, 
      name:"ghi"
   }
};

for(let key in search) {
  if(search[key].name == 'def') {
    console.log(search[key]);
  }
}

If you are using a newer version of Javascript, you can do the following:

const search = {
   "123":{
      id:123, 
      name:"abc"
   }, 
   "456":{
      id:456, 
      name:"def"
   }, 
   "789":{
      id:789, 
      name:"ghi"
   }
}

for(let obj of Object.values(search)){
  if(obj.name==='def') console.log(obj);
}

// or, very similarly

for(let key of Object.keys(search)) {
  let obj = search[key];
  if(obj.name=='def') console.log(obj);
}

Or, lastly.

const search = {
   "123":{
      id:123, 
      name:"abc"
   }, 
   "456":{
      id:456, 
      name:"def"
   }, 
   "789":{
      id:789, 
      name:"ghi"
   }
}

for(let [key, obj] of Object.entries(search)){
  if(obj.name=='def') console.log('found on key ', key, 'value:', obj);
}

Note that the last one uses the destructuring assignment so that you do not need to use a placeholder variable simply to assign key and object/value out inside the loop.

Jhecht
  • 4,407
  • 1
  • 26
  • 44