简体   繁体   中英

hiw push data into object of array of object

i wanna ask about how to push/replace data to objec of array of objects, sorry bout ma graamaar hehe. this my dummy data:

const dummyData = {
      id: 1,
      daerah: "Bandung",
      date:"1668790800000",
      kendaraan: [
        {rodaEmpat: 50},
        {rodaDua: 22},
        {truck: 30},
        {Bus: 70},
      ]
    }, {
      id: 2,
      daerah: "Tasik",
      date:"1668877200000",
      kendaraan: [
        {rodaEmpat: 80},
        {rodaDua: 15},
        {truck: 10},
        {Bus: 50},
      ]
    },

then this the variable data that i want push in

 data: {
        
        datasets: [
          {
            data: [50, 40, 80, 19],
            backgroundColor: ["#6225E3", "#E32525", "#E7A543", "#3EAFE0"],
          },
        ],
      },

i want push/override data.dataSets[0].data with value of dummyData.kendaraan (this data have to using map function first), the result have to become like below:

data: {
        
        datasets: [
          {
            data: [50, 22, 30, 70],
            backgroundColor: ["#6225E3", "#E32525", "#E7A543", "#3EAFE0"],
          },
        ],
      },

thanks for your helpinggg

const newArr = kendaraan.map(item => Object.values(item)[0])
//newArr = [50, 22, 30, 70]

it map function will help you

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