数据转换
2019-09-29 本文已影响0人
章鱼要回家
数组转字符串
初始:
[
{
index:'1',
freqTX:'x1',
freqRX:'y1',
},
{
index:'2',
freqTX:'x2',
freqRX:'y2',
},
{
index:'3',
freqTX:'x3',
freqRX:'y3',
},
]
最终:
{
freqTX:'x1,x2,x3',
freqRX:'y1,y2,y3',
}
方法:
function convert (arr){
const obj = {};
arr.map(item=>{
if (obj.freqRX){
obj.freqTX += ','+item.freqTX;
obj.freqRX += ','+item.freqRX;
} else {
obj.freqTX = item.freqTX;
obj.freqRX = item.freqRX;
}
})
return obj;
}
字符串转数组
初始:
{
freqTX:'x1,x2,x3',
freqRX:'y1,y2,y3',
}
最终:
[
{
freqTX:'x1',
freqRX:'y1',
},
{
freqTX:'x2',
freqRX:'y2',
},
{
freqTX:'x3',
freqRX:'y3',
},
]
方法:
const freqTX0= data.freqTX.split(',');
const freqRX0= data.freqRX.split(',');
const dataSource0 = [];
for (let i=0; i<freqTX.length; i++) {
dataSource0.push({
freqTX:reqTX0[i],
freqRX:freqRX0[i],
});
}
console.log(dataSource0);
对象去重、重新命名key
初始:
// 将value1作为新obj的value,value2作为新obj的key,去除重复
{
value1:'1,2,12,13,6,4,5,11,22,23,9,10,24,25',
value2:'7,7,8,9,8,7,7,3,14,3,3,3,10,10',
}
最终:
{
8:'1,5,16,22,8',
1:'2,6',
6:'3,7',
7:'4,9,10',
15:'99,96',
11:'11,22,23',
12:'25,26',
}
方法:
function convert (obj){
var sector = obj.sector.split(',');
var nodeid = obj.nodeid.split(',');
var obj1 = {};
for(var i=0; i <nodeid.length; i++) {
var sector1 = sector[i];
var nodeid1 = nodeid[i];
if (obj1[sector1]){
obj1[sector1] += ','+nodeid1;
} else {
obj1[sector1]= nodeid1;
}
}
return(obj1);
}