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?

3 Answers 3

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)
  }
}
Sign up to request clarification or add additional context in comments.

9 Comments

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?
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
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 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.
|
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);

1 Comment

Can we filter by Value instead of Key here?
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.

4 Comments

What if for example I have array of name that I have to compare to the Object.keys(element)? Basically instead of comparing just one name, I have to compare multiple names.
Actually that wasn't part of your question, but... you need another loop to do that.
I know, my apologies. That's what I realized. Seems like not very efficient solution.
@espresso_coffee does my answer help with this?

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.