简体   繁体   English

如何检查数组是否包含至少一个对象?

[英]How to check if array contains at least one object ?

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 = ['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 那么如何检查数组是否包含对象

You can use some method which tests whether at least one element in the array passes the test implemented by the provided function. 您可以使用some方法来测试array是否至少有一个元素通过了由提供的函数实现的test

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

I want to check if array contains object or not 我想检查数组是否包含对象

Use some to simply check if any item of the array has value of type "object" 使用一些来简单地检查数组中的任何项是否具有“object”类型的值

var hasObject = $arr.some( function(val){
   return typeof val == "object";
});

 var hasObject = function(arr) { for (var i=0; i<arr.length; i++) { if (typeof arr[i] == 'object') { return true; } } return false; }; console.log(hasObject(['a','b','c'])); console.log(hasObject([{ id: 1}, {id: 2}])); console.log(hasObject([{id: 1}, {id:2}, 'a', 'b'])); 

You could count the objects and use it for one of the three types to return. 您可以计算对象并将其用于返回的三种类型之一。

 function getType(array) { var count = array.reduce(function (r, a) { return r + (typeof a === 'object'); }, 0); return count === array.length ? 'array of objects' : count ? 'mix values' : 'normal'; } console.log([ ['a', 'b', 'c'], [{ id: 1 }, { id: 2 }], [{ id: 1 }, { id: 2 }, 'a', 'b'] ].map(getType)); 

对数组进行类型检查

const hasObject = a => Array.isArray(a) && a.some(val => typeof val === 'object')

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何检查对象是否包含至少一个其值包含JavaScript子字符串的键? - How can I check if an object contains at least one key whose value contains a substring in JavaScript? 如何检查一个Javascript数组,该数组至少包含一个以特定文本开头的值(例如ROLE_) - How to check a Javascript array that contains at least one value that starts with a particular text (eg. ROLE_) 在JS中如何检查对象数组是否包含一个对象? - How in JS check if array of objects contains one object? 如何检查数组对象是否包含angularjs中的一个字符串 - How to check if an array object contains one string in angularjs JavaScript 如何检查字符串是否至少包含一个字母? - How to check if a string contains at least one letter in JavaScript? 如何检查数组的至少一项是否包含在 object 的数组中并遍历所有对象(JS) - How to check if at least one item of an array is included in an array of an object and loop through all objects (JS) 检查集合中是否至少包含一个文档 - Check if a collection contains at least one document 如何在另一个 object 中检查至少一个 object - How to check for at least one object inside another object Javascript:有没有办法检查一个数组是否至少包含另一个数组中的一个元素? - Javascript: Is there a way to check if an array contains at least one element from another array? 检查对象是否包含数组 - Check if object contains array
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM