JavaScript 将数组添加到现有对象数组中

huangapple go评论65阅读模式
英文:

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 babelfalse -->

<!-- 语言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我得到的期望输出是falsetruetrue这是什么原因我想在现有对象数组下方添加属性作为`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 = [
{
&quot;name&quot;: &quot;Attestation Component 1&quot;,
&quot;values&quot;: [
{
&quot;component&quot;: &quot;Attestation Component 1&quot;
},
{
&quot;component&quot;: &quot;Attestation Component 1&quot;
},
{
&quot;component&quot;: &quot;Attestation Component 1&quot;,
}
]
},
{
&quot;name&quot;: &quot;Attestation Component 2&quot;,
&quot;values&quot;: [
{
&quot;id&quot;: &quot;10005884&quot;,
&quot;url&quot;: &quot;https://www.msn.com&quot;,
&quot;bfaId&quot;: &quot;G44.5.3.1N/A&quot;,
&quot;component&quot;: &quot;Attestation Component 2&quot;
},
{
&quot;id&quot;: &quot;10005883&quot;,
&quot;url&quot;: &quot;https://www.hotmail.com&quot;,
&quot;bfaId&quot;: &quot;G44.5.3.2N/A&quot;,
&quot;component&quot;: &quot;Attestation Component 2&quot;
}
]
},
{
&quot;name&quot;: &quot;Attestation Component 3&quot;,
&quot;values&quot;: [
{
&quot;id&quot;: &quot;10005882&quot;,
&quot;url&quot;: &quot;https://www.rediffmail.com&quot;,
&quot;bfaId&quot;: &quot;G44.5.3.3N/A&quot;,
&quot;component&quot;: &quot;Attestation Component 3&quot;
}
]
}
]
const bool = arr.map(group =&gt; group.values.every(val =&gt; 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 =&gt; group.values.some(val =&gt; 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 =&gt; group.values.some(val =&gt; val.id)).filter(bool =&gt; !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.

huangapple
  • 本文由 发表于 2023年7月4日 22:10:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/76613499.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定