来自这样的对象:
{a:1, b: 2, c: 3}
我想转为
['a', 'b', 'b', 'c', 'c', 'c']
其中键是字符串,值是副本数,顺序无关紧要。
最好的方法是什么?
我曾考虑过使用array.fill,但不确定这是否真的比迭代和推送更容易。
编辑:当前位置:
const arr = []
_.each(obj, function (v, k) {
_.times(v, function () {
arr.push(k)
})
})
您可以将flatMap
、Object.entries
和fill
作为每种大小的数组。
const obj = { a: 1, b: 2, c: 3 };
const result = Object.entries(obj).flatMap(([k, v]) => Array(v).fill(k));
console.log(result)
或使用Lodash
const obj = { a: 1, b: 2, c: 3 };
const arr = _.flatMap(obj, (v,k) => Array(v).fill(k))
console.log(arr);
<script src="https://cdn.jsdelivr.net/npm/lodash@4.17.21/lodash.min.js"></script>
但没有什么能比得上一个简单的循环
const obj = { a: 1, b: 2, c: 3 };
const result = []
for (let [k, v] of Object.entries(obj)) {
while (v--) {
result.push(k)
}
}
console.log(result)
我会使用Object.keys
将对象转换为键数组,然后使用新创建的空结果数组,然后映射到键中。
对于每个键,我都会在现有结果中添加一个填充数组。
以下是ES6解决方案(不需要额外的库(
const obj = { a: 1, b: 2, c: 3 };
let result = []
Object.keys(obj).forEach(key => {
result = [...result, ...new Array(obj[key]).fill(key)]
})
console.log(result)
您可以按如下方式使用Object.entries
和Array#reduce
:
const input = {a:1, b: 2, c: 3};
const output = Object.entries(input).reduce(
(prev, [key,value]) => prev.concat( Array(value).fill(key) ),
[]
);
console.log( output );
或者,使用Array#push
而不是Array#concat
,
const input = {a:1, b: 2, c: 3};
const output = Object.entries(input).reduce(
(prev, [key,value]) => prev.push( ...Array(value).fill(key) ) && prev,
[]
);
console.log( output );
或者,使用for
循环,
const input = {a:1, b: 2, c: 3};
const output = [],
pairs = Object.entries(input);
for(let i = 0; i < pairs.length; i++) {
const [key, value] = pairs[i];
for(let j = 0; j < value; j++) {
output.push( key );
}
}
console.log( output );