在 JavaScript 中有效地将 2D 数组转换为删除重复项的对象
Efficiently converting 2D array to an object erasing duplicates in JavaScript
例如,如果有以下二维数组(排列为 [[key, value], [key, value]]):
var input_array = [
["one", "stuff"],
["two", "things"],
["two", "more things"],
["three", "stuff"],
["one", "something"],
["one", "stuff"],
["two", "more things"],
["three", "more stuff"]
]
使用 ES5/6 必须提供的(与带有计数器的 for 循环相反)创建输出对象的最小且有效的方法是什么按字母顺序排列的值:
var output_obj = {
"one": ["something", "stuff"],
"two": ["more things", "things"],
"three": ["more stuff", "stuff"]
}
您无法订购对象,但是...
const input_array = [
["one", "stuff"],
["two", "things"],
["two", "more things"],
["three", "stuff"],
["one", "something"],
["one", "stuff"],
["two", "more things"],
["three", "more stuff"]
];
let obj = {}, p, v;
for(let a of input_array){
p = a[0]; v = a[1];
if(obj.hasOwnProperty(p)){
if(obj[p].indexOf(v) === -1)obj[p].push(v);
}
else{
obj[p] = [v];
}
}
for(let i in obj){
obj[i].sort();
}
console.log(obj);
也许你可以先创建字符串到列表(数组)的映射,然后再对它们进行排序。
const fn = (arr) => {
const results = arr.reduce((acc, curr) => {
acc[curr[0]] = acc[curr[0]] || [];
if (!acc[curr[0]].includes(curr[1])) {
acc[curr[0]].push(curr[1]);
}
return acc;
}, {});
Object.values(results).forEach(vals => vals.sort());
return results;
}
console.log(fn([
["one", "stuff"],
["two", "things"],
["two", "more things"],
["three", "stuff"],
["one", "something"],
["one", "stuff"],
["two", "more things"],
["three", "more stuff"]
]));
一种有效的 ES6 消除重复项的方法是使用 Set
.
const input_array = [
['one', 'stuff'],
['two', 'things'],
['two', 'more things'],
['three', 'stuff'],
['one', 'something'],
['one', 'stuff'],
['two', 'more things'],
['three', 'more stuff']
];
// create object containing sets
const output_obj = input_array
.reduce((sets, [key, val]) => {
sets[key] = sets[key] || new Set();
sets[key].add(val);
return sets;
}, {});
// convert each set to a sorted array
Object.entries(output_obj)
.forEach(([key, set]) => {
output_obj[key] = [...set].sort();
});
console.log('output_obj:', output_obj);
这可以像使用 logical nullish assignment (??=), adding elements to a Set()
to handle duplicates, and then iterating the resulting Object.keys()
to convert the Set
s to arrays using spread syntax 的单行 reduce 一样简洁,同时还对它们进行排序。
const input = [["one", "stuff"], ["two", "things"], ["two", "more things"], ["three", "stuff"], ["one", "something"], ["one", "stuff"], ["two", "more things"], ["three", "more stuff"]];
const
output = input.reduce((a, [k, v]) => ((a[k] ??= new Set()).add(v), a), {});
Object.keys(output).forEach(k => output[k] = [...output[k]].sort());
console.log(output)
或者如果您需要避免 compatibility...
的逻辑空赋值
const input = [["one", "stuff"], ["two", "things"], ["two", "more things"], ["three", "stuff"], ["one", "something"], ["one", "stuff"], ["two", "more things"], ["three", "more stuff"]];
const
output = input.reduce((a, [k, v]) => ((a[k] = a[k] || new Set()).add(v), a), {});
Object.keys(output).forEach(k => output[k] = [...output[k]].sort());
console.log(output)
例如,如果有以下二维数组(排列为 [[key, value], [key, value]]):
var input_array = [
["one", "stuff"],
["two", "things"],
["two", "more things"],
["three", "stuff"],
["one", "something"],
["one", "stuff"],
["two", "more things"],
["three", "more stuff"]
]
使用 ES5/6 必须提供的(与带有计数器的 for 循环相反)创建输出对象的最小且有效的方法是什么按字母顺序排列的值:
var output_obj = {
"one": ["something", "stuff"],
"two": ["more things", "things"],
"three": ["more stuff", "stuff"]
}
您无法订购对象,但是...
const input_array = [
["one", "stuff"],
["two", "things"],
["two", "more things"],
["three", "stuff"],
["one", "something"],
["one", "stuff"],
["two", "more things"],
["three", "more stuff"]
];
let obj = {}, p, v;
for(let a of input_array){
p = a[0]; v = a[1];
if(obj.hasOwnProperty(p)){
if(obj[p].indexOf(v) === -1)obj[p].push(v);
}
else{
obj[p] = [v];
}
}
for(let i in obj){
obj[i].sort();
}
console.log(obj);
也许你可以先创建字符串到列表(数组)的映射,然后再对它们进行排序。
const fn = (arr) => {
const results = arr.reduce((acc, curr) => {
acc[curr[0]] = acc[curr[0]] || [];
if (!acc[curr[0]].includes(curr[1])) {
acc[curr[0]].push(curr[1]);
}
return acc;
}, {});
Object.values(results).forEach(vals => vals.sort());
return results;
}
console.log(fn([
["one", "stuff"],
["two", "things"],
["two", "more things"],
["three", "stuff"],
["one", "something"],
["one", "stuff"],
["two", "more things"],
["three", "more stuff"]
]));
一种有效的 ES6 消除重复项的方法是使用 Set
.
const input_array = [
['one', 'stuff'],
['two', 'things'],
['two', 'more things'],
['three', 'stuff'],
['one', 'something'],
['one', 'stuff'],
['two', 'more things'],
['three', 'more stuff']
];
// create object containing sets
const output_obj = input_array
.reduce((sets, [key, val]) => {
sets[key] = sets[key] || new Set();
sets[key].add(val);
return sets;
}, {});
// convert each set to a sorted array
Object.entries(output_obj)
.forEach(([key, set]) => {
output_obj[key] = [...set].sort();
});
console.log('output_obj:', output_obj);
这可以像使用 logical nullish assignment (??=), adding elements to a Set()
to handle duplicates, and then iterating the resulting Object.keys()
to convert the Set
s to arrays using spread syntax 的单行 reduce 一样简洁,同时还对它们进行排序。
const input = [["one", "stuff"], ["two", "things"], ["two", "more things"], ["three", "stuff"], ["one", "something"], ["one", "stuff"], ["two", "more things"], ["three", "more stuff"]];
const
output = input.reduce((a, [k, v]) => ((a[k] ??= new Set()).add(v), a), {});
Object.keys(output).forEach(k => output[k] = [...output[k]].sort());
console.log(output)
或者如果您需要避免 compatibility...
的逻辑空赋值const input = [["one", "stuff"], ["two", "things"], ["two", "more things"], ["three", "stuff"], ["one", "something"], ["one", "stuff"], ["two", "more things"], ["three", "more stuff"]];
const
output = input.reduce((a, [k, v]) => ((a[k] = a[k] || new Set()).add(v), a), {});
Object.keys(output).forEach(k => output[k] = [...output[k]].sort());
console.log(output)