skip to Main Content

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

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

…and this is the result i get

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

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

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

Anyone knows where my mistake is?

Thanks in advance

4

Answers


  1. 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 < closed_status.length; i++) {
        closed_status[i].newValue = str[i]
    }
    
    Login or Signup to reply.
  2. 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:

    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)
    Login or Signup to reply.
  3. 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.

    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);
    Login or Signup to reply.
  4. The following solution assumes the first array is longer than the second. However, the lengths are always the same then i < str.length ? i : i % str.length can be replaced by 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})) );
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search