Check if value exists in Array object Javascript or Angular

后端 未结 4 989
粉色の甜心
粉色の甜心 2021-01-06 15:43

I want to check if value exist in array object, example:

I have this array:

[
    {id: 1, name: \'foo\'},
    {id: 2, name: \'bar\'},
    {id: 3, nam         


        
4条回答
  •  渐次进展
    2021-01-06 16:04

    You can use find method as below

    var x=[
        {id: 1, name: 'foo'},
        {id: 2, name: 'bar'},
        {id: 3, name: 'test'}
    ]
    var target=x.find(temp=>temp.id==2)
    if(target)
      console.log(target)
    else
      console.log("doesn't exists")

提交回复
热议问题