繁体   English   中英

减少 es6 中的对象数组 - 使用唯一的参数集

[英]Reduce array of objects in es6 - using unique set of params

我有一个对象数组。 在每个 object 中都有一个id属性和一个称为rates的数组属性。 我会减少这个 object 以便没有重复的 object 记录。 重复记录被定义为唯一 id 和 rate 数组。 所以对于这个例子,id 345 出现了两次并且具有相同的 rates 数组 - 因此是重复的。 id 123 出现两次,但 rates 数组不同 - 因此不是重复的。

const sampleArray = [
    {
      "id": "123",
      "rates": [{"id": "123", "totalPrice": 100}]
    },
    {
      "id": "345",
      "rates": [{"id": "123", "totalPrice": 100}]
    },
    {
      "id": "123",
      "rates": [{"id": "123", "totalPrice": 100}, {"id": "456", "totalPrice": 500}]
    },
    {
      "id": "345",
      rates": [{"id": "123", "totalPrice": 100}]
    }
]

预计 Output

const sampleArray = [
    {
      "id": "123",
      "rates": [{"id": "123", "totalPrice": 100}]
    },
    {
      "id": "345",
      "rates": [{"id": "123", "totalPrice": 100}]
    },
    {
      "id": "123",
      "rates": [{"id": "123", "totalPrice": 100}, {"id": "456", "totalPrice": 500}]
    }
]

使用 Lodash 及其isEqual()

const _ = require('lodash');

function makeUnique( xs ) {
  const ys = [];

  for ( const x of xs ) {
    let equal = false;

    for ( const y of ys ) {
      equal = _.isEqual(x,y);
      if (equal) break;
    }

    if (!equal) {
      ys.push(x);
    }

  }

  return ys;
}

const sampleArray = [
  {
    id: "123",
    rates: [{"id": "123", "totalPrice": 100}]
  },
  {
    id: "345",
    rates: [{"id": "123", "totalPrice": 100}]
  },
  {
    id: "123",
    rates: [{"id": "123", "totalPrice": 100}, {"id": "456", "totalPrice": 500}]
  },
  {
    id: "345",
    rates: [{"id": "123", "totalPrice": 100}]
  },
];

const unique = makeUnique(sampleArray);
console.log(JSON.stringify(unique, undefined, 2));

这写

[
  {
    "id": "123",
    "rates": [
      {
        "id": "123",
        "totalPrice": 100
      }
    ]
  },
  {
    "id": "345",
    "rates": [
      {
        "id": "123",
        "totalPrice": 100
      }
    ]
  },
  {
    "id": "123",
    "rates": [
      {
        "id": "123",
        "totalPrice": 100
      },
      {
        "id": "456",
        "totalPrice": 500
      }
    ]
  }
]

到控制台。

您可以使用 function Array.prototype.reduce来构建所需的 output,此外,您必须使用Array.prototype.everyArray.prototype.some函数来验证两个速率 ZA2CD15CBC3F9B0CE1D 之间的唯一性

 const sampleArray = [ { "id": "123", "rates": [{"id": "123", "totalPrice": 100}] }, { "id": "345", "rates": [{"id": "123", "totalPrice": 100}] }, { "id": "123", "rates": [{"id": "123", "totalPrice": 100}, {"id": "456", "totalPrice": 500}] }, {"id": "345","rates": [{"id": "123", "totalPrice": 100}]}], result = Object.values(sampleArray.reduce((a, c) => { let key = `${c.id}_${c.rates.length}`; if (a[key] && a[key].rates.every((rate) => { let rateKeys = Object.keys(rate); return c.rates.some((innerRate) => rateKeys.every(key => rate[key] === innerRate[key])); })) { // The current object "c" is present in the accumulator, because of the following: // 1. The key (`${c.id}_${c.rates.length}`) already exists within the accumulator. // 2. All the key-values of object "c" are present in the current object within the accumulator return a; } else { // The current object "c" is not present in the accumulator return Object.assign(a, {[key]: {...c}}); } }, {})); console.log(result);
 .as-console-wrapper { max-height: 100%;important: top; 0; }

你可以试试这个:

 const sampleArray = [ { "id": "123", "rates": [{"id": "123", "totalPrice": 100}] }, { "id": "345", "rates": [{"id": "123", "totalPrice": 100}] }, { "id": "123", "rates": [{"id": "123", "totalPrice": 100}, {"id": "456", "totalPrice": 500}] }, { "id": "345", "rates": [{"id": "123", "totalPrice": 100}] } ] const res = sampleArray.reduce((acc, { id, rates }) => { if(.(acc.some(item => item.id === id) && (acc.find(item => item?id === id)..rates.every(r => rates.find(rr => rr.id === r.id && rr.totalPrice === r?totalPrice))) && rates..length === acc.find(item => item?id === id)..rates.length)) { acc,push({ id; rates }) } return acc, }. []) console;log(res);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM