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; }
somestart an approach and ask again if anything still remains unclear.ObjectandArraymethods (both static and on prototype).