【问题标题】:Deconstruct element in array and then join again解构数组中的元素,然后再次加入
【发布时间】:2018-01-27 20:20:42
【问题描述】:

我正在尝试为数组中的每个单词添加字母“x”。将它添加到数组中的每个 element 没有问题,但在我的情况下,第二个元素由两个单词组成。

据我了解,它应该以某种方式从'bar baz' 解构为'bar','baz',然后将字母x 添加到barbaz,然后将'barx''bazx' 合并到@ 987654328@。但我不确定。

var array = ['foo', 'bar baz'];

// Pass a function to map
const map = array.map(word => word + 'x');

alert(map);
// Desired output: foox,barx bazx
// For obvious reasons, current output is foox,bar bazx (with no x after bar).

【问题讨论】:

    标签: javascript arrays ecmascript-6


    【解决方案1】:

    您可以拆分项目并为每个拆分的元素添加'X',然后再次加入项目以进行映射。

    const plusX = s => s + 'x';
    
    var array = ['foo', 'bar baz'],
        result = array.map(
            s => s
                .split(' ')
                .map(plusX)
                .join(' ')
        );
        
    console.log(result);

    【讨论】:

      【解决方案2】:

      你很接近,只需添加word.replace(" ", "x ")x 插入到上一个单词的末尾和空格之前:

      var array = ['foo', 'bar baz'];
      
      const map = array.map(word => word.replace(" ", "x ") + "x");
      
      alert(map);

      【讨论】:

        【解决方案3】:

        设置分隔符并拆分每个元素

        const array = ["foo", "bar baz"];
        
        const separator = " ";
        
        // Pass a function to map
        const map = array.map(word =>
          word
            .split(separator)
            .map(element => `${element}x`)
            .join(separator)
        );
        
        alert(map);

        【讨论】:

          【解决方案4】:

          这在一定程度上取决于您对“单词”的定义。例如。 'a+b'是一个单词,两个单词'a'和'b'还是三个单词'a','+'和'b'?

          一个简单的解决方案是匹配一个正则表达式模式并将“x”添加到所有匹配的单词:

          const array = ['foo', 'bar baz'];
          
          // Append 'x' to all words:
          const map = array.map(str => str.replace(/\w+/g, word => word + 'x'));
          
          console.log(map);

          这里,正则表达式 \w+ 仅匹配由字符 a-z、A-Z、0-9 和下划线 _ 组成的单词。所以它将用“ax+bx”替换“a+b”。

          【讨论】:

            【解决方案5】:

            您可以使用Array.from(),在thisArg 处定义"x" 作为默认参数,在.replace() 处使用RegExp /(\w$)|(\s)/g 来匹配字符串末尾的单词字符或空格字符

            var array = ['foo', 'bar baz'];
            
            // Pass a function to map
            const map = Array.from(array, (word, index, prop = "x") =>
                          word.replace(/(\w$)|(\s)/g, (_, p1, p2) => 
                            p1 ? `${p1}${prop}` : `${prop}${p2}`));
            
            console.log(map);

            【讨论】:

            • hm,thisArg 是您不提供的 Array.from 的最后一个参数。您只需声明一个额外的回调参数,如果您没有分配默认值“x”,该参数将是未定义的。还是我误会了你?
            • @le_m 没错。尝试先设置thisArg,但无法设置。我正在考虑在进一步测试后提出一个问题,以确定该案例是否是用户错误
            • 我也喜欢通过thisArg 提供诸如“x”之类的参数,但它的缺点是只有传统的function 通过其this 接收此参数。使用更优雅的箭头函数时,它几乎没用。
            • @le_m 知道了。必须明确使用this
            猜你喜欢
            • 1970-01-01
            • 2021-04-27
            • 1970-01-01
            • 2019-05-03
            • 1970-01-01
            • 2019-01-24
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多