1

I have array of objects with about 500 records. I would like to loop trough the array of objects and check if object name is equal to one of the elements. Here is example of my array of objects:

data = [
      {
        "row": 23,
        "code": "ERT"
      },
      {
        "row": 45,
        "code": "TTR"
      },
      {
        "row": 90,
        "code": "CXZ"
      }
    ];

Here is what I tried so far:

data.some(function (obj) {
    if(obj.name === "code"){
        return obj[name]; // Return value if name exists
    }
});

I would like to check if name exists in the object. If does, pull the value for that name. My code will produce undefined in the console. Can someone help with this? Is there a way to achieve this?

PM 77-1
  • 12,933
  • 21
  • 68
  • 111
espresso_coffee
  • 5,980
  • 11
  • 83
  • 193

3 Answers3

1

If I understand your question correctly, then you can achieve this by .filter(), where for each item being filtered, your examine the item's keys (via Object.keys()) to see if it contains any of the keys that you're searching/filtering against:

var namesToCompare = ['name', 'code'];

var data = [
      {
        "row": 23,
        "code": "ERT"
      },
      {
        "row": 45,
        "code": "TTR"
      },
      {
        "row": 90,
        "code": "CXZ"
      },
      {
        "row": 91,
        "code": "ABC", 
        "name" : "code"
      }
    ];
    
var result = data
.filter(item => Object
    .keys(item)
    .find(key => namesToCompare.includes(key)))

console.log('items with keys', namesToCompare, 'in list', result);
Dacre Denny
  • 29,664
  • 5
  • 45
  • 65
1

Something like so:

data = [
      {
        "row": 23,
        "code": "ERT"
      },
      {
        "row": 45,
        "code": "TTR"
      },
      {
        "row": 90,
        "code": "CXZ"
      }
    ];


$.each(data, function(index,v) {
 $.each(v, function(key,val) {
    if(key == "code")
       console.log(val)
    }
 });
});

Without inner loop:

for (var key in data){

  if(key == "code"){
     console.log(key)
  }
}
clearshot66
  • 2,292
  • 1
  • 8
  • 17
  • I was trying to avoid inner loop for efficiency purposes. Do you have any experience with the solution above? How that will affect the large amount of data? – espresso_coffee Nov 01 '18 at 18:56
  • Just fixed, you can use second example for no loop. I use it but I also use under 100 objects typically. Try it and find out. Simple google of "Search a JS object" yields 100s of results – clearshot66 Nov 01 '18 at 18:58
  • https://stackoverflow.com/questions/7700987/performance-of-key-lookup-in-javascript-object – clearshot66 Nov 01 '18 at 18:59
  • Second solution will only provide element number/position. I need the actual name of each element in the object. I hope this make sense. When I loop through I should get `row` and `code` for the name. – espresso_coffee Nov 01 '18 at 19:01
  • @espresso_coffee I'm using jsbin right now and that solution returns "ERT" ,"TTR","CXZ". It gives the actual name. editing to show key too then now. – clearshot66 Nov 01 '18 at 19:03
  • Those are the values. I need to return `row` or/and `code`. – espresso_coffee Nov 01 '18 at 19:04
  • Why on earth would you return the word row or code ? That's just the key name which is in every object by default – clearshot66 Nov 01 '18 at 19:04
  • Because I might have to return just specific key values. In that case I have to loop and compare key names with the array of column names that will be displayed. I hope this make sense. – espresso_coffee Nov 01 '18 at 19:06
  • You need to clarify your question. Actually all the solutions are correct based in what you asked. – Ariel Nov 01 '18 at 19:15
1

You can do something like this:

const name = "code";
const data = [
      {
        "row": 23,
        "code": "ERT"
      },
      {
        "row": 45,
        "code": "CXZ"
      },
      {
        "row": 90,
        "code": "TTR"
      },
      {
        "row": 190,
        "code": "CXZ"
      }
    ];

const values = [];

data.forEach(element => {
 if (Object.keys(element).includes(name)) {
   values.push(element[name]);
  }
});
    
console.log(values);

Object.keys grab all the keys of the object, and then includes searchs the "name" in those keys.

Ariel
  • 1,366
  • 7
  • 16