I tried to counts the same values in object and arrays, and it should show the first numbers which show off in arrays to country it in object here is my wrong code and example case:
let index = {};
let result = [];
const arrayList = [{
"code": 101,
"name": "banana",
"price": 1000
}, {
"code": 4,
"name": "bluebberries",
"price": 3000
}, {
"code": 900,
"name": "apple",
"price": 300
}];
// here is value of code in object list
const userChoose = [900, 900, 101, 101, 101, 101, 101, 101, 101, 101, 101, 101, 101];
userChoose.forEach(ar => {
arrayList.forEach(point => {
let key = ar;
if (key in index) {
index[key].count++;
} else {
let newEntry = {
id: point.code,
count: 1
};
index[key] = newEntry;
result.push(newEntry);
}
})
});
console.log(result);
the output is not same like I wanted like this :
// final result:
[
{
"id": 900,
"count": 2
},
{
"id": 101,
"count": 11
}
]