在数组元素之间插入对象的最优雅方法是什么? [英] What is the most elegant way to insert objects between array elements?

查看:88
本文介绍了在数组元素之间插入对象的最优雅方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我肯定有很多方法可以实现这一目标,但是我正在寻找优雅"的东西.

I'm sure there are many ways to achieve that but I'm looking for something "elegant".

a = [
  'a',
  'b',
  'c'
];

magicArrayJoin(a, {value: 255} ); // insert the same object between each item

result ==  [
  'a',
  {value: 255},
  'b',
  {value: 255}
  'c'
];

欢迎所有建议.:)

推荐答案

Ramda 具有

使用元素之间的分隔符创建一个新列表.

Creates a new list with the separator interposed between elements.

代码:

R.intersperse({name: 'separator'}, ['one', 'two', 'three']);

结果:

[
    'one',
    {name: 'separator'},
    'two',
    {name: 'separator'},
    'three'
]

这篇关于在数组元素之间插入对象的最优雅方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