nero nero - 1 year ago 86
Node.js Question

is there a built in command that returns which array element contains a specific string in javascript

my question is that is there a command which can return for me which object has a specfic data. For example , if I have the following json data , I want to know which entry has Serial Number of value 11 which should return to me the index of that entry which is 0 :

"Barcode": "123451",
"serialNumber": "11"
"Barcode": "1234512",
"serialNumber": "12"
"Barcode": "123451",
"serialNumber": "13"

so basically i'm looking for something similar to (includes) but instead of stating true or false , it states which object has it :

var data = JSON.parse(body);
returns : index=2 or last object

Answer Source

That is what the find or findIndex methods are for:

data.find(e => e.serialNumber === '13') 
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download