簡體   English   中英

對對象數組內的對象數組求和

[英]sum array of objects inside an array of objects

我有一個對象數組,數組內部還有另一個對象數組,我想對這些值求和。 所以總和是基於同一個picker_id。 然后將 current_capacity、process_time_in_minutes 和 products 中的 picked_qty 相加,這是對象數組。 這是我的數據:

var arr = [
  {
    current_capacity: 6000,
    picker_id: "icQrHPuE2fMZslceSG6liwuRar92",
    process_time_in_minutes: 10,
    products: [
      {
        product_id: 1,
        picked_qty: 2
      },
      {
        product_id: 2,
        picked_qty: 3
      }
    ]
  },

  {
    current_capacity: 2500,
    picker_id: "icQrHPuE2fMZslceSG6liwuRar92",
    process_time_in_minutes: 20,
    products: [
      {
        product_id: 1,
        picked_qty: 10
      }
    ]
  },

  {
    current_capacity: 36000,
    picker_id: "WIRzfIZALeftRk3DRGvh4nBdxQV2",
    process_time_in_minutes: 15,
    products: [
      {
        product_id: 1,
        picked_qty: 2
      },
      {
        product_id: 2,
        picked_qty: 3
      }
    ]
  }
];

這是我的代碼:

  var res = arr.reduce((acc, obj) => {
  var existObj = acc.find((item) => item.picker_id === obj.picker_id);
  if (existObj) {
    let total_picked = obj.products.reduce((acc2, curr) => acc2 + curr);

    // console.log("total_picked", total_picked);
    existObj.current_capacity =
      existObj.current_capacity + obj.current_capacity;
    existObj.process_time_in_minutes =
      existObj.process_time_in_minutes + obj.process_time_in_minutes;

    existObj.total = existObj.total ? existObj.total : 0 + total_picked;
    return acc;
  }
  acc.push(obj);
  return acc;
}, []);

const formatted = res.map((el) => {
  return {
    picker_id: el.picker_id,
    total_volume: el.current_capacity,
    total_time: el.process_time_in_minutes,
    total_products: el.total
  };
});

結果如下:

[
 {
   picker_id: "icQrHPuE2fMZslceSG6liwuRar92"
   total_volume: 8500
   total_time: 30
   total_products: "0[object Object]"
 },
 {
   picker_id: "WIRzfIZALeftRk3DRGvh4nBdxQV2"
   total_volume: 36000
   total_time: 15
   total_products: undefined
  }
]

預期如下:

[
 {
   picker_id: "icQrHPuE2fMZslceSG6liwuRar92"
   total_volume: 8500
   total_time: 30
   total_products: 15
 },
 {
   picker_id: "WIRzfIZALeftRk3DRGvh4nBdxQV2"
   total_volume: 36000
   total_time: 15
   total_products: 5
  }
]

您的實施問題是,如果existObj沒有在您的acc中退出,您是直接推送obj而不是您需要首先從產品的內部數組中處理total

我已經更新了您的代碼,使其看起來更清晰且易於維護。

方法:

  1. 為每個保存計算數據的picker_id構建一個dict
  2. dict轉換為list
var result = arr.reduce((acc, obj) => {
  if (!acc[obj.picker_id]) {
    acc[obj.picker_id] = {
      total_volume: 0,
      total_time: 0,
      total_products: 0
    };
  }

  const selectedPicker = acc[obj.picker_id];
  const total_picked = obj.products.reduce((acc2, item) => acc2 + item.picked_qty, 0);

  selectedPicker.total_volume = selectedPicker.total_volume + obj.current_capacity;
  selectedPicker.total_time =
    selectedPicker.total_time + obj.process_time_in_minutes;
  selectedPicker.total_products = selectedPicker.total_products + total_picked;
  
  return acc;
}, {});


const formatted = Object.keys(result).reduce((acc, picker_id) => {
    acc.push({
    picker_id,
    ...result[picker_id]
  })
    return acc;
}, [])

console.log("formmated", formatted);

你也可以通過這個實現你的 output


    function getProductQty(arr){
      let total = 0;
      arr.forEach(prd => {
        total += prd.picked_qty
      })
      return total;
    }
    const result = arr.reduce((acc,product) => {
       if(!acc.hasOwnProperty(product.picker_id)){
          acc[product.picker_id] = {
            picker_id: product.picker_id,
            total_volume: product.current_capacity,
            total_time: product.process_time_in_minutes
       }    
             
      acc[product.picker_id].total_products = getProductQty(product.products);
      }else{
         acc[product.picker_id].total_volume = acc[product.picker_id].total_volume + product.current_capacity
         acc[product.picker_id].total_time = acc[product.picker_id].total_time + product.process_time_in_minutes
         acc[product.picker_id].total_products = acc[product.picker_id].total_products + getProductQty(product.products);
      }
          
         return acc
       },{})
        
        
console.log(Object.values(result),'result');

使用一點參數解構,我認為您可以在解決其他人描述的問題后進一步清理。 我的版本可能是這樣的:

 const extract = (xs) => Object.values (xs.reduce ( (acc, {current_capacity, picker_id, process_time_in_minutes, products}) => { const curr = acc [picker_id] || (acc [picker_id] = { picker_id, total_volume: 0, total_time: 0, total_products: 0 }) curr.total_volume += current_capacity curr.total_time += process_time_in_minutes curr.total_products += products.reduce ((t, p) => t + p.picked_qty, 0) return acc }, {} )) const arr = [{current_capacity: 6e3, picker_id: "icQrHPuE2fMZslceSG6liwuRar92", process_time_in_minutes: 10, products: [{product_id: 1, picked_qty: 2}, {product_id: 2, picked_qty: 3}]}, {current_capacity: 2500, picker_id: "icQrHPuE2fMZslceSG6liwuRar92", process_time_in_minutes: 20, products: [{product_id: 1, picked_qty: 10}]}, {current_capacity: 36e3, picker_id: "WIRzfIZALeftRk3DRGvh4nBdxQV2", process_time_in_minutes: 15, products: [{product_id: 1, picked_qty: 2}, {product_id: 2, picked_qty: 3}]}] console.log (extract (arr))
 .as-console-wrapper {max-height: 100%;important: top: 0}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM