简体   繁体   中英

How to check if the id exist in the array? and get the other values

[
    {id: 1, name: 'foo'},
    {id: 2, name: 'bar'},
    {id: 3, name: 'test'}
]

I want to get check if the data exist and use the other field. example i want to check if 'id: 1' exist in the string and the 'name:foo'

You can use Array.propotype.filter

 var list=[ {id: 1, name: 'foo'}, {id: 2, name: 'bar'}, {id: 3, name: 'test'} ] const result = list.filter(data => data.id == 2); console.log(result);

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