我有一個帶有幾個值的二維陣列。我想計算這個陣列中的重復項并獲得一個帶有值的新二維陣列,最后是計數
let colors = [
['blue', 'green', 5.00, 57],
['blue', 'green', 5.00, 57],
['yellow', 'green', 8.30, 84],
['blue', 'green', 5.00, 57],
['orange', 'blue', 7.00, 0],
['yellow', 'green', 8.30, 84],
];
function count_duplicate() {
let counts = []
for (let i = 0; i < colors.length; i ) {
if (counts[colors[i]]) {
counts[colors[i]] = 1
} else {
counts[colors[i]] = 1
}
}
console.log(counts);
}
count_duplicate();
現在我有這個結果
counts = [blue,green,5,57: 3, yellow,green,8.3,84: 2, orange,blue,7,0: 1]
但我需要這樣的陣列
counts = [[blue,green,5,57,3],
[yellow,green,8.3,84,2],
[orange,blue,7,0,1]]
uj5u.com熱心網友回復:
您可以Map在此處使用以實作結果:
let colors = [
["blue", "green", 5.0, 57],
["blue", "green", 5.0, 57],
["yellow", "green", 8.3, 84],
["blue", "green", 5.0, 57],
["orange", "blue", 7.0, 0],
["yellow", "green", 8.3, 84],
];
function count_duplicate() {
let counts = new Map();
for (let i = 0; i < colors.length; i ) {
const data = counts.get(colors[i].toString());
if (data) data.count ;
else counts.set(colors[i].toString(), { value: colors[i], count: 1 });
}
const result = [];
for (let [, { value, count }] of counts) {
result.push([...value, count]);
}
return result;
}
console.log(count_duplicate());
uj5u.com熱心網友回復:
更改array []為an object {},為了sub-array(row)輕松操作每個出現的次數,它應該是一個物件而不是一個陣列!和使用Object.entries回圈遍歷它的key(str) and value(times), 并回傳str由逗號和它們出現的時間分割。
let colors = [
["blue", "green", 5.0, 57],
["blue", "green", 5.0, 57],
["yellow", "green", 8.3, 84],
["blue", "green", 5.0, 57],
["orange", "blue", 7.0, 0],
["yellow", "green", 8.3, 84],
];
function count_duplicate() {
let counts = {};//<--
for (let i = 0; i < colors.length; i ) {
if (counts[colors[i]]) {
counts[colors[i]] = 1;
} else {
counts[colors[i]] = 1;
}
}
console.log(counts); //{ 'blue,green,5,57': 3, 'yellow,green,8.3,84': 2, 'orange,blue,7,0': 1 }
const res = Object.entries(counts).map((count) => {
[str, times] = count; //['blue,green,5,57': 3]
return [...str.split(","), times];//[ 'blue', 'green', '5', '57', 3 ]
});
console.log(res);
}
count_duplicate();
結果:
[ [ 'blue', 'green', '5', '57', 3 ],
[ 'yellow', 'green', '8.3', '84', 2 ],
[ 'orange', 'blue', '7', '0', 1 ] ]
uj5u.com熱心網友回復:
我不知道花哨的語法,但使用哈希圖應該可以作業,它只是一個 JavaScript 物件。
let colors = [
['blue', 'green', 5.00, 57],
['blue', 'green', 5.00, 57],
['yellow', 'green', 8.30, 84],
['blue', 'green', 5.00, 57],
['orange', 'blue', 7.00, 0],
['yellow', 'green', 8.30, 84],
];
const hashmap = {};
colors.forEach(color => {
const key = JSON.stringify(color);
if(!hashmap[key]) hashmap[key] = 1;
else hashmap[key] ;
if(hashmap[key] == 1) return color;
});
console.log(hashmap);
const uniqueArray = [];
for(const key in hashmap)
uniqueArray.push(JSON.parse(key));
console.log(uniqueArray);
uj5u.com熱心網友回復:
您獲得該輸出是因為您使用的環境會自動將陣列強制轉換為字串。例如,Chrome 和 NodeJS 會這樣做,而 Firefox 則不會。
這是一個簡單的方法,它使用一個物件將資料存盤在鍵/值對中更新的顏色資訊。它使用toString陣列作為鍵,一個陣列作為包含原始陣列的值,以及一個初始化的總數。然后可以使用 提取輸出(所有存盤為物件中的屬性值的陣列)Object.values。
let colors=[["blue","green",5,57],["blue","green",5,57],["yellow","green",8.3,84],["blue","green",5,57],["orange","blue",7,0],["yellow","green",8.3,84]];
const out = {};
colors.forEach(arr => {
// Create the key from the stringified array
const key = arr.toString();
// If the output object doesn't have a
// property with that key create one with
// an array built from the array, and setting
// the total to zero
out[key] = out[key] ?? [ ...arr, 0 ];
// Increment the total (the last element)
out[key][arr.length] ;
});
// Use Object.values to retrieve
// the updated arrays
console.log(Object.values(out));
附加檔案
傳播語法
Nullish coalescing operator
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/359499.html
標籤:javascript
上一篇:從多個陣列狀態中洗掉索引元素
