2

I know to find a value exist or not in an array I can use indexOf, but how to do it with an array of object?

const x = [{
  "id": "roadshows",
  "name": "Roadshows"
}, {
  "id": "sporting_events",
  "name": "Sporting Events"
}]

console.log( x.indexOf('roadshows') ) // don't work
4
  • obvously indexOf works only for primitive type values. You gotta loop through array Commented Jun 1, 2017 at 6:22
  • There's no built-in function that does this. You could do it using libraries like underscore.js or lodash. Otherwise you have to write your own loop. Commented Jun 1, 2017 at 6:23
  • x.map(o => o.id).indexOf('roadshows') Commented Jun 1, 2017 at 6:25
  • x.findIndex(o => o.id === 'roadshows') Commented Jun 1, 2017 at 6:25

5 Answers 5

7

Since this is tagged , here's an ES6 array method: Array#findIndex():

const x = [{
  "id": "roadshows",
  "name": "Roadshows"
}, {
  "id": "sporting_events",
  "name": "Sporting Events"
}]

console.log( x.findIndex( o => o.id === 'roadshows' ) )

If you want a more re-useable way of doing this, consider creating a factory isId(id):

function isId(id) {
  return (o) => o.id === id;
}

const x = [{
  "id": "roadshows",
  "name": "Roadshows"
}, {
  "id": "sporting_events",
  "name": "Sporting Events"
}]

console.log( x.findIndex( isId('roadshows') ) )

This is referred to as a "factory" because it is a function that returns a function with the passed parameter in its scope.

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

Comments

0

You have to loop through since you have object's inside array.

for(var i = 0; i < x.length; i++) {
    if (x[i].id== 'roadshows') {
        console.log(i);
        break;
    }
}

Or if you just checking that the object exist with that id, filter is handy

if (x.filter(function(e) x.id== 'roadshows').length > 0) {
  // Yay. Do Something
}

Comments

0

manually I'd do something like this:

for(let item of x) {
 if ( item.hasOwnProperty('id') && item['id'] == 'roadshows' ) {   
    //do your stuff here
 }
}

Comments

0

And if you can use es6 and want to return the object in question, then there is always Array.prototype.find()

x.find( item => { return item.id === "roadshows" } )

// returns {id: "roadshows", name: "Roadshows"}

Comments

0

You have a couple of options.

First and foremost, findIndex. You pass it a function that tests if an element is what you are looking for, it returns the index of the first element that makes that function return true.

x.findIndex((o) => o.id === 'roadshows');

const x = [{
  "id": "roadshows",
  "name": "Roadshows"
}, {
  "id": "sporting_events",
  "name": "Sporting Events"
}];

console.log(x.findIndex((o) => o.id === 'roadshows'));

Another option is first mapping the relevant property to an array and searching in that one.

x.map((o) => o.id).indexOf('roadshows');

const x = [{
  "id": "roadshows",
  "name": "Roadshows"
}, {
  "id": "sporting_events",
  "name": "Sporting Events"
}];

console.log(x.map((o) => o.id).indexOf('roadshows'));

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.