从具有多个副本的对象创建数组的最佳方法

发布于 2025-01-20 19:00:56 字数 355 浏览 2 评论 0 原文

从这样的对象:

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

From an object like this:

{a:1, b: 2, c: 3}

I would like to turn into

['a', 'b', 'b', 'c', 'c', 'c']

Where the key is the string and the value is the number of copies, order doesn't matter.

What's the best way to do this?

I was thinking about using array.fill but not sure if that's actually easier than just iterating and push.

Edit: Currently this:

const arr = []
_.each(obj, function (v, k) {
  _.times(v, function () {
    arr.push(k)
  })
})

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

So尛奶瓶 2025-01-27 19:00:56

您可以 object。条目 填充 每个大小的数组。

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/[email protected]/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)

You could flatMap the Object.entries and fill an array of each size.

const obj = { a: 1, b: 2, c: 3 };
const result = Object.entries(obj).flatMap(([k, v]) => Array(v).fill(k));

console.log(result)

or with 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/[email protected]/lodash.min.js"></script>

But there's nothing like a simple loop

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)

空城仅有旧梦在 2025-01-27 19:00:56

我将使用 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)

I would convert the object into an array of keys using Object.keys and then use a newly created empty results array, then map through the keys.

For each key I would add a fill array to the existing results.

Here's the ES6 solution to that (no extra libraries required)

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)

感情洁癖 2025-01-27 19:00:56

您可以使用 Object.entriesArray#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 );

You can use Object.entries and Array#reduce as follows:

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

Or, using Array#push instead of 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 );

Or, using for loops,

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文