我需要以特定方式对数据进行排序,过滤和存储。我将通过展示来解释。这是数据:
var pieData, cakeData, icecreamData;
var desserts = [
{
pies: [
{
name: "blueberry",
count: 3
},
{
name: "pumpkin",
count: 6
},
{
name: "apple",
count: 9
}
],
cakes: [
{
name: "chocolate",
count: 3
},
{
name: "foam",
count: 6
},
{
name: "wedding",
count: 9
}
],
icecream: [
{
name: "chocolate",
count: 3
},
{
name: "strawberry",
count: 6
},
{
name: "mint-chip",
count: 9
}
],
date: "2016-01-06T00:00:00"
},
{
pies: [
{
name: "blueberry",
count: 2
},
{
name: "pumpkin",
count: 4
},
{
name: "apple",
count: 6
}
],
cakes: [
{
name: "chocolate",
count: 2
},
{
name: "foam",
count: 4
},
{
name: "wedding",
count: 6
}
],
icecream: [
{
name: "chocolate",
count: 2
},
{
name: "strawberry",
count: 4
},
{
name: "mint-chip",
count: 6
}
],
date: "2016-01-07T00:00:00"
},
{
pies: [
{
name: "blueberry",
count: 4
},
{
name: "pumpkin",
count: 8
},
{
name: "apple",
count: 12
}
],
cakes: [
{
name: "chocolate",
count: 4
},
{
name: "foam",
count: 8
},
{
name: "wedding",
count: 12
}
],
icecream: [
{
name: "chocolate",
count: 4
},
{
name: "strawberry",
count: 8
},
{
name: "mint-chip",
count: 12
}
],
date: "2016-01-08T00:00:00"
}
];
所以我得到了我的数据。数据基本上是什么类型的馅饼,蛋糕和冰淇淋,其数量,名称和数量可能不同。甜点中的每个对象都是一天,日期是最后一个属性。我会直接找到我想要摆脱的东西,然后再进一步解释。以下是我需要摆脱它:
pieData = [
{
name: "blueberry",
dates: ["2016-01-06T00:00:00", "2016-01-07T00:00:00", "2016-01-08T00:00:00"],
counts: [3, 2, 4]
},
{
name: "pumpkin",
dates: ["2016-01-06T00:00:00", "2016-01-07T00:00:00", "2016-01-08T00:00:00"],
counts: [6, 4, 8]
},
{
name: "apple",
dates: ["2016-01-06T00:00:00", "2016-01-07T00:00:00", "2016-01-08T00:00:00"],
counts: [9, 6, 12]
}
];
cakeData = [
{
name: "chocolate",
dates: ["2016-01-06T00:00:00", "2016-01-07T00:00:00", "2016-01-08T00:00:00"],
counts: [3, 2, 4]
},
{
name: "foam",
dates: ["2016-01-06T00:00:00", "2016-01-07T00:00:00", "2016-01-08T00:00:00"],
counts: [6, 4, 8]
},
{
name: "wedding",
dates: ["2016-01-06T00:00:00", "2016-01-07T00:00:00", "2016-01-08T00:00:00"],
counts: [9, 6, 12]
}
];
icecreamData = [
{
name: "chocolate",
dates: ["2016-01-06T00:00:00", "2016-01-07T00:00:00", "2016-01-08T00:00:00"],
counts: [3, 2, 4]
},
{
name: "strawberry",
dates: ["2016-01-06T00:00:00", "2016-01-07T00:00:00", "2016-01-08T00:00:00"],
counts: [6, 4, 8]
},
{
name: "mint-chip",
dates: ["2016-01-06T00:00:00", "2016-01-07T00:00:00", "2016-01-08T00:00:00"],
counts: [9, 6, 12]
}
];
所以我需要将desserts变量传递给一个函数并让它将原始数据顶部声明的pieData,cakeData和icecreamData变量设置为我在第二个中显示的对象数组一点代码。
需要注意的几点:
在输出数据中,值对应于原始数据中最初分配的天数。例如,pieData [0] .dates [0]对应于原始数据中出现的pieData [0] .counts [0]。
每种甜点可能有无限种类,也可能没有。意思是"馅饼"中可能有10个不同的对象。原始数据中的数组或无数据。但总会有馅饼,蛋糕和冰淇淋阵列,无论它们是空的还是有千个物体。
我不知道每种甜点会有什么类型。它可能是任何东西。所以馅饼可以命名为#34;南瓜"或者它可以被命名为#34;电池酸"。
我能想到的唯一解决方案是执行多个循环和嵌套循环以及整个代码太多。我知道必须有一些有效的巫术才能做到这一点。
答案 0 :(得分:1)
将其转换为您想要的格式
https://jsfiddle.net/sdhjL7dv/
var pieData = [], cakeData = [], icecreamData = [];
var desserts = [
{
pies: [
{
name: "blueberry",
count: 3
},
{
name: "pumpkin",
count: 6
},
{
name: "apple",
count: 9
}
],
cakes: [
{
name: "chocolate",
count: 3
},
{
name: "foam",
count: 6
},
{
name: "wedding",
count: 9
}
],
icecream: [
{
name: "chocolate",
count: 3
},
{
name: "strawberry",
count: 6
},
{
name: "mint-chip",
count: 9
}
],
date: "2016-01-06T00:00:00"
},
{
pies: [
{
name: "blueberry",
count: 2
},
{
name: "pumpkin",
count: 4
},
{
name: "apple",
count: 6
}
],
cakes: [
{
name: "chocolate",
count: 2
},
{
name: "foam",
count: 4
},
{
name: "wedding",
count: 6
}
],
icecream: [
{
name: "chocolate",
count: 2
},
{
name: "strawberry",
count: 4
},
{
name: "mint-chip",
count: 6
}
],
date: "2016-01-07T00:00:00"
},
{
pies: [
{
name: "blueberry",
count: 4
},
{
name: "pumpkin",
count: 8
},
{
name: "apple",
count: 12
}
],
cakes: [
{
name: "chocolate",
count: 4
},
{
name: "foam",
count: 8
},
{
name: "wedding",
count: 12
}
],
icecream: [
{
name: "chocolate",
count: 4
},
{
name: "strawberry",
count: 8
},
{
name: "mint-chip",
count: 12
}
],
date: "2016-01-08T00:00:00"
}
];
for(var i = 0; i < desserts.length; i++) {
var d = desserts[i].date;
desserts[i].pies.length && save(pieData, desserts[i].pies, d);
desserts[i].cakes.length && save(cakeData, desserts[i].cakes, d);
desserts[i].icecream.length && save(icecreamData, desserts[i].icecream, d);
}
function save(destination, items, d) {
for(var i = 0; i < items.length; i++) {
var name = items[i].name;
var count = items[i].count;
if(destination[name] === undefined) { destination[name] = {name:'',dates:[],counts:[]}; }
destination[name].name = name;
destination[name].dates.push(d);
destination[name].counts.push(count);
}
}
console.log(pieData);
console.log(cakeData);
console.log(icecreamData);
答案 1 :(得分:1)
我理解想要“缩小”代码的驱动力,但我认为最好保持可读性。尽管你有所保留,我认为在这种情况下,一些嵌套循环是有意义的。
考虑以下获得所需结果的方法:
var pieData, cakeData, icecreamData;
var desserts = [{pies:[{name:"blueberry",count:3},{name:"pumpkin",count:6},{name:"apple",count:9}],cakes:[{name:"chocolate",count:3},{name:"foam",count:6},{name:"wedding",count:9}],icecream:[{name:"chocolate",count:3},{name:"strawberry",count:6},{name:"mint-chip",count:9}],date:"2016-01-06T00:00:00"},{pies:[{name:"blueberry",count:2},{name:"pumpkin",count:4},{name:"apple",count:6}],cakes:[{name:"chocolate",count:2},{name:"foam",count:4},{name:"wedding",count:6}],icecream:[{name:"chocolate",count:2},{name:"strawberry",count:4},{name:"mint-chip",count:6}],date:"2016-01-07T00:00:00"},{pies:[{name:"blueberry",count:4},{name:"pumpkin",count:8},{name:"apple",count:12}],cakes:[{name:"chocolate",count:4},{name:"foam",count:8},{name:"wedding",count:12}],icecream:[{name:"chocolate",count:4},{name:"strawberry",count:8},{name:"mint-chip",count:12}],date:"2016-01-08T00:00:00"}];
var dessertData= {};
for (var dessertGroup of desserts) {
for (var item in dessertGroup) {
// the timestamp is not a dessert, so skip it
if (item === 'date') { continue; }
if (!dessertData[item]) {
// this is a new kind of dessert, add it
dessertData[item] = [];
}
for (var flavour of dessertGroup[item]) {
// get the index of the flavour
var index = dessertData[item].map(function(e) { return e.name; }).indexOf(flavour.name);
if (index < 0) {
// this is a new flavour of dessert, add it to the dessert type
dessertData[item].push({
name: flavour.name,
dates: [],
counts: []
});
index = dessertData[item].length - 1;
}
// append the relevant data to the flavour properties
dessertData[item][index].dates.push(dessertGroup.date);
dessertData[item][index].counts.push(flavour.count);
}
}
}
// finally, we want 'cakes' in 'cakeData'
// 'pies' in 'pieData'
// and 'icecream in 'icecreamData'
cakeData = dessertData.cakes;
pieData = dessertData.pies;
icecreamData = dessertData.icecream;
console.log("cakeData=", cakeData);
console.log("pieData=", pieData);
console.log("icecreamData=", icecreamData);
这很容易阅读和修改。此外,它允许任何类型的甜点!为什么要限制自己吃馅饼,蛋糕和冰淇淋。
您会注意到我通过dessertData
这样的内容动态创建和访问dessertData[propertyName]
的属性。
也许你知道这是可能的,但我在学习[]
语法不仅仅是数字索引之前已经使用了很长时间的javascript。祝你好运!