简体   繁体   English

将字符串数组转换为一个数组JS

[英]Convert arrays of strings to one array JS

I faced a problem with array convertions. 我遇到了阵列转换的问题。 I have an array with different values like: 我有一个不同值的数组,如:

let fruitsArry = ['apple', 'apple, mango, orange', 'orange'];

And I need to convert it to array like: 我需要将其转换为数组,如:

let fruitsArrSplited = ['apple', 'apple', 'mango', 'orange', 'orange'];

Could you suggest me any simple solution for this? 你能为我建议一个简单的解决方案吗?

Thanks in advance. 提前致谢。

Just join and split with the separator of the quoted part. 只需使用引用部分的分隔符进行连接和拆分。

 let fruitsArray = ['apple', 'apple, mango, orange', 'orange'], result = fruitsArray.join(', ').split(', '); console.log(result); 

With white space after comma, you could split with optional whitepsace. 使用逗号后的空白区域,您可以使用可选的whitepsace进行拆分。

 let fruitsArray = ['apple', 'apple, mango, orange', 'orange'], result = fruitsArray.join().split(/,\\s*/); console.log(result); 

Just use join(',') and split(',') . 只需使用join(',')split(',') And further use map() (optional) to remove the trailing and leading whitespaces. 并进一步使用map() (可选)来删除尾随和前导空格。 This will also ensures that there can be any amount of whitespaces between array element. 这也将确保数组元素之间可以有任何数量的空格。

 let fruitsArry = ['apple', 'apple, mango, orange', 'orange']; var res = fruitsArry.join(',').split(',').map(item=>item.trim()); console.log(res); 

Without join(" ,") which generates an unnecessary intermediate string, you may do as follows; 如果没有生成不必要的中间字符串的join(" ,") ,您可以执行以下操作:

 var fruitsArry = ['apple', 'apple, mango, orange', 'orange'], result = fruitsArry.reduce((r,e) => r.concat(e.includes(",") ? e.split(/\\s*,\\s*/) : e),[]); console.log(result); 

 let array = ['apple', ' apple , mango , orange ', 'orange']; newArray = array.join(',').split(/\\s*,\\s*/); console.log(newArray); 

I think this should do the trick. 我认为这应该可以解决问题。 This will handle white space before and after each word too. 这也将处理每个单词前后的空白区域。

To answer your question simply you can use: 要回答您的问题,您可以使用:

const af=a=>a.join(',').split(',').map(e=>e.trim()); // 45 character

and use it like: 并使用它像:

let res = af(['apple', 'apple, mango, orange', 'orange']);
console.log(res);

Or if you prefer the long version: 或者如果您更喜欢长版:

/**
 * Define the function
 */
function formatArray(arr) {
  let tmp: string[];
  let result: string[] = [];

  tmp = arr.join(',').split(',');

  tmp.forEach((el, i) => {
    let str = el.trim();  // this is to remove the extra spaces
    result.push(str); // add the element to final array
  });
  return result;
}

And use it as: 并将其用作:

// The orignal array
let arr = ['apple', 'apple, mango, orange', 'orange'];
arr = formatArray(arr);
console.log(arr);

Note that trimming part in the function, is an option you might not need this in your case but I figured out this is what you might want. 请注意,在您的功能中修剪部分是您可能不需要的选项,但我发现这是您可能想要的。

You may use _js. 你可以使用_js。 Underscore Js Documentation 下划线Js文档

let fruitsArry = ['apple', 'apple, mango, orange', 'orange'];
var res = _.chain(fruitsArry).map(function(fruit) { return fruit.split(',') }).flatten().value()

console.log(res)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM