英文:
JavaScript add arrays to existing array of objects
问题
I can help you with the translation part of your request. Here is the translated code snippet:
我有以下代码片段
<!-- 开始代码片段:js 隐藏:false 控制台:true babel:false -->
<!-- 语言:lang-js -->
const arr = [
{
"name": "认证组件 1",
"values": [
{
"component": "认证组件 1"
},
{
"component": "认证组件 1"
},
{
"component": "认证组件 1",
}
]
},
{
"name": "认证组件 2",
"values": [
{
"id": "10005884",
"url": "https://www.msn.com",
"bfaId": "G44.5.3.1N/A",
"component": "认证组件 2"
},
{
"id": "10005883",
"url": "https://www.hotmail.com",
"bfaId": "G44.5.3.2N/A",
"component": "认证组件 2"
}
]
},
{
"name": "认证组件 3",
"values": [
{
"id": "10005882",
"url": "https://www.rediffmail.com",
"bfaId": "G44.5.3.3N/A",
"component": "认证组件 3"
}
]
}
]
const bool = arr.map(group => group.values.every(val => val.id));
console.log(bool);
<!-- 结束代码片段 -->
我有三个对象,名称分别为认证组件 1、认证组件 2 和认证组件 3。我得到的期望输出是false、true、true。这是什么原因?我想在现有对象数组下方添加属性,作为`isInvalid: true/false`,位于`name`之下。
期望输出(在每个对象中添加以下键值对属性)
`isInvalid: true/false`
英文:
I've below code snippet
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
const arr = [
{
"name": "Attestation Component 1",
"values": [
{
"component": "Attestation Component 1"
},
{
"component": "Attestation Component 1"
},
{
"component": "Attestation Component 1",
}
]
},
{
"name": "Attestation Component 2",
"values": [
{
"id": "10005884",
"url": "https://www.msn.com",
"bfaId": "G44.5.3.1N/A",
"component": "Attestation Component 2"
},
{
"id": "10005883",
"url": "https://www.hotmail.com",
"bfaId": "G44.5.3.2N/A",
"component": "Attestation Component 2"
}
]
},
{
"name": "Attestation Component 3",
"values": [
{
"id": "10005882",
"url": "https://www.rediffmail.com",
"bfaId": "G44.5.3.3N/A",
"component": "Attestation Component 3"
}
]
}
]
const bool = arr.map(group => group.values.every(val => val.id));
console.log(bool);
<!-- end snippet -->
I've three object with name Attestation Component 1, Attestation Component 2 ,Attestation Component 3. I'm getting the expected output as false, true, true. What's the reason for this? I want to add the property to the existing array of object as something isInvalid: true/false
below name
Expected O/P (add property in each object with below key value pairs)
isInvalid: true/false
答案1
得分: -2
这是因为你的算法是不正确的。every
方法会检查所有对象是否都有一个 id,但这不是你想要的,对吧?
所以请尝试这个替代方法:
const bool = arr.map(group => group.values.some(val => val.id));
英文:
That is because your algorithm is incorrect. The every
method will check if all the objects have an id, but that is not what you want right ?
So try this instead
const bool = arr.map(group => group.values.some(val => val.id));
答案2
得分: -3
使用every()
时,您应该使用some()
。
const bool = arr.map(group => group.values.some(val => val.id)).filter(bool => !bool).toString();
every()
方法用于检查数组的所有元素是否满足给定条件。而some()
方法用于检查数组中是否至少有一个元素满足给定条件。
英文:
Instead of every(), you should use some().
const bool = arr.map(group => group.values.some(val => val.id)).filter(bool => !bool).toString();
every() method is used to check whether all the elements of the array satisfy the given condition or not. The Array. some() method is used to check whether at least one of the elements of the array satisfies the given condition or not.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论