-3

Here my array :

[{id: 1, value:'blabla'}, {id: 2, value:'blabla'}, {id: 3, value:'blabla'}]

I try to return true or false if an id is present in this array :

array.includes(2) -> true
array.includes(7) -> false

I need to do this on the id index of each object of the array.

I know i can use a foreach on each id of the array, but i want to use a cleanest way to do this. What can i use ? Thanks !

Tony S
  • 491
  • 6
  • 26
  • 3
    make use of [`some`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/some) start an approach and ask again if anything still remains unclear. – Peter Seliger Feb 19 '21 at 10:46
  • 1
    array.some(item => item.id === yourID) – Kunvar Singh Feb 19 '21 at 10:51
  • 1
    [Duplicate](https://google.com/search?q=site%3Astackoverflow.com+js+check+if+array+contains+object+with+id) of [How to determine if Javascript array contains an object with an attribute that equals a given value?](https://stackoverflow.com/q/8217419/4642212). – Sebastian Simon Feb 19 '21 at 10:58
  • 1
    Familiarize yourself with [how to access and process nested objects, arrays or JSON](https://stackoverflow.com/q/11922383/4642212) and how to [create objects](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Object_initializer) and use the available [`Object`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object#Static_methods) and [`Array`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array#Static_methods) methods (both static and on prototype). – Sebastian Simon Feb 19 '21 at 10:59

2 Answers2

2

Operating Array.prototype.some directly at the given array, one can write ...

array.some(({ id }) => id === yourID)

... or one uses a more generic approach like ...

var array = [
  { id: 1, value: 'blabla' },
  { id: 2, value: 'blabla' },
  { id: 3, value: 'blabla' },
];

function hasItemWithKeyAndValue(arr, key, value) {
  return arr.some(item => item[key] === value);
}

// what the OP did ask for.
console.log(
  "hasItemWithKeyAndValue(array, 'id', 1) ?",   // true
  hasItemWithKeyAndValue(array, 'id', 1)
);
console.log(
  "hasItemWithKeyAndValue(array, 'id', '1') ?", // false
  hasItemWithKeyAndValue(array, 'id', '1')
);
console.log(
  "hasItemWithKeyAndValue(array, 'id', 3) ?",   // true
  hasItemWithKeyAndValue(array, 'id', 3)
);
console.log(
  "hasItemWithKeyAndValue(array, 'id', 4) ?",   // false
  hasItemWithKeyAndValue(array, 'id', 4)
);

// proof of the generic key value approach.
console.log(
  "hasItemWithKeyAndValue(array, 'value', 'blabla') ?", // true
   hasItemWithKeyAndValue(array, 'value', 'blabla')
);
.as-console-wrapper { min-height: 100%!important; top: 0; }
Peter Seliger
  • 11,747
  • 3
  • 28
  • 37
Kunvar Singh
  • 1,779
  • 2
  • 13
  • 21
0

You can use Array.some():

let arr = [{id: 1, value:'blabla'}, {id: 2, value:'blabla'}, {id: 3, value:'blabla'}];
arr.some(e => e.id === 1);
// -> true

If you want an approach like Array.includes() you can extend the Array prototype:

let arr = [{id: 1, value:'blabla'}, {id: 2, value:'blabla'}, {id: 3, value:'blabla'}];

Array.prototype.hasId = function(id){
    return this.some(e => e.id === id);
};

console.log(arr.hasId(1));
console.log(arr.hasId(3));
console.log(arr.hasId(4));

Otherwise I'd just wrap it inside a function:

let arr = [{id: 1, value:'blabla'}, {id: 2, value:'blabla'}, {id: 3, value:'blabla'}];


let hasId = function(arr, id){
    return arr.some(e => e.id === id);
};

console.log(hasId(arr, 1));
console.log(hasId(arr, 2));
console.log(hasId(arr, 4));
NullDev
  • 6,739
  • 4
  • 30
  • 54