繁体   English   中英

ES6:如何将这个对象数组减少到唯一值?

[英]ES6: How do I reduce this array of objects to unique values?

我一直在旋转我的轮子太久试图解决这个问题,我准备把我的电脑扔出窗外。 我该如何减少这个数组:

const array = [
  {'location': "Plovdiv", 'department': "Finance"},
  {'location': "Plovdiv", 'department': "Client & Employee Support"},
  {'location': "Plovdiv", 'department': "Client & Employee Support"},
  {'location': "London", 'department': "Engineering"},
  {'location': "London", 'department': "Engineering"},
  {'location': "Plovdiv", 'department': "Engineering"}
];

对此:

{'location': "Plovdiv", 'department': ["Finance", "Client & Employee Support", "Engineering"]},
{'location': "London", 'department': ["Engineering"]},

我的目标是删除具有位置的对象中的重复数组,并将它们合并为一个键。 其中每个键都会有对应的部门作为一个列表。

编辑:我终于设法用普通的 ol' JS 解决了这个问题,但它很笨重,并且有很多循环,我可能可以用更现代的方法摆脱它们。

let locArray = [];
  let newGrouping = [];

  // For each location
  for (let i = 0; i < locations.length; i += 1) {
    // Check to see if the current locations is in the new locArray
    if (locArray.indexOf(locations[i].location) === -1) {
      // Get in there!
      locArray.push(locations[i].location);
    }
  }

  // Loop through the new set of unique locations
  for (let i = 0; i < locArray.length; i += 1) {
    let depArray = [];

    // Loop through our original locations array
    for (let j = 0; j < locations.length; j += 1) {
      // Check to see if the current unique location matches the current location
      // AND make sure that it's not already in depArray
      if (locArray[i] === locations[j].location && depArray.indexOf(locations[j].department) === -1) {
        // Get in there!
        depArray.push(locations[j].department);
      }
    }

    // Push our current unique location and its unique departments into a new object
    newGrouping.push({
      'location': locArray[i],
      'departments': depArray
    });
  }

使用Setmap

我们想要唯一地键入location来做到这一点,我们使用Set来确保唯一性。

  // Notice we use the map function to pull just location. 
  new Set(array.map(({ location }) => location))

但是现在我们需要迭代这些唯一键并重建我们的数组。
所以我们将Set加载到一个Array

 const unique = new Array(...new Set(array.map(({ location }) => location)))

现在我们有一个唯一location array ,从这里我们可以使用 map 函数来构建我们想要的array输出。
请注意,当我们构建最终的object array时, department参数是如何使用原始数组的filtermap重新水化的。

  [Unique Location Array].map(location => ({ 
          location, // ES6 the property name it is inferred
          department: array.filter(({ location: l}) => location === l)
                            .map(({ department }) => department)
          }));

 const array = [ {'location': "Plovdiv", 'department': "Finance"}, {'location': "Plovdiv", 'department': "Client & Employee Support"}, {'location': "Plovdiv", 'department': "Client & Employee Support"}, {'location': "London", 'department': "Engineering"}, {'location': "London", 'department': "Engineering"}, {'location': "Plovdiv", 'department': "Engineering"} ]; const unique = new Array(...new Set(array.map(({ location }) => location))) .map(location => ({ location, department: array.filter(({ location: l}) => location === l) .map(({ department }) => department) })); console.log(unique);

暂无
暂无

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

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