Finden Sie ein doppeltes Element auf dem Array
const order = ["apple", "banana", "orange", "banana", "apple", "banana"];
const result = order.reduce(function (prevVal, item) {
if (!prevVal[item]) {
// if an object doesn't have a key yet, it means it wasn't repeated before
prevVal[item] = 1;
} else {
// increase the number of repetitions by 1
prevVal[item] += 1;
}
// and return the changed object
return prevVal;
}, {}); // The initial value is an empty object.
console.log(result); // { apple: 2, banana: 3, orange: 1 }
Cruel Cormorant