我有一系列包含对象的数组,其中包含对象的数组...
const testArray = [
[
{
name: "object 1",
elements: [
{type: "horizontal", scope: "name"},
{type: "vertical", scope: "age"}
]
},
{
name: "object 2",
elements: [
{type: "horizontal", scope: "age"},
{type: "vertical", scope: "location"}
]
},
],
[
{
name: "object 3",
elements: [
{type: "horizontal", scope: "age"},
{type: "vertical", scope: "location"}
]
}
]
]
我只想返回包含对象不包含作用域的对象的数组:“名称”。在上述情况下,它将返回testArray[1]
。我尝试使用forEach
和some()
,但无法正常工作。
//is called inside function that iterates over testArray
const generateNameField = arr => {
arr.forEach(item => {
const nameFieldFound = item.elements.some(el => el.scope === "name")
if(!nameFieldFound){
return arr
}
})
}
但是因为其中一个对象不包含scope: "name"
,所以它返回了该对象。似乎无法解决此问题,不胜感激。
您需要使用“每个”
const result=testArray.filter(e=>e.every(k=>k.elements.every(l=>l.scope!='name')))
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句