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

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

Add values from Array to every Object in Array

问题

以下是代码部分的翻译:

    closed_status = [{
        "project_no": 5,
        "priority": 3,
        "s_status": "S8",
        "project_Status: closed": "closed"
      },
      {
        "project_no": 8,
        "priority": 1,
        "s_status": "S5",
        "project_Status: closed": "closed"
      },
      {
        "project_no": 12,
        "priority": 2,
        "s_status": "S2",
        "project_Status: closed": "closed"
      }
    ]
    str = [
      "Value 1",
      "Value 2",
      "Value 3",
    ]
    let result = []
    closed_status.forEach((obj) => {
      str.forEach((newValue) => {
        result.push({ ...obj,
          newValue
        })
      })
    })

请注意,这只是代码的翻译部分,不包括问题和结果。如果您需要问题的翻译或结果的翻译,请提出明确的请求。

英文:

For a project i need to push the values of an array into every object in another array.
What i have so far:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

closed_status = [{
    &quot;project_no&quot;: 5,
    &quot;priority&quot;: 3,
    &quot;s_status&quot;: &quot;S8&quot;,
    &quot;project_Status: closed&quot;
  },
  {
    &quot;project_no&quot;: 8,
    &quot;priority&quot;: 1,
    &quot;s_status&quot;: &quot;S5&quot;,
    &quot;project_Status: closed&quot;
  },
  {
    &quot;project_no&quot;: 12,
    &quot;priority&quot;: 2,
    &quot;s_status&quot;: &quot;S2&quot;,
    &quot;project_Status: closed&quot;
  }
]
str = [
  &quot;Value 1&quot;,
  &quot;Value 2&quot;,
  &quot;Value 3&quot;,
]
let result = []
closed_status.forEach((obj) =&gt; {
  str.forEach((newValue) =&gt; {
    result.push({ ...obj,
      newValue
    })
  })
})

<!-- end snippet -->

...and this is the result i get

result = [{
&quot;project_no&quot; : 5,
&quot;priority&quot; : 3,
&quot;s_status&quot;: &quot;S8&quot;,
&quot;project_Status: closed&quot;,
&quot;newValue&quot;: &quot;Value 1&quot;
},
{
&quot;project_no&quot; : 5,
&quot;priority&quot; : 3,
&quot;s_status&quot;: &quot;S8&quot;,
&quot;project_Status: closed&quot;,
&quot;newValue&quot;: &quot;Value 2&quot;
},
{
&quot;project_no&quot; : 5,
&quot;priority&quot; : 3,
&quot;s_status&quot;: &quot;S8&quot;,
&quot;project_Status: closed&quot;,
&quot;newValue&quot;: &quot;Value 3&quot;
},
{
&quot;project_no&quot; : 8,
&quot;priority&quot; : 1,
&quot;s_status&quot;: &quot;S5&quot;,
&quot;project_Status: closed&quot;,
&quot;newValue&quot;: &quot;Value 1&quot;,
},
{
&quot;project_no&quot; : 8,
&quot;priority&quot; : 1,
&quot;s_status&quot;: &quot;S5&quot;,
&quot;project_Status: closed&quot;,
&quot;newValue&quot;: &quot;Value 2&quot;,
}]

...and so on...
But what i am trying to get is:

{
&quot;project_no&quot; : 5,
&quot;priority&quot; : 3,
&quot;s_status&quot;: &quot;S8&quot;,
&quot;project_Status: closed&quot;,
&quot;newValue&quot;: &quot;Value 1&quot;,
},
{
&quot;project_no&quot; : 8,
&quot;priority&quot; : 1,
&quot;s_status&quot;: &quot;S5&quot;,
&quot;project_Status: closed&quot;,
&quot;newValue&quot;: &quot;Value 2&quot;,
},
{
&quot;project_no&quot; : 12,
&quot;priority&quot; : 2,
&quot;s_status&quot;: &quot;S2&quot;,
&quot;project_Status: closed&quot;,
&quot;newValue&quot;: &quot;Value 3&quot;,
},

Anyone knows where my mistake is?

Thanks in advance

答案1

得分: 1

以下是翻译好的部分:

假设closed_statusstr的长度始终相同,从您的示例和期望结果来看,您可以执行以下操作。

for (let i = 0; i &lt; closed_status.length; i++) {
    closed_status[i].newValue = str[i]
}
英文:

