JS数据操作
JS data manipulation
我有这样一个数据:(真实记录远不止这些)
year type quantity
2011 A 1000
2012 B 1000
2012 C 1000
2012 A 1000
2015 A 1000
JSON:
[{year:2011, type:A, quantity:1000},...]
我希望得到这样的结果:(我希望每年和每种类型都有记录)
year type quantity
2011 A 1000
2011 B -
2011 C -
2012 A 1000
2012 B 1000
2012 C 1000
2015 A 1000
2015 B -
2015 C -
请问有什么好的方法吗?也欢迎下划线解决方案!
希望这能与下划线一起使用:
var source = [{year: "2011", type: "A",...} ...];
var years = _.uniq(_.map(source , function(item){
return item.year;
}))
var types = _.uniq(_.map(source , function(item){
return item.type;
}))
var result = [];
_.each(years, function(yearItem){
_.each(types, function(typeItem){
var resultItem = _.find(source, function(item){
return item.year === yearItem && item.type === typeItem;
})
if(undefined === resultItem){
resultItem = {
year: yearItem,
type: typeItem,
quantity: "-"
};{}
}
result.push(resultItem);
})
})
带有jquery
的稍微优化的版本
var source = [{year:2011, type: 'A', quantity:100},{year:2012, type: 'B', quantity:200}],
product = [],
types = {},
years = {},
quantities = {};
$.each(source, function(i){
var type = source[i].type,
year = source[i].year;
types[type]= true;
years[year]= true;
if (typeof quantities[type] == "undefined")
quantities[type] = {};
quantities[type][year] = source[i].quantity;
});
$.each(types, function(type){
var qts = quantities[type];
$.each(years, function(year){
var value = "-";
if (typeof qts[year] != "undefined")
value = qts[year];
product.push({year:year, type: type, quantity:value});
});
});
console.log(product);
通过在一个循环中提取类型和年份来节省时间,并且不存储重复值(因此不需要 .unique)。
Vanilla JS,包括最终排序
var yrs = [], types = [], tmp={};
data.forEach(function(item){
// create arrays of years and types
if(yrs.indexOf(item.yr) === -1){
yrs.push(item.yr);
}
if(types.indexOf(item.type) === -1){
types.push(item.type);
}
// temp object used in next step to look for holes
tmp[item.yr + item.type] = true;
});
yrs.forEach(function(yr){
types.forEach(function(type){
// fill in holes
if(!tmp.hasOwnProperty(yr+type)){
data.push({yr: yr, type: type, qty: 0});
}
})
});
data.sort(function(a,b){
return b.yr === a.yr ? a.type > b.type : +a.yr - +b.yr
});
我有这样一个数据:(真实记录远不止这些)
year type quantity
2011 A 1000
2012 B 1000
2012 C 1000
2012 A 1000
2015 A 1000
JSON:
[{year:2011, type:A, quantity:1000},...]
我希望得到这样的结果:(我希望每年和每种类型都有记录)
year type quantity
2011 A 1000
2011 B -
2011 C -
2012 A 1000
2012 B 1000
2012 C 1000
2015 A 1000
2015 B -
2015 C -
请问有什么好的方法吗?也欢迎下划线解决方案!
希望这能与下划线一起使用:
var source = [{year: "2011", type: "A",...} ...];
var years = _.uniq(_.map(source , function(item){
return item.year;
}))
var types = _.uniq(_.map(source , function(item){
return item.type;
}))
var result = [];
_.each(years, function(yearItem){
_.each(types, function(typeItem){
var resultItem = _.find(source, function(item){
return item.year === yearItem && item.type === typeItem;
})
if(undefined === resultItem){
resultItem = {
year: yearItem,
type: typeItem,
quantity: "-"
};{}
}
result.push(resultItem);
})
})
带有jquery
的稍微优化的版本var source = [{year:2011, type: 'A', quantity:100},{year:2012, type: 'B', quantity:200}],
product = [],
types = {},
years = {},
quantities = {};
$.each(source, function(i){
var type = source[i].type,
year = source[i].year;
types[type]= true;
years[year]= true;
if (typeof quantities[type] == "undefined")
quantities[type] = {};
quantities[type][year] = source[i].quantity;
});
$.each(types, function(type){
var qts = quantities[type];
$.each(years, function(year){
var value = "-";
if (typeof qts[year] != "undefined")
value = qts[year];
product.push({year:year, type: type, quantity:value});
});
});
console.log(product);
通过在一个循环中提取类型和年份来节省时间,并且不存储重复值(因此不需要 .unique)。
Vanilla JS,包括最终排序
var yrs = [], types = [], tmp={};
data.forEach(function(item){
// create arrays of years and types
if(yrs.indexOf(item.yr) === -1){
yrs.push(item.yr);
}
if(types.indexOf(item.type) === -1){
types.push(item.type);
}
// temp object used in next step to look for holes
tmp[item.yr + item.type] = true;
});
yrs.forEach(function(yr){
types.forEach(function(type){
// fill in holes
if(!tmp.hasOwnProperty(yr+type)){
data.push({yr: yr, type: type, qty: 0});
}
})
});
data.sort(function(a,b){
return b.yr === a.yr ? a.type > b.type : +a.yr - +b.yr
});