【问题标题】:Node JS : JSON Refactoring based on the cityNode JS:基于城市的JSON重构
【发布时间】:2021-10-09 05:10:44
【问题描述】:

我有以下 JSON 对象:

    const data = [
    {
        "city name": "Chennai",
        "product name": "Apple",
        "no of qty": 2,
        "indvidual price": 50,
        "gst tax": 0.18
    },
    {
        "city name": "Chennai",
        "product name": "Samsung",
        "no of qty": 1,
        "indvidual price": 150,
        "gst tax": 0.18
    },
    {
        "city name": "Delhi",
        "product name ": "MIUI",
        "no of qty": 5,
        "indvidual price": 100,
        "gst tax": 0.18
    },
    {
        "city name": "Chennai",
        "product name": "Redmi",
        "indvidual price": 100,
        "no of qty": 1,
        "gst tax": 0.18
    },
    {
        "city name": "Delhi",
        "product name": "Realme",
        "indvidual price": 100,
        "no of qty": 5,
        "gst tax": 0.18
    },
];

我正在尝试根据城市和计算总体数量和成本进行分组。基于单个产品的总成本有不同的 GST,我们需要计算并添加到总价值中。

例外的JSON是这样的,

var result = [
    {
        "city": "chennai",
        "totalqty": 4, //Total qty based on the city
        "totalPrice": 354 //With the GST, Each product have separate gst(59+177+118)
    },
    {
        "city": "Delhi",
        "totalqty": 10, //Total qty based on the city
        "totalPrice": 236 //With the GST, Each product have separate gst(118+118)
    }
]

【问题讨论】:

    标签: javascript node.js arrays json reactjs


    【解决方案1】:

    您可以使用reduce轻松实现此结果

    const data = [{
        "city name": "Chennai",
        "product name": "Apple",
        "no of qty": 2,
        "indvidual price": 50,
        "gst tax": 0.18,
      },
      {
        "city name": "Chennai",
        "product name": "Samsung",
        "no of qty": 1,
        "indvidual price": 150,
        "gst tax": 0.18,
      },
      {
        "city name": "Delhi",
        "product name ": "MIUI",
        "no of qty": 5,
        "indvidual price": 100,
        "gst tax": 0.18,
      },
      {
        "city name": "Chennai",
        "product name": "Redmi",
        "indvidual price": 100,
        "no of qty": 1,
        "gst tax": 0.18,
      },
      {
        "city name": "Delhi",
        "product name": "Realme",
        "indvidual price": 100,
        "no of qty": 5,
        "gst tax": 0.18,
      },
    ];
    
    const dict = {
      city: "city name",
      qty: "no of qty",
      Price: "indvidual price",
      gst: "gst tax",
    };
    
    const result = data.reduce((acc, curr) => {
      const cityAlredyExist = acc.find((o) => o.city === curr[dict.city]);
      if (cityAlredyExist) {
        cityAlredyExist.totalqty += curr[dict.qty];
        cityAlredyExist.totalPrice +=
          curr[dict.Price] + curr[dict.Price] * curr[dict.gst];
      } else {
        acc.push({
          city: curr[dict.city],
          totalqty: curr[dict.qty],
          totalPrice: curr[dict.Price] + curr[dict.Price] * curr[dict.gst],
        });
      }
      return acc;
    }, []);
    
    console.log(result);
    /* This is not a part of answer. It is just to give the output fill height. So IGNORE IT */
    
    .as-console-wrapper {
      max-height: 100% !important;
      top: 0;
    }

    【讨论】:

    • 是的,兄弟,它可以工作,但是如果 json 中的城市像城市名称(中间有空格),我们该怎么办?
    • { "city name": "Delhi", "product name": "MIUI", "no of qty": 5, "individual price": 100, "gst tax": 0.18 } Like这个
    • @Naren 编辑答案,看看这是你想要的吗?
    • 请查看我编辑的问题。您编辑的答案显示为城市是城市名称,这不是预期结果
    【解决方案2】:

    reduce 在这种情况下很有用。然后,您可以使用Object.values 拉出与您的输出对应的新数组。

    如果您的 JSON 中有带空格的键,只需替换

    const { gst, price, qty } = rest;
    

    const { 'gst tax': gst, 'individual price': price, qty } = rest;
    

    这会分配一个与其余代码中的变量匹配的标签,因此您无需更改任何其他内容。

    const data=[{city:"Chennai",product:"Apple",qty:2,price:50,gst:.18},{city:"Chennai",product:"Samsung",qty:1,price:150,gst:.18},{city:"Delhi",product:"MIUI",qty:5,price:100,gst:.18},{city:"Chennai",product:"Redmi",price:100,qty:1,gst:.18},{city:"Delhi",product:"Realme",price:100,qty:5,gst:.18}];
    
    const out = data.reduce((acc, c, i, arr) => {
    
      // Destructure the city and product, and use `rest`
      // to destructure the rest
      const { city, product, ...rest } = c;
      const { gst, price, qty } = rest;
    
      // If the city doesn't exist on the object, create it
      // and set some default values
      acc[city] = (acc[city] || { city, product, qty: 0, price: 0 });
    
      // Update the quantity, and price
      acc[city].qty += qty;
      acc[city].price += gst * price + price;
    
      // Return the object for the next iteration
      return acc;
    }, {});
    
    // And then grab the Object.values
    console.log(Object.values(out));

    【讨论】:

      猜你喜欢
      • 2018-10-12
      • 1970-01-01
      • 1970-01-01
      • 2022-11-07
      • 1970-01-01
      • 1970-01-01
      • 2019-08-04
      • 2017-07-15
      • 2019-04-29
      相关资源
      最近更新 更多