将数组中的值添加到数组中的每个对象

Add values from Array to every Object in Array

提问人:sPohlerUs 提问时间:6/12/2023 最后编辑:DecPKsPohlerUs 更新时间:6/12/2023 访问量:60

问:

对于一个项目,我需要将数组的值推送到另一个数组中的每个对象中。 到目前为止我所拥有的:

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",
},

有人知道我的错误在哪里吗?

提前致谢

JavaScript 数组 对象 ecmascript-6

评论

0赞 sPohlerUs 6/12/2023
@technophyle 感谢您的编辑:)
0赞 CBroe 6/12/2023
“有人知道我的错误在哪里吗?”- 在制作两个嵌套循环时,什么时候你真的应该只做一个循环?
2赞 icecub 6/12/2023
到底有什么区别?我的意思是,我看到的唯一区别是实际结果是对象数组,而您的预期结果只是对象。这些值似乎是相同的。我错过了什么?
0赞 sPohlerUs 6/12/2023
@icebub - 我的错误。我试图编辑它,但收到一条错误消息,我的帖子主要是代码。结果也是一个数组。在我的循环中,我的“str”数组中的每个值都会被推送到每个对象中。

答:

1赞 Geshode 6/12/2023 #1

假设 and 总是具有相同的长度,从您的示例和想要的结果中可以看出,那么您可以执行以下操作。closed_statusstr

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

如果您确定并且拥有相同数量的项目,则可以使用将单个值移出您分布到原始对象中的单个值。strclosed_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.lengthi

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})) );