nodejs / javascript合并相同项目并设置最高/最低价格

时间:2018-12-01 18:07:38

标签: javascript node.js

我有这个数组,我想将它们堆叠起来,以至于我只一次拥有每一项,但是我需要其中的最高和最低价格。

那是我拥有的数组

[ { sku: '199',
name: 'FAMAS | God of War (Factory New)',
price: 2003892,
caseid: '7' },
{ sku: '199',
name: 'FAMAS | God of War (Minimal Wear)',
price: 1703308,
caseid: '7' },
{ sku: '199',
name: 'FAMAS | God of War (Field-Tested)',
price: 1345613,
caseid: '7' },
{ sku: '199',
name: 'FAMAS | God of War (Well-Worn)',
price: 1170684,
caseid: '7' },
{ sku: '199',
name: 'FAMAS | God of War (Battle-Scarred)',
price: 1112150,
caseid: '7' }]

您可以看到最低的“价格”是1112150,而最高的价格是2003892

那就是我想要的:

 [ { sku: '199',
name: 'FAMAS | God of War (Factory New)',
highestprice: 2003892,
lowestprice: 1112150,
caseid: '7' }]

我想要每个sku的最高“价格”和最低的“价格”。 像我上次发布的那样,组合成一个数组结果。

4 个答案:

答案 0 :(得分:1)

您可以缩小数组,并使用相同的sku查找相同的对象。然后取最小值和最大值。

var array = [ { sku: '199', name: 'FAMAS | God of War (Factory New)', price: 2003892, caseid: '7' }, { sku: '199', name: 'FAMAS | God of War (Minimal Wear)', price: 1703308, caseid: '7' }, { sku: '199', name: 'FAMAS | God of War (Field-Tested)', price: 1345613, caseid: '7' }, { sku: '199', name: 'FAMAS | God of War (Well-Worn)', price: 1170684, caseid: '7' }, { sku: '199', name: 'FAMAS | God of War (Battle-Scarred)', price: 1112150, caseid: '7' }],
    result = array.reduce((r, { sku, name, price, caseid }) => {
        var temp = r.find(o => sku === o.sku);
        if (!temp) {
            return r.concat({ sku, name, highestprice: price, lowestprice: price, caseid });
        }
        temp.lowestprice = Math.min(price, temp.lowestprice);
        temp.highestprice = Math.max(price, temp.highestprice);
        return r;
    }, []);
    
console.log(result);

答案 1 :(得分:0)

此功能应为您提供所需的输出:

function findHiLo(arr, sku) {
  let min = +Infinity;
  let max = -Infinity;
  let max_index = 0;

  for(let i=0; i<arr.length; i++) {
    if(arr[i]["sku"] === sku) {
      if(arr[i]["price"] < min) min = arr[i]["price"];
      if(arr[i]["price"] > max) {
        max = arr[i]["price"];
        max_index = i;
      }
    }
  }

  return { "sku": sku,
           "name": arr[max_index]["name"],
           "highestprice": max,
           "lowestprice": min,
           "caseid": "7" };
}

它遍历整个数组,并查找具有给定“ sku”的项目。在此过程中,它将找到价格最高和最低的商品。

在像这样的数据上运行时:findHiLo(data, '199'),它将返回以下对象:

{ sku: "199", name: "FAMAS | God of War (Factory New)", highestprice: 2003892, lowestprice: 1112150, caseid: "7" }

从价格最高的商品复制名称。

我唯一不确定的是“ caseid”属性。我在此功能中将其设置为“ 7”,您可能需要更改它。

答案 2 :(得分:0)

在JavaScript数组(以及Java)中,有一种称为sort的方法。 看看documentation

以下是将其实现到您的示例中的方法:

let array = [ { sku: '199',
name: 'FAMAS | God of War (Factory New)',
price: 2003892,
caseid: '7' },
{ sku: '199',
name: 'FAMAS | God of War (Minimal Wear)',
price: 1703308,
caseid: '7' },
{ sku: '199',
name: 'FAMAS | God of War (Field-Tested)',
price: 1345613,
caseid: '7' },
{ sku: '199',
name: 'FAMAS | God of War (Well-Worn)',
price: 1170684,
caseid: '7' },
{ sku: '199',
name: 'FAMAS | The expensive God of War (Battle-Scarred)',
price: 111215000,
caseid: '7' }];

let sorted = array.sort(function(a, b) {
  return Math.sign(b.price - a.price);
  /* 
   * This compares the prices. Sign returns 1, if
   * the value is positive, -1 when negative and zero
   * when 0 is given.
   */
});

// OUTPUT
document.body.appendChild(document.createTextNode(
  JSON.stringify(sorted)
));

答案 3 :(得分:0)

我并没有真正理解堆栈的含义,但是根据我对问题的理解,您可以尝试一下

    let highestPrice = 0;
    let lowestPrice = 0;
    let sku = ' ';
    let caseID = ' ';
    let name = ' ';

    let newObj = {};

    // Let's call your array *arr*, you could do this

    arr.map((elem, index) => {
     if (elem.price > highestPrice ) {
       highestPrice = elem.price;
       sku = elem.sku;
       caseID = elem.caseid;
       name = elem.name;
     }
    if (elem.price < lowestPrice ) {
       lowestPrice = elem.price;
     }
   });

   newObj = {
     sku: sku, 
     highestprice: highestPrice, 
     lowestprice: lowestPrice,
     caseid: caseID;
     name: name
   }