简体   繁体   中英

How can i search for a value in a multi-dimensional array in typescript

this is my array:

0: {value: "VALUE1", label: "VALUE1"}
1: {value: "VALUE2", label: "VALUE2"}

I want to search for a value truck in this array, if found i need to run a function, else another function

 function find(value, array) { for(data of array) { if(data['value'] === value) { return data } } return null } const result = find('truck', [ { value: "VALUE1", label: "VALUE1" }, { value: "VALUE2", label: "VALUE2" }, { value: "truck", label: "VALUE2" } ]) if (result) { console.log('found a truck', { result }) } else { console.log('no truck found') } 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM