我如何组织在JavaScript中根据类似的值被分组在一起的单个数组?



我试图在JavaScript中编写一个函数,该函数以数组为输入并返回一个数组,其中类似的元素分组在子数组中。该函数接受一个数组作为输入,并返回一个数组作为输出。

例如,当我输入groupfunction([1, 2, 4, 591, 392, 391, 2, 5, 10, 2, 1, 1, 1, 20, 20]),它应该返回[[1, 1, 1, 1], [2, 2, 2], 4, 5, 10, [20, 20], 391, 392, 591]。然而,由于某种原因,函数输出的是[[1, 1, 1, 1], 2, [4, 4, 4], [591, 591], 392, 391, 5, 10, 20]。我可以使用什么来将数组中的相似元素分组到子数组中?

我发现以下问题很有帮助,并根据其中包含的答案编写了代码:

  • 创建一个重复多次的相同元素的数组
  • 用JavaScript从数组中删除零值
  • 如何创建包含1…N
const findnumberofinstances = (array, element) => { // This function finds how many times a specific element occurs in an array
let numberoftimes = 0;
const functionisequal = (input1) => {if(input1 === element) {return true}
else {return false}};
const indexofelement = array.findIndex(functionisequal);
while(array.includes(element) === true) {
array.find(functionisequal);
numberoftimes += 1;
array.splice(indexofelement, 1);
}
return numberoftimes;
}
const finduniquevalues = (arr) => { // This function reduces an array to only have unique values.
const map = [];
for (let value of arr) {
if (map.indexOf(value) === -1) {
map.push(value);
}
}
return map;
};
const removeElementsWithValue = (array, val) => { // This function removes specific values from an array
var i = array.length;
while (i--) {
if (array[i] === val) {
array.splice(i, 1);
}
}
return array;
}
const removezeroes = (array) => {return removeElementsWithValue(array, 0)} // This function removes zeroes from an array
const repeatelement = (elem, n) => { // This function repeats an element, effectively multiplying an array of one value [value] by n so that value occurs n times.
var arr = [];
for (var i = 0; i < n; i++) {
arr = arr.concat(elem);
};
return arr;
};
const findnumberofeachelement = (array) => {let blank = []; // This function finds how many of each element is in an array.
let arraycopy = array.concat([]);
let sortedArray = array.slice().sort();
let arraycopysorted = arraycopy.slice().sort();
for(element of arraycopysorted){let num = findnumberofinstances(sortedArray, element);
blank.push(num);
}
return removezeroes(blank);
}
const groupfunction = (array) => { // This function groups elements that are identical into sub-arrays only if there are more than one of the elements.
let sum = [];
let unique = finduniquevalues(array);
let blank = [];
let number = findnumberofeachelement(array);
for(let i=0; i< unique.length; i++) {
if(number[i] > 1) {
sum = repeatelement(unique[i], number[i])
blank.push(sum);
}
else {
sum = unique[i]
blank.push(sum)
}
sum = []
}
return blank
}
console.log(groupfunction([1, 2, 4, 591, 392, 391, 2, 5, 10, 2, 1, 1, 1, 20, 20])) // This should return [[1, 1, 1, 1], [2, 2, 2], 4, 5, 10, [20, 20], 391, 392, 591].

您可以使用array#reduce对对象累加器中的每个值对数组进行分组。

const groupfunction = arr => Object.values(
arr.reduce((r, v) => {
r[v] = r[v] || [];
r[v].push(v);
return r;
}, {}))
.map(a => a.length === 1 ? a[0] : a);
console.log(groupfunction([1, 2, 4, 591, 392, 391, 2, 5, 10, 2, 1, 1, 1, 20, 20]));
.as-console-wrapper { max-height: 100% !important; top: 0; }

const a = [1,2,3,3,5,1, 10, 9, 10];
const b = a.reduce((acc,e ) => {
const item = acc.find(i =>  i === e); // find item in array;
if (item ){ // if we found item;
const index = acc.indexOf(item);
acc.splice(index, 1, [item, item]) // convert item to array
} else {
let inserted = false; // we'll push item into array or subarray   
acc.forEach(item => {
if (item instanceof Array && item === e){
item.push(e);  // append item to subarray
inserted = true;
}
}) 
if (!inserted) acc.push(e); // append as item;
}
return acc; 
}, [] /* initial array */) 

console.log(JSON.stringify(b))

好了。这并不简单,但我应该是正确的。

function groupUp(array) {
array.sort(function(a, b){return a - b})
const map = new Map()
const order = []
array.forEach(e => {
if (map.has(e)) {
map.get(e).push(e)
} else {
map.set(e, [e])
order.push(e)
}
})
const final = []
order.forEach(e => {
const group = map.get(e)
if (group.length == 1) {
final.push(group[0])
} else {
final.push(group)
}
})
return final
}
console.log(groupUp([1,2,4,591,392,391,2,5,10,2,1,1,1,20,20]))

最新更新