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

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

Add values from Array to every Object in Array

问题

以下是代码部分的翻译:

  1. closed_status = [{
  2. "project_no": 5,
  3. "priority": 3,
  4. "s_status": "S8",
  5. "project_Status: closed": "closed"
  6. },
  7. {
  8. "project_no": 8,
  9. "priority": 1,
  10. "s_status": "S5",
  11. "project_Status: closed": "closed"
  12. },
  13. {
  14. "project_no": 12,
  15. "priority": 2,
  16. "s_status": "S2",
  17. "project_Status: closed": "closed"
  18. }
  19. ]
  20. str = [
  21. "Value 1",
  22. "Value 2",
  23. "Value 3",
  24. ]
  25. let result = []
  26. closed_status.forEach((obj) => {
  27. str.forEach((newValue) => {
  28. result.push({ ...obj,
  29. newValue
  30. })
  31. })
  32. })

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

英文:

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

  1. closed_status = [{
  2. &quot;project_no&quot;: 5,
  3. &quot;priority&quot;: 3,
  4. &quot;s_status&quot;: &quot;S8&quot;,
  5. &quot;project_Status: closed&quot;
  6. },
  7. {
  8. &quot;project_no&quot;: 8,
  9. &quot;priority&quot;: 1,
  10. &quot;s_status&quot;: &quot;S5&quot;,
  11. &quot;project_Status: closed&quot;
  12. },
  13. {
  14. &quot;project_no&quot;: 12,
  15. &quot;priority&quot;: 2,
  16. &quot;s_status&quot;: &quot;S2&quot;,
  17. &quot;project_Status: closed&quot;
  18. }
  19. ]
  20. str = [
  21. &quot;Value 1&quot;,
  22. &quot;Value 2&quot;,
  23. &quot;Value 3&quot;,
  24. ]
  25. let result = []
  26. closed_status.forEach((obj) =&gt; {
  27. str.forEach((newValue) =&gt; {
  28. result.push({ ...obj,
  29. newValue
  30. })
  31. })
  32. })

<!-- end snippet -->

...and this is the result i get

  1. result = [{
  2. &quot;project_no&quot; : 5,
  3. &quot;priority&quot; : 3,
  4. &quot;s_status&quot;: &quot;S8&quot;,
  5. &quot;project_Status: closed&quot;,
  6. &quot;newValue&quot;: &quot;Value 1&quot;
  7. },
  8. {
  9. &quot;project_no&quot; : 5,
  10. &quot;priority&quot; : 3,
  11. &quot;s_status&quot;: &quot;S8&quot;,
  12. &quot;project_Status: closed&quot;,
  13. &quot;newValue&quot;: &quot;Value 2&quot;
  14. },
  15. {
  16. &quot;project_no&quot; : 5,
  17. &quot;priority&quot; : 3,
  18. &quot;s_status&quot;: &quot;S8&quot;,
  19. &quot;project_Status: closed&quot;,
  20. &quot;newValue&quot;: &quot;Value 3&quot;
  21. },
  22. {
  23. &quot;project_no&quot; : 8,
  24. &quot;priority&quot; : 1,
  25. &quot;s_status&quot;: &quot;S5&quot;,
  26. &quot;project_Status: closed&quot;,
  27. &quot;newValue&quot;: &quot;Value 1&quot;,
  28. },
  29. {
  30. &quot;project_no&quot; : 8,
  31. &quot;priority&quot; : 1,
  32. &quot;s_status&quot;: &quot;S5&quot;,
  33. &quot;project_Status: closed&quot;,
  34. &quot;newValue&quot;: &quot;Value 2&quot;,
  35. }]

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

  1. {
  2. &quot;project_no&quot; : 5,
  3. &quot;priority&quot; : 3,
  4. &quot;s_status&quot;: &quot;S8&quot;,
  5. &quot;project_Status: closed&quot;,
  6. &quot;newValue&quot;: &quot;Value 1&quot;,
  7. },
  8. {
  9. &quot;project_no&quot; : 8,
  10. &quot;priority&quot; : 1,
  11. &quot;s_status&quot;: &quot;S5&quot;,
  12. &quot;project_Status: closed&quot;,
  13. &quot;newValue&quot;: &quot;Value 2&quot;,
  14. },
  15. {
  16. &quot;project_no&quot; : 12,
  17. &quot;priority&quot; : 2,
  18. &quot;s_status&quot;: &quot;S2&quot;,
  19. &quot;project_Status: closed&quot;,
  20. &quot;newValue&quot;: &quot;Value 3&quot;,
  21. },

Anyone knows where my mistake is?

Thanks in advance

答案1

得分: 1

以下是翻译好的部分:

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

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

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.

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

答案2

