简体   繁体   中英

Creating a array of Objects in Javascript

I nee to create an array of Object(s) based on the response which i get,

Here is the Object which i need to create

this later will be used to show in a table in React JS.

const data = [
    {
      index: Math.floor(Math.random() * 10000) + 1,
      schudule_number: "",
      scheduleBudget: "",
      budgetedqty: "",
      paidQty: "",
      ... and some more to come
    }
  ];

Response which i am getting

const result = [ { schudule_number: '11010',
    scheduleBudget: 1598000,
    budgetedqty: 2000,
    paidQty: 3 }, 
    { schudule_number: '11020',
    scheduleBudget: 1000,
    budgetedqty: 20,
    paidQty: 18 } ];

How do i insert result to data objects?

(result).forEach(element => {
    data.forEach(e => {
        data.schudule_number === result.schudule_number;
    });
});

Expected Output:

[ { index : 2345,
    schudule_number: '11010',
    scheduleBudget: 1598000,
    budgetedqty: 2000,
    paidQty: 3 }, 
    { index : 2343, 
    schudule_number: '11020',
    scheduleBudget: 1000,
    budgetedqty: 20,
    paidQty: 18 } ];

How to assign values?

try this

result.forEach(element => {
    // add index and  rest of original object with spread operatir.
    var newEl = {index: 1, ...element}
    // use new object with index
    console.log(newEl)
});

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