2

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

1
  • a.map(x => x.id).indexOf(b.id) != -1 Commented May 17, 2018 at 6:25

8 Answers 8

1

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

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

Comments

0

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.

Comments

0

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

Comments

0

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)

Comments

0

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

Comments

0

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  

Comments

0

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

Comments

0

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

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.