This is my array
let a = [{"id":1,"barcode":"8851907264888"},{"id":2,"barcode":"8857124022072"}];
I want to input a barcode and check that if the input barcode is exist in array or not, what should I do?
This is my array
let a = [{"id":1,"barcode":"8851907264888"},{"id":2,"barcode":"8857124022072"}];
I want to input a barcode and check that if the input barcode is exist in array or not, what should I do?
The
some()
method tests whether at least one element in the array passes the test implemented by the provided function. It returns a Boolean value.
arr.some(el => el.barcode === barcodeToSearch);
const a = [{
"id": 1,
"barcode": "8851907264888"
}, {
"id": 2,
"barcode": "8857124022072"
}];
const hasBarcode = (arr, barcode) => arr.some(el => el.barcode === barcode);
const barcode1 = "1234567890123";
const barcode2 = "8857124022072";
console.log(hasBarcode(a, barcode1)); // false
console.log(hasBarcode(a, barcode2)); // true