首页 > 解决方案 > ES6:如何将这个对象数组简化为唯一值?

问题描述

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

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
    });
  }

标签: javascriptarraysecmascript-6

解决方案


使用Setmap

我们想要唯一的关键location来做到这一点,我们使用 aSet来确保唯一性。

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

但是现在我们需要迭代那些唯一的键并重建我们的数组。
所以我们加载SetArray

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

现在我们有了一个arrayunique location,从这里我们可以使用 map 函数来构建我们想要的array输出。请注意,在
我们构建最终参数array时,如何使用原始数组的and进行再水化。objectdepartmentfiltermap

  [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);
                                  
 


推荐阅读