我有下面的数组
var firstjson = [
{
"Week" : "Week 1",
"Product" : "Apple",
"Buy" : "24",
"Sell" : "20",
"Profit" : "100",
"Lost" : "20"
},
{
"Week" : "Week 1",
"Product" : "Egg",
"Buy" : "24",
"Sell" : "20",
"Profit" : "100",
"Lost" : "20"
},
{
"Week" : "Week 2",
"Product" : "Apple",
"Buy" : "44",
"Sell" : "10",
"Profit" : "10",
"Lost" : "200"
},
{
"Week" : "Week 2",
"Product" : "Milk",
"Buy" : "24",
"Sell" : "20",
"Profit" : "100",
"Lost" : "20"
}
]
我正试着像下面一样出去
var finalarray = [
{
"Week" : "Week 1",
"Apple_Buy" : "24",
"Apple_Sell" : "20",
"Apple_Profit" : "100",
"Apple_Lost" : "20",
"Egg_Buy" : "24",
"Egg_Sell" : "20",
"Egg_Profit" : "100",
"Egg_Lost" : "20"
},
{
"Week" : "Week 2",
"Apple_Buy" : "44",
"Apple_Sell" : "10",
"Apple_Profit" : "10",
"Apple_Lost" : "200",
"Milk_Buy" : "24",
"Milk_Sell" : "20",
"Milk_Profit" : "100",
"Milk_Lost" : "20"
},
]
我尝试先将周放入一个数组,然后循环遍历每个周并将值推送到新数组,如下所示
var Weeklist = [];
var dataSetarray = []
$.when(
$.each(data, function(i,item){
Weeklist.push(data[i]['Week'])
})
).then(function () {
var uniqueWeeklist = Weeklist.filter(function(itm, i, a) {
return i == Weeklist.indexOf(itm);
});
for(z = 0; z<uniqueWeeklist.length; z++){
$.when(
$.each(data, function(x,item){
if(data[x]['ATD/ETD/RETD Week'] == uniqueWeeklist[z]){
dataSetarray.push({
"year" : uniqueWeeklist[z],
})
}
})
).then(function () {
console.log(dataSetarray)
});
}
});
但不能继续之后,也许有一个镜头的方式,通过第一个阵列本身循环,并创建最终的阵列与出采取数周的另一个阵列?
2条答案
按热度按时间pqwbnv8z1#
这绝对是不需要jQuery也能做到的事情。
我会分两步来处理
1.将所有数据收集到一个对象中,其中键为
Week
值,值为一个以Product
为前缀的其余数据的对象1.将该对象转换为数组,并将
Week
键转换为值4ioopgfo2#
首先..抱歉我的代码很乱,但它应该足够简单,你可以理解.一些弱点,这是请确保您的第一周有独特的产品,这样就没有重复的鸡蛋,苹果和牛奶.