将对象数组转换为嵌套对象数组

时间:2019-02-13 14:04:38

标签: javascript

实际上我有一系列要转换的对象。

[  
   { "val1":"2.0" },
   { "val2":"2.0" },
   { "val3":"2.0" },
   { "val1":"4.0" },
   { "val2":"2.0" },
   { "val3":"4.7" },
   { "val1":"4.0" },
   { "val2":"4.0" },
   { "val3":"4.0" }
]

我应该把每三个项目放在一个带有父项目的单独数组中。

我会将其转换为这种格式

[  
   [  
      "20190201",
      [  
         { "val1":"2.0" },
         { "val2":"2.0" },
         { "val3":"2.0" }
      ]
   ],
   [  
      "20190202",
      [  
         { "val2":"2.0" },
         { "val3":"2.0" },
         { "val1":"4.0" }
      ]
   ]
]

实际上这就是我的方式

const returnData= fileDateName.map((_fileDate: string, _index: number) => {
        return [_fileDate, [
            filteredData[_index],
            filteredData[_index + 1],
            filteredData[_index + 2],
        ]];
    });

我的实际问题是我的输出不采用下三个值,但是每次移动一个项目然后采用下一个值。我认为这是因为_index的值没有按预期增加。

Index =0 
filteredData[0],
filteredData[1],
filteredData[2],
index=1
filteredData[1],
filteredData[2],
filteredData[3],
...

但是迭代应该完成

Index =0 
filteredData[0],
filteredData[1],
filteredData[2],
index=3
filteredData[3],
filteredData[4],
filteredData[5],
...

修改

FileDateName= ["20190201","20190202","20190203"]

如何在每次迭代中设置索引值?

4 个答案:

答案 0 :(得分:2)

您可以使用Array#map和Array#slice做类似的事情。取日期的索引,然后乘以3,以找到应该取数据的起始位置。

const dates = [20190201, 20190202, 20190203]
const data=[{"val1":"2.0"},{"val2":"2.0"},{"val3":"2.0"},{"val1":"4.0"},{"val2":"2.0"},{"val3":"4.7"},{"val1":"4.0"},{"val2":"4.0"},{"val3":"4.0"}]

const res = dates.map((date, index)=>{
  const start = index * 3;
  const end = start + 3;
  return [
    date,
    data.slice(start, end)
  ]
});

console.log(res);

答案 1 :(得分:1)

尝试这个:

df <- data.frame('variable'=rep(c('control','treatment'),each=20),
'value'=c(runif(20, min=0, max=3), rnorm(60)))

ggtest<-ggplot(df,aes(variable, value)) +
geom_beeswarm(priority='random',cex=2.5)
ggtest

答案 2 :(得分:0)

这是一个相当容易的任务。

只需将数组简化为三个项目大小的块数组即可。在此过程中,使用模块化算法确定何时需要启动新组。

var data = [  
   { "val1": "2.0" },
   { "val2": "2.0" },
   { "val3": "2.0" },
   { "val1": "4.0" },
   { "val2": "2.0" },
   { "val3": "4.7" },
   { "val1": "4.0" },
   { "val2": "4.0" },
   { "val3": "4.0" }
];

console.log(groupData(data, 3, new Date(2019, 1, 1)));

function formatDate(d) {
  return [
    d.getFullYear(),
    ('00' + (d.getMonth() + 1)).substr(-2),
    ('00' + d.getDate()).substr(-2),
  ].join('');
}

function groupData(data, size, startDate) {
  return data.reduce((result, item, index) => {
    if (index % size === 0) {
      result.push([ formatDate(startDate), [ item ] ]); // Begin a new group
      startDate.setDate(startDate.getDate() + 1); // Increment the day by 1
    } else {
      result[result.length - 1][1].push(item); // Add item to the last group
    }
    return result;
  }, []);
}
.as-console-wrapper { top: 0; max-height: 100% !important; }

答案 3 :(得分:0)

您可以使用reduce进行此操作,方法是遍历dates数组并将带有日期的3个项目推到ouptut数组:

const data = [  
   { "val1":"2.0" },
   { "val2":"2.0" },
   { "val3":"2.0" },
   { "val1":"4.0" },
   { "val2":"2.0" },
   { "val3":"4.7" },
   { "val1":"4.0" },
   { "val2":"4.0" },
   { "val3":"4.0" }
];

const dates = ['20190101', '20190201', '20190301'];

const result = dates.reduce((acc, date, i) => {
  acc.push([date, [data[i * 3], data[i * 3 + 1], data[i * 3 + 2]]]);
  return acc;
}, []);

console.log(result);

相关问题