如何检查数组是否包含至少一个对象? [英] How to check if array contains at least one object ?

查看:88
本文介绍了如何检查数组是否包含至少一个对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想检查数组是否包含对象.我不是试图比较值,只是想检查是否存在对象?

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?

例如.

$arr = ['a','b','c'] // normal
$arr = [{ id: 1}, {id: 2}] // array of objects
$arr = [{id: 1}, {id:2}, 'a', 'b'] // mix values

那么我如何检查数组是否包含对象

So how can i check if array contains object

推荐答案

您可以使用some方法测试array中的至少一个元素是否通过提供的 strong>功能.

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);

这篇关于如何检查数组是否包含至少一个对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