0

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.

2
  • Also, what are you checking the search array against? The ids? Commented Feb 24, 2014 at 17:32
  • yes, i don't know if i have to cycle the search or the object...i try with sugarjs.com/api/Array/find Commented Feb 24, 2014 at 17:34

7 Answers 7

2
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.

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

Comments

2

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

Comments

0
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; });
}

Comments

0
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' }]

Comments

0

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) 
    }) 
})

Comments

0

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'
   }

Comments

0

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.

Comments

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.