does include works with array of objects?

前端 未结 3 888
深忆病人
深忆病人 2021-01-28 23:24

i\'m trying to use includes to see if an object is inside the array like so:

arr=[{name:\'Dan\',id:2}]

and I want to check like so:

<         


        
3条回答
  •  南方客
    南方客 (楼主)
    2021-01-29 00:04

    Not like that. Use some instead:

    arr.some(obj => obj.id === 2)
    

    Which checks if there is an object with id equals to 2.

    If you are checking for both id and name then:

    arr.some(obj => obj.id === 2 && obj.name === "Dan")
    

提交回复
热议问题