有可能创建一个多维数组,然后在每个多维数组上推送另一个数组吗? 假设变量
arr = ["apple", "orange", "Avocados", "Tomato", "Tangerine"]
我想要的输出是:
[
["a", ["apple", "avocados"] ],
[ "o", ["orange"] ],
["T", ["Tomato", "Tangering"]]
]
关于在输出之前创建第一个从头到新的arr多维的示例,我们像这样[[ "a"],["o"],["T"]]
创建,我想要的输出在上方(框码)
然后再次检查该第一个初始值是否与第一个初始数组相同,例如,将其推入那些2d数组,但是如果其他数组的长度不相同,则应创建一个函数,稍后再使用
答案 0 :(得分:0)
我认为您的输出太复杂而无法产生。您可以像这样简化输出格式:
{
"a" : ["apple", "avocados"],
"o": ["orange"],
"t": ["Tomato", "Tangering"]
}
您可以使用Lodash轻松生成这样的输出:
_.groupBy(["apple", "orange", "Avocados", "Tomato", "Tangerine"], function (item) {
return item[0].toLowerCase()
});
或迭代数组:
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]);
}
答案 1 :(得分:0)
您可以通过查找具有该值的数组来对数据进行分组,或者为结果集创建一个新数组。
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; }
更好的结构是使用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; }
答案 2 :(得分:0)
您可以将数组简化为对象
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);
答案 3 :(得分:0)
还有另一种使用普通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)
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)
答案 4 :(得分:0)
使用reduce
,创建一个对象,其中每个项目的第一个char
为key
,并创建一个以该字符开头的所有项目的数组,形式为value
。然后调用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);