简体   繁体   English

创建多维数组,然后将每个多维数组推入另一个数组

[英]create multidimensional array then push another array each those multidimensional array

is that possible to create a multidimensional array, then push another array on each those multidimensional array ?? 是否有可能创建一个多维数组,然后在每个多维数组上推送另一个数组? lets say the variable 可以说变量

arr = ["apple", "orange",  "Avocados", "Tomato", "Tangerine"]

and the output I want is: 我想要的输出是:

 [ ["a", ["apple", "avocados"] ], [ "o", ["orange"] ], ["T", ["Tomato", "Tangering"]] ] 

the example about to create first initial to new arr multidimensional before the output, we create like this, [[ "a"],["o"],["T"]] and output i want is on above ( the box code ) 关于在输出之前创建第一个从头到新的arr多维的示例,我们像这样创建[[ "a"],["o"],["T"]]然后我想要的输出在上面(方框代码)

then check again if that first initial same with a first initial array, push it on those 2d arrays, that is for example, but if array length for others is not same, we should create a function, to use it again late 然后再次检查该第一个初始值是否与第一个初始数组相同,例如,将其推入那些2d数组,但是如果其他数组的长度不相同,我们应该创建一个函数,稍后再使用

I think your output is too complicated to produce. 我认为您的输出太复杂而无法产生。 You can simplify the output format like this: 您可以像这样简化输出格式:

{
   "a" : ["apple", "avocados"], 
   "o": ["orange"], 
   "t": ["Tomato", "Tangering"] 
 }

You can produce output like this with ease with Lodash: 您可以使用Lodash轻松生成如下输出:

_.groupBy(["apple", "orange",  "Avocados", "Tomato", "Tangerine"], function (item) {
  return item[0].toLowerCase()
});

Or with iterate the array: 或迭代数组:

var arr = ["apple", "orange",  "Avocados", "Tomato", "Tangerine"];
var output = [];
for (var i = 0 ; i < arr.length ; i++){
  if (output[arr[i][0].toLowerCase()] == undefined)
    output[arr[i][0].toLowerCase()] = [];
  output[arr[i][0].toLowerCase()].push(arr[i]);
}

You could group the data by finding the array with the value or create a new array for the result set. 您可以通过查找具有该值的数组来对数据进行分组,或者为结果集创建一个新数组。

 var array = ["apple", "orange", "avocados", "tomato", "tangerine"], result = array.reduce((r, s) => { var temp = r.find(([c]) => c === s[0]); if (temp) { temp[1].push(s); } else { r.push([s[0], [s]]); } return r; }, []); console.log(result); 
 .as-console-wrapper { max-height: 100% !important; top: 0; } 

A better structure would be to use a Map and collect all strings to the same starting letter. 更好的结构是使用Map并将所有字符串收集到相同的起始字母。

 var array = ["apple", "orange", "avocados", "tomato", "tangerine"], result = Array.from( array.reduce((map, s) => map.set(s[0], [...(map.get(s[0]) || []), s]), new Map) ); console.log(result); 
 .as-console-wrapper { max-height: 100% !important; top: 0; } 

You can reduce the array to object 您可以将数组简化为对象

 var arr = ["apple", "orange", "Avocados", "Tomato", "Tangerine"]; var output = arr.reduce(function(res, item) { if(Object.keys(res).indexOf(item.charAt(0).toLowerCase()) == -1) { res[item.charAt(0).toLowerCase()] = []; } res[item.charAt(0).toLowerCase()].push(item); return res; },{}); console.log(output); 

Yet another alternative using vanilla JavaScript and some functional programming: 使用香草JavaScript和一些功能编程的另一种选择:

 const input = ["apple", "orange", "Avocados", "Tomato", "Tangerine"] // (1) An unique list of lower-cased initials of all given input words // * Set gives us the unique behavior const initials = [...new Set ( input.map (([initial]) => initial.toLowerCase ()) )] const byInitial = ([initial]) => ([initial_]) => initial_.toLowerCase () == initial // (2) This groups each world by their initial // Iterates each lowered initial and filters the input by each initial // to build the groups! const grouped = initials.reduce ((o, initial) => [ ...o, // <--- this accumulates the new result with the previous one [ initial, input.filter (byInitial(initial)) ] ], []) console.log (grouped) 

Yet another approach, now using zip : 现在使用zip另一种方法:

 const input = ["apple", "orange", "Avocados", "Tomato", "Tangerine"] // (1) An unique list of lower-cased initials of all given input words // * Set gives us the unique behavior const initials = [...new Set ( input.map (([initial]) => initial.toLowerCase ()) )] const byInitial = ([initial]) => ([initial_]) => initial_.toLowerCase () == initial // (2) This builds groups each world by their initial const groups = initials.map (initial => input.filter (byInitial (initial)) ) const zip = (xs, ys) => xs.map((x, i) => [x, ys[i]]) // zip builds the pairs, where each one is the initial and the group const grouped = zip (initials, groups) console.log (grouped) 

Using reduce , create an object with first char of each item as the key and an array of all the items which start with that char as it's value . 使用reduce创建一个对象,该对象的每个项的第一个charkey并以该char的value作为所有项的数组。 Then call Object.entries to get the desired output: 然后调用Object.entries以获取所需的输出:

 const arr = ["apple", "orange", "avocados", "tomato", "tangerine"], group = Object.entries(arr.reduce((a, i) => ((a[i[0]] = a[i[0]] || []).push(i), a), {})); console.log(group); 

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

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