How to check if array contains at least one object ?

前端 未结 5 1785
余生分开走
余生分开走 2021-02-08 13:04

I want to check if array contains object or not. I am not trying to compare values just want to check in my array if object is present or not?

Ex.

$arr =         


        
5条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-02-08 13:44

    You can use some method which tests whether at least one element in the array passes the test implemented by the provided function.

    let arr = [{id: 1}, {id:2}, 'a', 'b'];
    let exists = arr.some(a => typeof a == 'object');
    console.log(exists);

提交回复
热议问题