jquery 从另一个阵列创建新阵列

kcrjzv8t  于 2023-03-29  发布在  jQuery
关注(0)|答案(2)|浏览(110)

我有下面的数组

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)    
                });
        }                
});

但不能继续之后,也许有一个镜头的方式,通过第一个阵列本身循环,并创建最终的阵列与出采取数周的另一个阵列?

pqwbnv8z

pqwbnv8z1#

这绝对是不需要jQuery也能做到的事情。
我会分两步来处理
1.将所有数据收集到一个对象中,其中键为Week值,值为一个以Product为前缀的其余数据的对象
1.将该对象转换为数组,并将Week键转换为值

// just your data minified
const 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"}];

// group
const groupByWeek = firstjson.reduce((map, { Week, Product, ...rest }) => {
  // initialise to an object if not set
  map[Week] ??= {};

  // merge in data
  Object.assign(
    map[Week],
    // map each key to be Product-prefixed
    ...Object.entries(rest).map(([key, val]) => ({
      [`${Product}_${key}`]: val,
    }))
  );

  return map;
}, {});

// convert to array
const finalarray = Object.entries(groupByWeek).map(([Week, data]) => ({
  Week,
  ...data,
}));

console.log(finalarray);
.as-console-wrapper { max-height: 100% !important; }
4ioopgfo

4ioopgfo2#

首先..抱歉我的代码很乱,但它应该足够简单,你可以理解.一些弱点,这是请确保您的第一周有独特的产品,这样就没有重复的鸡蛋,苹果和牛奶.

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 = []
var list = []
for(i = 0; i < firstjson.length; i++){
    list.push(firstjson[i].Week);
}

var unique = [...new Set(list)]

for(i = 0; i < unique.length; i++){
    tempjson = {};
    tempjson.Week = unique[i]
    for(j = 0; j < firstjson.length;j++){
        if(unique[i] == firstjson[j].Week){
            tempjson[firstjson[j].Product + "_Buy"] = firstjson[j].Buy
            tempjson[firstjson[j].Product + "_Sell"] = firstjson[j].Sell
            tempjson[firstjson[j].Product + "_Profit"] = firstjson[j].Profit
            tempjson[firstjson[j].Product + "_Lost"] = firstjson[j].Lost
        }
    }
    finalarray.push(tempjson);
}
console.log(finalarray)

相关问题