提问人:sPohlerUs 提问时间:6/12/2023 最后编辑:DecPKsPohlerUs 更新时间:6/12/2023 访问量:60
将数组中的值添加到数组中的每个对象
Add values from Array to every Object in Array
问:
对于一个项目,我需要将数组的值推送到另一个数组中的每个对象中。 到目前为止我所拥有的:
closed_status = [{
"project_no": 5,
"priority": 3,
"s_status": "S8",
"project_Status: closed"
},
{
"project_no": 8,
"priority": 1,
"s_status": "S5",
"project_Status: closed"
},
{
"project_no": 12,
"priority": 2,
"s_status": "S2",
"project_Status: closed"
}
]
str = [
"Value 1",
"Value 2",
"Value 3",
]
let result = []
closed_status.forEach((obj) => {
str.forEach((newValue) => {
result.push({ ...obj,
newValue
})
})
})
...这就是我得到的结果
result = [{
"project_no" : 5,
"priority" : 3,
"s_status": "S8",
"project_Status: closed",
"newValue": "Value 1"
},
{
"project_no" : 5,
"priority" : 3,
"s_status": "S8",
"project_Status: closed",
"newValue": "Value 2"
},
{
"project_no" : 5,
"priority" : 3,
"s_status": "S8",
"project_Status: closed",
"newValue": "Value 3"
},
{
"project_no" : 8,
"priority" : 1,
"s_status": "S5",
"project_Status: closed",
"newValue": "Value 1",
},
{
"project_no" : 8,
"priority" : 1,
"s_status": "S5",
"project_Status: closed",
"newValue": "Value 2",
}]
...等等...... 但我想得到的是:
{
"project_no" : 5,
"priority" : 3,
"s_status": "S8",
"project_Status: closed",
"newValue": "Value 1",
},
{
"project_no" : 8,
"priority" : 1,
"s_status": "S5",
"project_Status: closed",
"newValue": "Value 2",
},
{
"project_no" : 12,
"priority" : 2,
"s_status": "S2",
"project_Status: closed",
"newValue": "Value 3",
},
有人知道我的错误在哪里吗?
提前致谢
答:
1赞
Geshode
6/12/2023
#1
假设 and 总是具有相同的长度,从您的示例和想要的结果中可以看出,那么您可以执行以下操作。closed_status
str
for (let i = 0; i < closed_status.length; i++) {
closed_status[i].newValue = str[i]
}
评论
1赞
sPohlerUs
6/12/2023
非常感谢,这是我需要的答案:D
0赞
Wimanicesir
6/12/2023
#2
我猜你想用 str 的值填充对象数组。
您可以通过跟踪索引来做到这一点:
closed_status = [{
"project_no": 5,
"priority": 3,
"s_status": "S8",
"project_Status": "closed"
},
{
"project_no": 8,
"priority": 1,
"s_status": "S5",
"project_Status": 'closed'
},
{
"project_no": 12,
"priority": 2,
"s_status": "S2",
"project_Status": "closed"
}
]
str = [
"Value 1",
"Value 2",
"Value 3",
]
closed_status.forEach((obj,index) => {
obj.newValue = str[index]
})
console.log(closed_status)
0赞
0stone0
6/12/2023
#3
如果您确定并且拥有相同数量的项目,则可以使用将单个值移出您分布到原始对象中的单个值。str
closed_status
str.shift()
closed_status = [
{
"project_no": 5,
"priority": 3,
"s_status": "S8",
"project_Status": "closed"
},
{
"project_no": 8,
"priority": 1,
"s_status": "S5",
"project_Status": "closed"
},
{
"project_no": 12,
"priority": 2,
"s_status": "S2",
"project_Status": "closed"
}
]
str = [
"Value 1",
"Value 2",
"Value 3",
]
closed_status = closed_status.map(e => ({ ...e, newValue: str.shift() }) );
console.log(closed_status);
0赞
PeterKA
6/12/2023
#4
以下解决方案假定第一个数组比第二个数组长。但是,长度始终相同,然后可以用 替换。i < str.length ? i : i % str.length
i
const
closed_status = [{
"project_no": 5,
"priority": 3,
"s_status": "S8",
"project_Status": "closed"
},
{
"project_no": 8,
"priority": 1,
"s_status": "S5",
"project_Status": "closed"
},
{
"project_no": 12,
"priority": 2,
"s_status": "S2",
"project_Status": "closed"
}
],
str = [
"Value 1",
"Value 2",
"Value 3",
],
result = closed_status
.map((status,i) => ({...status, newValue:str[i < str.length ? i : i % str.length]}));
console.log( result );
//alternatively, (EQUAL LENGTHS)
console.log( str.map((newValue,i) => ({...closed_status[i],newValue})) );
评论