Javascript JS数据操作
我有这样一个数据:(真实的记录远远不止这些) JSON: 我想要这样的结果:(我想要每年,每种类型都有记录)Javascript JS数据操作,javascript,jquery,json,underscore.js,Javascript,Jquery,Json,Underscore.js,我有这样一个数据:(真实的记录远远不止这些) JSON: 我想要这样的结果:(我想要每年,每种类型都有记录) 有什么好办法吗?也欢迎下划线解决方案 希望这能在以下情况下起作用: var source = [{year: "2011", type: "A",...} ...]; var years = _.uniq(_.map(source , function(item){ return item.year; })) var types = _.uniq(_.map(source ,
有什么好办法吗?也欢迎下划线解决方案 希望这能在以下情况下起作用:
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)
香草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
});
您的数据是什么格式的?它是像这样的文本,您也需要一个解析器,还是它已经存在于某个对象或数组中?@JJW5432它是一个json数组,类似:[{year:2011,type:a:quantity:100},…]有什么区别?这种表格结构在两者中是相同的。请使用javasscript预期结果显示实际结构。也要展示你所需要的tried@charlietfl不同的是结果加上像{year:2011,type:B,quantity:-}这样的结果,我希望每年每个类型都有一个记录,但这不是它所显示的问题。。。恰恰相反。问题不清楚,也没有显示任何代码试图解决问题。是的,如果我们收集多年的所有密钥和类型并进行迭代,这应该是可行的。但我期待的是一个更好的方法。因为我在那里有很多唱片,我也需要考虑演出。如果没有人有更好的想法,我会把你的答案标记为接受答案。谢谢谢谢我认为这个答案更好。它肯定比lodash性能更好,因为lodash会导致比需要的数据循环多得多。也更容易阅读
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);
})
})
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);
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
});