我正在使用以下代码(由JQuery Javascript Sort Array by Highest Count提供)对从最高到最低计数的字符串列表进行排序:
var items = {}, sortableItems = [], i, len, element, listOfStrings;
listOfStrings = JSON.parse(the_chems);
for (i = 0, len = listOfStrings.length; i < len; i += 1)
{
if (items.hasOwnProperty(listOfStrings[i])) {
items[listOfStrings[i]] += 1;
} else {
items[listOfStrings[i]] = 1;
}
}
for (element in items)
{
if (items.hasOwnProperty(element)) {
sortableItems.push([element, items[element]]);
}
}
sortableItems.sort(function (first, second)
{
return second[1] - first[1];
});
而不是这种类型的数组输入
["red", "red", "red", "blue", "blue"]
返回
[ [ "red", 3 ], [ "blue", 2 ] ]
我想使用像
这样的数组[["red","apple"], ["red","chilli"], ["red","melon"], ["blue","ocean"], ["blue","eyes"]]
并返回
[["red", 3, ["apple","chilli","melon"]], ["blue", 2, ["blue","ocean"]]
答案 0 :(得分:3)
您可以使用哈希表并将结果收集到数组中。
var array = [["red", "apple"], ["red", "chilli"], ["red", "melon"], ["blue", "ocean"], ["blue", "eyes"]],
hash = Object.create(null),
result = [];
array.forEach(function (a) {
if (!hash[a[0]]) {
hash[a[0]] = [a[0], 0, []];
result.push(hash[a[0]]);
}
hash[a[0]][1]++;
hash[a[0]][2].push(a[1]);
});
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
答案 1 :(得分:2)
你可以使用Map和一些很酷的解构:
var hash = new Map(), result = [];
input.forEach(([color,fruit]) => {
if(hash.has(color)){
var arr = hash.get(color);
arr[1]++;
arr[2].push(fruit);
}else{
var tmp = [color,1,[fruit]];
hash.set(color,tmp);
result.push(tmp);
}
});
Try it out (提示:对于对象来说这将是一个很好的用例......)
较慢但可能更具可读性:
var hash = new Map();
input.forEach(([color,fruit])=>{
if(hash.has(color)){
hash.get(color).push(fruit);
}else{
hash.set(color,[fruit]);
}
})
var result = [...hash.entries()].map(([color,fruits])=>[color,fruits.length,fruits]);
答案 2 :(得分:1)
也可以没有地图...
function groupAndSort(items){
var output = []
for(var i=0;i<items.length;i++){
var exists = false;
for(var u=0;u<output.length;u++){
if(output[u][0]==items[i][0]){
output[u][1]++
output[u][2].push(items[i][1])
exists = true
break
}
}
if(!exists)
output.push([items[i][0],1,[items[i][1]]])
}
output.sort(function (first, second){
return second[1] - first[1];
});
return output
}
答案 3 :(得分:1)
为什么不创建这样的有用数据结构:
{
red:["apple","chili","melon"],
blue:["ocean","eyes"]
}
完成一个循环,没有多余的哈希表:
var arr = [["red","apple"], ["red","chilli"], ["red","melon"], ["blue","ocean"], ["blue","eyes"]];
arr.reduce(function(c,element) {
if(!c[element[0]]) {
c[element[0]] = [];
}
c[element[0]].push(element[1]);
return c;
},{});
对于那些不关心可读代码的人:
arr.reduce(function(c,element) {
return (c[element[0]] ? c[element[0]].push(element[1]) : c[element[0]] = [element[1]]) && c;
},{});
答案 4 :(得分:1)
您可以使用Array.reduce
重组数组。
const arr = [["red","apple"], ["red","chilli"], ["red","melon"], ["blue","ocean"], ["blue","eyes"]];
const newArr = arr.reduce((acc , curr) => {
let flag = false;
acc.forEach(a => {
if(a[0] === curr[0]) {
a[1]++;
a[2].push(curr[1]);
flag = true;
}
});
if(!flag) {
let newArr = [curr[0], 1, [curr[1]]];
acc.push(newArr);
}
return acc;
}, []);
console.log(newArr);
&#13;
答案 5 :(得分:1)
这是另一个解决方案。首先,该函数创建每种颜色的简单计数。从那里,我们通过对象并过滤/映射相关项目,并推送我们最终所需的输出。
const testArray = [["red","apple"], ["red","chilli"], ["red","melon"], ["blue","ocean"], ["blue","eyes"]]
function modifyArray(arr) {
let colorCount = arr.reduce((allColors, color) => {
if (color[0] in allColors) {
allColors[color[0]]++
}
else {
allColors[color[0]] = 1
}
return allColors
}, {})
let newArr = [];
for (var key in colorCount) {
let coloredItems = arr.filter(val => val[0] === key).map(val => val[1])
newArr.push([key, colorCount[key], coloredItems])
}
return newArr;
}
console.log(modifyArray(testArray))
&#13;