Raghul
Raghul

Reputation: 213

angularjs: check if value exists in array of objects

var a = [ { id:1}, {id:2} ];
var b = {id:1};
var res = a.indexOf(b._id) == -1;
console.log(res);

I want to check if b._id is in a[].
Note: a[] is an array of objects

Upvotes: 2

Views: 5654

Answers (8)

Navnath Godse
Navnath Godse

Reputation: 2225

You can use Filter of AngularJS

var a = [{id:1}, {id:2}];
var b = {id:1};

var found = false;
var filterResult = $filter('filter')(a, {id: b.id}, true);

if (filterResult.length > 0) {
    found = true;
}

Upvotes: 0

Vikasdeep Singh
Vikasdeep Singh

Reputation: 21756

Use Array.map() function of JavaScript to check it. It will compare id and its value as well.

Below is working code:

var a = [{
  id: 1
}, {
  id: 2
}];
var b = {
  id: 1
};

if (a.map(x => x.id).indexOf(b.id) != -1) {
  console.log("Available");
} else {
  console.log("Not available");
}

Upvotes: 0

Ramesh Rajendran
Ramesh Rajendran

Reputation: 38663

try this

var a = [ { id:1} ,{id:2} ];

var b={id:1}

console.log(a.find(x=>x.id==b.id))// return matched record

var a = [ { id:1} ,{id:2} ];

var b={id:3}

console.log(a.find(x=>x.id==b.id)) //return undefined  

Upvotes: 0

Sudhir Ojha
Sudhir Ojha

Reputation: 3305

Array.map() function compare id and its value and return a Boolean value if map as commented by @Slava Utesinov

var a = [{id: 1}, {id: 2}];
var b = {id: 1};
if(a.map(x => x.id).indexOf(b.id) != -1){
  console.log("Exists");
}else{
  console.log("Not exists");
}

Upvotes: 0

Sarath Kumar
Sarath Kumar

Reputation: 2353

Try this..

    var a = [{ id:1}, {id:2}];
    var b={id:1};
    var arrayWithIds = a.map(function(x){
                               return x.id
                          }); // get new array contains all ids 
    var present = arrayWithIds.indexOf(b.id) != -1 // find the b.id array
    console.log(present);

Here is the reference for Map and indexOf

Upvotes: 1

Saeed
Saeed

Reputation: 5488

A better way is using .find function.

let a = [{
    id: 1
  }, {
    id: 2
  }],
  b = {
    id: 1
  },
  obj = a.find(function(itm) {
    return itm.id == b.id;
  });

console.log(obj)

And also using .findIndex function to get just index of item in array.

let a = [{
    id: 1
  }, {
    id: 2
  }],
  b = {
    id: 1
  },
  objIndex = a.findIndex(function(itm) {
    return itm.id == b.id;
  });

console.log(objIndex)

And for getting all objects with that condition use .filter function.

let a = [{
    id: 1
  }, {
    id: 2
  }],
  b = {
    id: 1
  },
  objArr =  a.filter(function(itm) {
      return itm.id == b.id;
  });

console.log(objArr)

Upvotes: 0

I. Ahmed
I. Ahmed

Reputation: 2534

Please use the following code:

var a = [ { id:1}, {id:2} ];
var b={id:1}

function findMatch(element) {
  return element.id === b.id;
}

console.log(a.findIndex(findMatch));

Upvotes: 0

supravi
supravi

Reputation: 59

This should work :

var a = [ { id:1} ,{id:2} ];
var b={id:1}
console.log(a.findIndex(function(obj){return obj.id=b.id}))

indexOf works when you are dealing with indexed arrays not with array of objects.

Upvotes: 0

Related Questions