Assuming, that closed_status and str always have the same length, which it looks like from your example and wanted result, then you could do the following.

for (let i = 0; i &lt; closed_status.length; i++) {
    closed_status[i].newValue = str[i]
}

答案2

得分: 0

我猜你想用字符串的值填充对象数组。

你可以通过跟踪索引来实现这个目标:

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)

希望这对你有所帮助。

英文:

I guess you want to fill the array of objects with the values of str.

You could do this by keeping track of the index:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

closed_status = [{
    &quot;project_no&quot;: 5,
    &quot;priority&quot;: 3,
    &quot;s_status&quot;: &quot;S8&quot;,
    &quot;project_Status&quot;: &quot;closed&quot;
  },
  {
    &quot;project_no&quot;: 8,
    &quot;priority&quot;: 1,
    &quot;s_status&quot;: &quot;S5&quot;,
    &quot;project_Status&quot;: &#39;closed&#39;
  },
  {
    &quot;project_no&quot;: 12,
    &quot;priority&quot;: 2,
    &quot;s_status&quot;: &quot;S2&quot;,
    &quot;project_Status&quot;: &quot;closed&quot;
  }
]
str = [
  &quot;Value 1&quot;,
  &quot;Value 2&quot;,
  &quot;Value 3&quot;,
]

closed_status.forEach((obj,index) =&gt; {
  obj.newValue = str[index]
})

console.log(closed_status)

<!-- end snippet -->

答案3

得分: 0

如果你确定 strclosed_status 的项数相同,你可以使用 str.shift() 将单个值移出,然后将其扩展到原始对象中。

closed_status = closed_status.map(e => ({ ...e, newValue: str.shift() }) );
console.log(closed_status);
英文:

If you're sure str and closed_status have the same amount of items, you could use str.shift() to shift out a single value that you spread into the original object.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

closed_status = [
  {
    &quot;project_no&quot;: 5,
    &quot;priority&quot;: 3,
    &quot;s_status&quot;: &quot;S8&quot;,
    &quot;project_Status&quot;: &quot;closed&quot;
  },
  {
    &quot;project_no&quot;: 8,
    &quot;priority&quot;: 1,
    &quot;s_status&quot;: &quot;S5&quot;,
    &quot;project_Status&quot;: &quot;closed&quot;
  },
  {
    &quot;project_no&quot;: 12,
    &quot;priority&quot;: 2,
    &quot;s_status&quot;: &quot;S2&quot;,
    &quot;project_Status&quot;: &quot;closed&quot;
  }
]
str = [
  &quot;Value 1&quot;,
  &quot;Value 2&quot;,
  &quot;Value 3&quot;,
]

closed_status = closed_status.map(e =&gt; ({ ...e, newValue: str.shift() }) );
console.log(closed_status);

<!-- end snippet -->

答案4

得分: 0

以下解决方案假设第一个数组比第二个数组长。但如果长度始终相同,那么可以将 i &lt; 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})) );
英文:

The following solution assumes the first array is longer than the second. However, the lengths are always the same then i &lt; str.length ? i : i % str.length can be replaced by i.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

const
closed_status = [{
    &quot;project_no&quot;: 5,
    &quot;priority&quot;: 3,
    &quot;s_status&quot;: &quot;S8&quot;,
    &quot;project_Status&quot;: &quot;closed&quot;
  },
  {
    &quot;project_no&quot;: 8,
    &quot;priority&quot;: 1,
    &quot;s_status&quot;: &quot;S5&quot;,
    &quot;project_Status&quot;: &quot;closed&quot;
  },
  {
    &quot;project_no&quot;: 12,
    &quot;priority&quot;: 2,
    &quot;s_status&quot;: &quot;S2&quot;,
    &quot;project_Status&quot;: &quot;closed&quot;
  }
],
str = [
  &quot;Value 1&quot;,
  &quot;Value 2&quot;,
  &quot;Value 3&quot;,
],
result = closed_status
.map((status,i) =&gt; ({...status, newValue:str[i &lt; str.length ? i : i % str.length]}));

console.log( result );

//alternatively, (EQUAL LENGTHS)
console.log( str.map((newValue,i) =&gt; ({...closed_status[i],newValue})) );

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年6月12日 17:24:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/76455242.html
匿名

发表评论

匿名网友

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

确定