代码之家  ›  专栏  ›  技术社区  ›  Enrique Moreno Tent

在数组中的所有元素之间插入值的函数方法

  •  5
  • Enrique Moreno Tent  · 技术社区  · 6 年前

    假设我有以下数组:

    const myArray = ["q", "w", "e", "r", "t", "y"]
    

    我想做的是在所有元素之间添加一个元素,如下所示:

    myArray.someMethod("XXX")
    // ["q", "XXX", "w", "XXX", "e", "XXX", "r", "XXX", "t", "XXX", "y"]
    

    在某种程度上,这有点 .join 是的,但我希望输出是另一个数组,而不是字符串。

    我知道如何使用循环来实现这一点,但我想知道的是实现这一点的“功能性”方法。

    6 回复  |  直到 6 年前
        1
  •  6
  •   Nina Scholz    6 年前

    您可以通过获取rest参数和检查rest数组的长度来采用递归方法,而不是迭代方法。

    const
        zip = ([a, ...r], v) => r.length ? [a, v, ...zip(r, v)] : [a];
    
    console.log(zip(["q", "w", "e", "r", "t", "y"], 'XXX'));
    .as-console-wrapper { max-height: 100% !important; top: 0; }
        2
  •  4
  •   TaoPR    6 年前

    在函数式编程中,数组是 Monad 这意味着它是平面的。 FlatMap 是一个一元绑定运算符的实现,对于数组,它将元素映射到一个新数组,并将它们放在一起。

    有了这个想法,您只需要在flatmap函数中向输出数组添加一个新值。见下文

    Array.prototype.someMethod = function(a){ return this.flatMap(i => [i,a]) }
    
    myArray.someMethod("XXX")
    // Array(12) [ "q", "XXX", "w", "XXX", "e", "XXX", "r", "XXX", "t", "XXX", … ]
    

    上面的例子在数组的末尾添加了xxx,因此我们可以使用flatmap的index参数忽略最后一个元素,如下所示

    Array.prototype.someMethod = function(a){ let L=this.length; return this.flatMap((n,i) => (i<L-1) ? [n,a] : [n]) }
    
    myArray.someMethod("XXX")
    // Array(11) [ "q", "XXX", "w", "XXX", "e", "XXX", "r", "XXX", "t", "XXX", "y"]
    
        3
  •  1
  •   Mohammad Usman    6 年前

    你可以使用 .map() 具有 .concat() :

    const data = ["q", "w", "e", "r", "t", "y"];
    const str = "XXX";
    
    const insertValue = (arr, val) => [].concat(
        ...arr.map((v, i) => (i > 0 ? [str, v] : v))
    );
    
    console.log(insertValue(data, str));
    .as-console-wrapper { max-height: 100% !important; top: 0; }
        4
  •  0
  •   Saad Mehmood    6 年前

    const myArray = ["q", "w", "e", "r", "t", "y"]
    const joinStr = 'xxx';
    console.log(myArray.join(','+ joinStr + ',').split(','));
        5
  •  0
  •   Tom M    6 年前

    可以将map()与flat()一起使用

    const myArray = ["q", "w", "e", "r", "t", "y"];
    const newArray = myArray.map((i) => {
       return [i, 'xxx']
    }).flat();
    
    console.log(newArray);
    
        6
  •  0
  •   traktor    6 年前

    你可以用 reduce 转换原始数组并将其放入双参数函数或将其作为 Array.prototype (如果您真的想让call方法出现在post中):

    const myArray = ["q", "w", "e", "r", "t", "y"]
    
    // A function taking array and glue arguments
    
    const interpose = (array, glue) => array.reduce(
       (acc, c,i,a) => (
            acc.push(c) && (i < a.length -1) && acc.push( glue),
            acc
        ), []
    );
    
    // As an array method
    
    Array.prototype.interpose = function( glue) {
        return  this.reduce(
        (acc, c,i,a) => (
             acc.push(c) && (i < a.length -1) && acc.push( glue),
             acc
         ), []);
    }
    
    // Test
    
    console.log( 'interpose( myArray, "XXX" = %s', interpose( myArray, "XXX"));
    console.log( 'myArray.interpose("XXX")  = %s', myArray.interpose("XXX"));