Twinsen
Twinsen

Reputation: 893

Find Element in Array Object

I want to check if there is a certain value in an array of objects.

For example, if I have something like this:

[ { _id: 1,    
    name: foo },
  { _id: 2,    
    name: bar },
    { _id: 3,    
    name: foox },
    { _id: 4,    
    name: fooz },
    ]


var search = [1,25,33,4,22,44,5555,63]

then I want to check if one of the values in search is in one of the objects contained in the array of objects.

Upvotes: 0

Views: 334

Answers (7)

Andy
Andy

Reputation: 63589

Use some to iterate over the array of objects. If an id is found some short-circuits and doesn't continue with the rest of the iteration.

const data=[{_id:1,name:'foo'},{_id:2,name:'bar'},{_id:3,name:'foox'},{_id:4,name:'fooz'}];
const search = [1,25,33,4,22,44,5555,63]

function finder(searh) {
  return data.some(obj => {
    return search.includes(obj._id);
  });
}

console.log(finder(data, search));

Upvotes: 2

Adam Gawne-Cain
Adam Gawne-Cain

Reputation: 1720

A limitation of forEach loops is that you cannot return the found element from your (outer) method. Instead you can use Array.prototype.find as follows:

var elt = list.find(e => search.indexOf(e._id)>=0);
if (!elt)
    console.log("Element not found");
else
    console.log("Found element " + elt.name);

Note: This will require a polyfill for Array.prototype.find in IE.

Upvotes: 0

Naresh Bhadke
Naresh Bhadke

Reputation: 433

Below is the example of find method. Hope this will help you.

   // sample item array
   const items = [
                   { name : 'Bike', price : 100 },
                   { name : 'Car',  price : 3000 }
                 ]
   // find method example
   const foundItem = items.find(( item )) => {
         // you can put your desired condition over here to find an element from 
         // javascript array.
         return item.name == 'Bike'
   }

Upvotes: 0

Triode
Triode

Reputation: 11357

var list = [ { _id: 1, name: "foo" },
  { _id: 2, name: "bar" },
  { _id: 3, name: "foox" },
  { _id: 4, name: "fooz" },
];


var search = [1,25,33,4,22,44,5555,63];

list.forEach(function(element){
    if(search.indexOf(element._id) != -1){
        console.log("found");
    }
});

Try this, hope this is what you are looking for.

Upvotes: 2

Amir Sherafatian
Amir Sherafatian

Reputation: 2083

if:

var o = [{ _id: 1, name: "foo"}, { _id: 2, name: "bar"}, { _id: 3, name: "foox"}, { _id: 4, name: "fooz"}];
var search = [1, 25, 33, 4, 22, 44, 5555, 63];

try this:

var outPus = o.filter(function(u){ 
    return search.some(function(t){ return t == u._id}) 
})

or this:

var outPut = [];
search.forEach(function(u){ 

    o.forEach(function(t){ 

        if(t._id == u) outPut.push(t) 
    }) 
})

Upvotes: 0

Tibos
Tibos

Reputation: 27853

var list = [ 
  { _id: 1, name: 'foo' },
  { _id: 2, name: 'bar' },
  { _id: 3, name: 'foox' },
  { _id: 4, name: 'fooz' }
];

var search = [1,25,33,4,22,44,5555,63];

This code builds a list of all the elements in search that are also in your list:

var inArr = search.filter(function(index){
  return list.some(function(el){
    return el._id === index;
  });
});
console.log(inArr); // [1,4];

Obviously you can switch the list and search around so you get an array with the elements from list that are referenced in search:

var elements = list.filter(function(el){
  return search.some(function(index){
    return el._id === index;
  });
});
console.log(elements); // [{ _id: 1, name: 'foo' },{ _id: 4, name: 'fooz' }]

Upvotes: 0

Rui
Rui

Reputation: 4886

var list = [ { _id: 1,    
    name: foo },
  { _id: 2,    
    name: bar },
    { _id: 3,    
    name: foox },
    { _id: 4,    
    name: fooz },
    ];

var isAnyOfIdsInArrayOfObject = function(arrayOfObjects, ids){
   return arrayOfObjects.some(function(el) { return ids.indexOf(el._id) !== -1; });
}

Upvotes: 0

Related Questions