得分: 0

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

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

  1. closed_status = [{
  2. "project_no": 5,
  3. "priority": 3,
  4. "s_status": "S8",
  5. "project_Status": "closed",
  6. },
  7. {
  8. "project_no": 8,
  9. "priority": 1,
  10. "s_status": "S5",
  11. "project_Status": 'closed'
  12. },
  13. {
  14. "project_no": 12,
  15. "priority": 2,
  16. "s_status": "S2",
  17. "project_Status": "closed"
  18. }
  19. ]
  20. str = [
  21. "Value 1",
  22. "Value 2",
  23. "Value 3",
  24. ]
  25. closed_status.forEach((obj,index) => {
  26. obj.newValue = str[index]
  27. })
  28. 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 -->

  1. closed_status = [{
  2. &quot;project_no&quot;: 5,
  3. &quot;priority&quot;: 3,
  4. &quot;s_status&quot;: &quot;S8&quot;,
  5. &quot;project_Status&quot;: &quot;closed&quot;
  6. },
  7. {
  8. &quot;project_no&quot;: 8,
  9. &quot;priority&quot;: 1,
  10. &quot;s_status&quot;: &quot;S5&quot;,
  11. &quot;project_Status&quot;: &#39;closed&#39;
  12. },
  13. {
  14. &quot;project_no&quot;: 12,
  15. &quot;priority&quot;: 2,
  16. &quot;s_status&quot;: &quot;S2&quot;,
  17. &quot;project_Status&quot;: &quot;closed&quot;
  18. }
  19. ]
  20. str = [
  21. &quot;Value 1&quot;,
  22. &quot;Value 2&quot;,
  23. &quot;Value 3&quot;,
  24. ]
  25. closed_status.forEach((obj,index) =&gt; {
  26. obj.newValue = str[index]
  27. })
  28. console.log(closed_status)

<!-- end snippet -->

答案3

得分: 0

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

  1. closed_status = closed_status.map(e => ({ ...e, newValue: str.shift() }) );
  2. 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 -->

  1. closed_status = [
  2. {
  3. &quot;project_no&quot;: 5,
  4. &quot;priority&quot;: 3,
  5. &quot;s_status&quot;: &quot;S8&quot;,
  6. &quot;project_Status&quot;: &quot;closed&quot;
  7. },
  8. {
  9. &quot;project_no&quot;: 8,
  10. &quot;priority&quot;: 1,
  11. &quot;s_status&quot;: &quot;S5&quot;,
  12. &quot;project_Status&quot;: &quot;closed&quot;
  13. },
  14. {
  15. &quot;project_no&quot;: 12,
  16. &quot;priority&quot;: 2,
  17. &quot;s_status&quot;: &quot;S2&quot;,
  18. &quot;project_Status&quot;: &quot;closed&quot;
  19. }
  20. ]
  21. str = [
  22. &quot;Value 1&quot;,
  23. &quot;Value 2&quot;,
  24. &quot;Value 3&quot;,
  25. ]
  26. closed_status = closed_status.map(e =&gt; ({ ...e, newValue: str.shift() }) );
  27. console.log(closed_status);

<!-- end snippet -->

答案4

得分: 0

以下解决方案假设第一个数组比第二个数组长。但如果长度始终相同,那么可以将 i &lt; str.length ? i : i % str.length 替换为 i

  1. const
  2. closed_status = [{
  3. "project_no": 5,
  4. "priority": 3,
  5. "s_status": "S8",
  6. "project_Status": "closed"
  7. },
  8. {
  9. "project_no": 8,
  10. "priority": 1,
  11. "s_status": "S5",
  12. "project_Status": "closed"
  13. },
  14. {
  15. "project_no": 12,
  16. "priority": 2,
  17. "s_status": "S2",
  18. "project_Status": "closed"
  19. }
  20. ],
  21. str = [
  22. "Value 1",
  23. "Value 2",
  24. "Value 3",
  25. ],
  26. result = closed_status
  27. .map((status,i) => ({...status, newValue:str[i < str.length ? i : i % str.length]}));
  28. console.log( result );
  29. //alternatively, (EQUAL LENGTHS)
  30. 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 -->

  1. const
  2. closed_status = [{
  3. &quot;project_no&quot;: 5,
  4. &quot;priority&quot;: 3,
  5. &quot;s_status&quot;: &quot;S8&quot;,
  6. &quot;project_Status&quot;: &quot;closed&quot;
  7. },
  8. {
  9. &quot;project_no&quot;: 8,
  10. &quot;priority&quot;: 1,
  11. &quot;s_status&quot;: &quot;S5&quot;,
  12. &quot;project_Status&quot;: &quot;closed&quot;
  13. },
  14. {
  15. &quot;project_no&quot;: 12,
  16. &quot;priority&quot;: 2,
  17. &quot;s_status&quot;: &quot;S2&quot;,
  18. &quot;project_Status&quot;: &quot;closed&quot;
  19. }
  20. ],
  21. str = [
  22. &quot;Value 1&quot;,
  23. &quot;Value 2&quot;,
  24. &quot;Value 3&quot;,
  25. ],
  26. result = closed_status
  27. .map((status,i) =&gt; ({...status, newValue:str[i &lt; str.length ? i : i % str.length]}));
  28. console.log( result );
  29. //alternatively, (EQUAL LENGTHS)
  30. 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:

确定