在JS中转换对象数据



我有一个问题在JS中转换数据。

数据如下:

const data = [
{
"idPopulation": 1,
"namePopulation": "Population 1",
"idStrategy": 1,
"nameStrategy": "Strategy 1",
"priority": 1
},
{
"idPopulation": 1,
"namePopulation": "Population 1",
"idStrategy": 2,
"nameStrategy": "Strategy 2",
"priority": null
},
{
"idPopulation": 1,
"namePopulation": "Population 1",
"idStrategy": 3,
"nameStrategy": "Strategy 3",
"priority": 2
},
{
"idPopulation": 2,
"namePopulation": "Population 2",
"idStrategy": 1,
"nameStrategy": "Strategy 1",
"priority": null
},
{
"idPopulation": 2,
"namePopulation": "Population 2",
"idStrategy": 2,
"nameStrategy": "Strategy 2",
"priority": null
},
{
"idPopulation": 2,
"namePopulation": "Population 2",
"idStrategy": 3,
"nameStrategy": "Strategy 3",
"priority": 1
},
{
"idPopulation": 3,
"namePopulation": "Population 3",
"idStrategy": 1,
"nameStrategy": "Strategy 1",
"priority": 1
},
{
"idPopulation": 3,
"namePopulation": "Population 3",
"idStrategy": 2,
"nameStrategy": "Strategy 2",
"priority": 2
},
{
"idPopulation": 3,
"namePopulation": "Population 3",
"idStrategy": 3,
"nameStrategy": "Strategy 3",
"priority": 3
}
]

我需要将它转换成这个以便它可以在前端包中使用(https://devexpress.github.io/devextreme-reactive/react/grid/docs/guides/editing/):

const testRows = [
{
nameStrategy: 'Strategy 1',
priorityPopulation1: 1,
priorityPopulation2: null,
priorityPopulation3: 1,
}, 
{...}]

我试着减少和东西,但还没有弄清楚。到目前为止,我已经这样做了:

const result = data.reduce((groupedStrat, strat) => {
const stratName = strat.nameStrategie;
if(groupedStrat[stratName] == null) groupedStrat[stratName] = []
groupedStrat[stratName].push(strat)
return groupedStrat
}, {})
console.log(result).

让我知道我的问题是否清楚易懂。

谢谢!

对于您和其他新开发人员,我有一个建议:不要使用reduce。将问题分解为更简单的步骤:

  1. 您有一些数据
  2. 你想要一些从这些数据生成的NewData

要实现这一点,您必须读取旧数据并基于该信息创建新数据。旧的数据是数组格式,然后需要一个for循环。对于每个数组项,您必须读取属性并存储一些基于该属性的临时数据。在你有了所有这些临时数据之后,用新的格式化数据创建一个最终数组。

const data = [
{
"idPopulation": 1,
"namePopulation": "Population 1",
"idStrategy": 1,
"nameStrategy": "Strategy 1",
"priority": 1
},
{
"idPopulation": 1,
"namePopulation": "Population 1",
"idStrategy": 2,
"nameStrategy": "Strategy 2",
"priority": null
},
{
"idPopulation": 1,
"namePopulation": "Population 1",
"idStrategy": 3,
"nameStrategy": "Strategy 3",
"priority": 2
},
{
"idPopulation": 2,
"namePopulation": "Population 2",
"idStrategy": 1,
"nameStrategy": "Strategy 1",
"priority": null
},
{
"idPopulation": 2,
"namePopulation": "Population 2",
"idStrategy": 2,
"nameStrategy": "Strategy 2",
"priority": null
},
{
"idPopulation": 2,
"namePopulation": "Population 2",
"idStrategy": 3,
"nameStrategy": "Strategy 3",
"priority": 1
},
{
"idPopulation": 3,
"namePopulation": "Population 3",
"idStrategy": 1,
"nameStrategy": "Strategy 1",
"priority": 1
},
{
"idPopulation": 3,
"namePopulation": "Population 3",
"idStrategy": 2,
"nameStrategy": "Strategy 2",
"priority": 2
},
{
"idPopulation": 3,
"namePopulation": "Population 3",
"idStrategy": 3,
"nameStrategy": "Strategy 3",
"priority": 3
}];
//start reading and storing temp data 
var strategies = {};//here store by strategy
for( var i=0; i<data.length; i++){
var item = data[i];
if( !strategies[item.idStrategy] ){ //define the strategy
strategies[item.idStrategy] = [];
}
//add item to strategy
strategies[item.idStrategy].push(item);
}
//create converted data from temp data 
var convertedData = [];

for(var strategyId in strategies){
var items= strategies[strategyId];
var convertedItem = {
nameStrategy: items[0].nameStrategy //get it from the firs as all items have the same nameStrategy
//priorityPopulation1: null, dont define these yet
//priorityPopulation2: null,
//priorityPopulation3: null,
}
for( var j=0; j<items.length; j++){
var item = items[j];
convertedItem["priorityPopulation" + item.idPopulation] = item.priority;
}
convertedData.push(convertedItem );
}
console.log( convertedData );

相关内容

  • 没有找到相关文章

最新更新