Concat数组成一个数组(JS或Lodash) [英] Concat array into one array (JS or Lodash)

查看:99
本文介绍了Concat数组成一个数组(JS或Lodash)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

输入:

var array1 = ["12346","12347\n12348","12349"];

步骤:


\ n 替换为','并添加到列表中。

Replace \n with ',' and Add into list.

输出:

var array2 = ["12346","12347","12348","12349"];

我尝试了以下逻辑,但未达到输出。看起来缺少某些东西。

I tried below logic but not reach to output. Looks like something is missing.

var array2 = [];

_.forEach(array1, function (item) {               
       var splitData = _.replace(item, /\s+/g, ',').split(',').join();
       array2.push(splitData);
});

我的代码输出:

["12346","12347,12348","12349"]


推荐答案

您可以使用换行符'\ n'加入它,然后按换行符分隔结果。

You could join it with newline '\n' and split it by newline for the result.

var array= ["12346", "12347\n12348", "12349"],
    result = array.join('\n').split('\n');

console.log(result);

这篇关于Concat数组成一个数组(JS或Lodash)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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