简体   繁体   中英

How to push an array of object as key value pair into an array

Lets say I am getting a JSON from an API like this

lineItem = {
  "rows": {
    "10010": [
      {
        "id": "[10010,1,1]",
        "po_item": 10010,
        "schedule_no": 1,
        "chainage_no": 1
      },
      {
        "id": "[10010,2,1]",
        "po_item": 10010,
        "schedule_no": 2,
        "chainage_no": 1
      }
    ],
    "10020": [
      {
        "id": "[10020,1,1]",
        "po_item": 10020,
        "schedule_no": 1,
        "chainage_no": 1
      },
      {
        "id": "[10020,2,1]",
        "po_item": 10020,
        "schedule_no": 2,
        "chainage_no": 1
      }
    ]
  }
}

I need to populate an array like below,

resultArray = [
  "10010": [
      {
          "id": "[10010,1,1]",
          "status": 0
      },
      {
          "id": "[10010,2,1]",
          "status": 0
      }
  ],
  "10020": [
      {
          "id": "[10020,1,1]",
          "status": 0
      },
      {
          "id": "[10020,2,1]",
          "status": 0
      }
  ]
]

I have tried with the following method

Object.keys(this.lineItem.rows).map(key => {
  this.resultArray[key] = [];

  this.lineItem.rows[key].map(item => {
    this.resultArray[key].push({ id: item.id, status: 0 });
  });
  
});

But I am getting result like below with empty elements

(10021) [empty × 10010, Array(2), empty × 9, Array(2)]
10010: (2) [{…}, {…}]
10020: (2) [{…}, {…}]
length: 10021
__proto__: Array(0)

here you can see empty x 10010 and empty x 9. when I try to send the resultArray as a payload to an API it is adding 10019 null and then two arrays.

You are using the key from the JSON as the index in your array. Try this.

Object.keys(this.lineItem.rows).forEach(key => {
  var obj = {};
  obj[key] = this.lineItem.rows[key];
  this.resultArray.push(obj);
});

 let lineItem = { "rows": { "10010": [ { "id": "[10010,1,1]", "po_item": 10010, "schedule_no": 1, "chainage_no": 1 }, { "id": "[10010,2,1]", "po_item": 10010, "schedule_no": 2, "chainage_no": 1 } ], "10020": [ { "id": "[10020,1,1]", "po_item": 10020, "schedule_no": 1, "chainage_no": 1 }, { "id": "[10020,2,1]", "po_item": 10020, "schedule_no": 2, "chainage_no": 1 } ] } }; let resultArray=Object.entries(lineItem.rows).map(([key,value])=>({[key]:value.map(v=>({id:v.id,status:0}))})); console.log(resultArray);

You can iterate each key of rows key and generate your result using array#map . Generate back the object using Object.fromEntries .

 const lineItem = { "rows": { "10010": [ { "id": "[10010,1,1]", "po_item": 10010, "schedule_no": 1, "chainage_no": 1 }, { "id": "[10010,2,1]", "po_item": 10010, "schedule_no": 2, "chainage_no": 1 } ], "10020": [ { "id": "[10020,1,1]", "po_item": 10020, "schedule_no": 1, "chainage_no": 1 }, { "id": "[10020,2,1]", "po_item": 10020, "schedule_no": 2, "chainage_no": 1 } ] } }, rows = Object.keys(lineItem.rows).map(key => [ [key], lineItem.rows[key].map(({id}) => ({id, status: 0})) ]), result = {"rows": Object.fromEntries(rows)}; console.log(result);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM