如何在obj中使用for循环?

我有这个:

Alabama:
data: (28) [{…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}]
lat: "32.3182"
long: "-86.9023"
state: "Alabama"
__proto__: Object
Alaska: {state: "Alaska", lat: "61.3707", long: "-152.4044", data: Array(28)}
Alberta: {state: "Alberta", lat: "53.9333", long: "-116.5765", data: Array(2)}

So for each state we have soem values and data with values in it. One of the value inside that is date

我需要循环以推送到数组

var dataSetCopy = [];
for(var t = 0; t < initialDataSet.length; ++t) {
    var eachStateData = initialDataSet[t].data;
    // this is not working > console.log(eachStateData);
    dataSetCopy.push(eachStateData.date);
}
评论
  • kesse
    kesse 回复
    const dataSetCopy = [];
    intitialDataset.foreach(item => {
     const data = item.data; 
     data.foreach(obj => {
      dataSetCopy.push(obj.date)
     }) 
    })
    

    您需要循环两次。由于数据也是数组。我也建议使用foreach代替for。