简体   繁体   English

将数组拆分为多个 arrays

[英]Splitting array into multiple arrays

I have looked up answers on these questions How to split a long array into smaller arrays, with JavaScript and Split array into chunks but i still do not understand what i am doing wrong.我已经查找了这些问题的答案How to split a long array into small arrays, with JavaScript and Split array into chunks但我仍然不明白我做错了什么。

I have multiple random arrays that look like this我有多个看起来像这样的随机 arrays

[dog]
[dog, cat]
[cat, apple, frog]
[apple, dog]
etc.

and i want to split them so they look like this我想把它们分开,让它们看起来像这样

[dog]
[dog] [cat]
[cat] [apple] [frog]
[apple] [dog]

I was wondering if i could use a foreach loop to loop through all the arrays and split them into single arrays.我想知道是否可以使用 foreach 循环遍历所有 arrays 并将它们拆分为单个 arrays。

this is what my code looks like这就是我的代码的样子

 var arrays = press.post_category;
      arrays.forEach(function(item){
      var arr = [];
      var size = 1;
      for (let i = 0; i < item.length; i += size)
      arr.push(item.slice(i, i + size));
      console.log(arr);
        });

the output of this is output 是

["d","o","g"]
["d","o","g"," ","c","a","t"]
etc.

which is not what i want.这不是我想要的。 Im not sure why its splitting it into each individual letter instead of each value in the array.我不确定为什么将其拆分为每个单独的字母而不是数组中的每个值。

I am trying to avoid SPLICE by all means and only use SLICE if necessary.我想尽一切办法避免使用 SPLICE,并且只在必要时使用 SLICE。

Thanks in advance.提前致谢。

You can use .map() for each array:您可以为每个数组使用.map()

const animals = ['dog', 'cat', 'lama']

const result = animals.map(animal => [animal]) 

console.log(result)
// [ ['dog'], ['cat'], ['lama'] ]

You can then push the results into another array or you can use .reduce() to generate the end result.然后,您可以将结果推送到另一个数组中,也可以使用.reduce()生成最终结果。

You can use map here to take those individual values and return them to a new array as arrays, just by wrapping them in braces.您可以在此处使用 map 获取这些单独的值并将它们作为 arrays 返回到新数组,只需将它们包裹在大括号中即可。

 let arrays = ['cat', 'apple', 'frog']; console.log(JSON.stringify(arrays)); let final = arrays.map(m=>{return [m]}); console.log(JSON.stringify(final));

You can also define a splitter function and just call it against the array.您还可以定义一个拆分器 function 并针对数组调用它。

 let arrays= ['cat', 'apple', 'frog']; const splitter=(arr)=>arr.map(m=>{return [m]}); console.log(JSON.stringify(splitter(arrays)));

This would be useful if you had to do it in a lot of places or if you had an array of these arrays and wanted to iterate over that and use the splitter constant on each, like this:如果您必须在很多地方执行此操作,或者如果您有这些 arrays 的数组并希望对其进行迭代并在每个位置上使用拆分器常量,这将很有用,如下所示:

 let arrays = [ ['dog'], ['dog', 'cat'], ['cat', 'apple', 'frog'], ['apple', 'dog'] ]; const splitter=(arr)=>arr.map(m=>{return [m]}); let final = arrays.map(m=>{return splitter(m)}); console.log(final);

or if you needed to do this recursively with an array of unknown depth:或者如果您需要使用未知深度的数组递归地执行此操作:

 let arrays = [ ['dog'], ['dog', 'cat'], [ ['test', 'it', 'out'], ['cat', 'apple', 'frog'] ], [ ['one', 'more'], ['two', 'more'] ], ['apple', 'dog'] ]; const splitter=(arr)=>arr.map(m=>{ return Array.isArray(m)? splitter(m): [m] }); let final = splitter(arrays); console.log(final);

 var arrays = [ 'dog', 'dog,cat', 'cat,apple,frog', 'apple,dog' ]; var arr = []; arrays.forEach((item) => arr.push(item.split(","))); console.log(arr);

Do you mean like this?你的意思是这样吗?

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

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