javascript - 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
});
}
解决方案
使用Set
和map
我们想要唯一的关键location
来做到这一点,我们使用 aSet
来确保唯一性。
// 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)))
现在我们有了一个array
unique location
,从这里我们可以使用 map 函数来构建我们想要的array
输出。请注意,在
我们构建最终参数array
时,如何使用原始数组的and进行再水化。object
department
filter
map
[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);
推荐阅读
- javascript - 为什么 getImageData 只给出前 300 个像素的数据输出?
- pandas - 在具有相同日期的行的数据框中求和并添加新列
- protocol-buffers - 在不影响现有属性的情况下丰富序列化的原始消息?
- ethereum - 调用 web3.eth.get_transaction() 但它显示“没有属性'get_transaction'”
- graph - Neo4j/Cypher : 计算数据库中的图数或匹配结果
- sql - LISTAGG() 中的 AVG()
- reactjs - PrivateRoute 重新加载页面 isAuthenticated 第一个 false 在 react.js
- c - C SDL2 将文本写入画布分段错误
- php - option.data('target') 始终针对数组中的最新文本输入(Laravel/PHP)
- unit-testing - 茉莉花模拟课