I have this lst, which has the name and total available.
let lst = [
{ name: 'Apple', total: 4 },
{ name: 'Mango', total: 9 },
{ name: 'Orange', total: 6 },
{ name: 'Passion', total: 2 }
]
Now there is a new list json which I receive from an api, that only has names of the items, I changed to an array like this.
let newlst = ["Apple","Orange","Banana","Mango","Apple","Pineapple","Passion"]
So how do I go on about for each item in newlst that is found in lst array then sum their total +1 .... If not then skip.
So expected out is
let lst = [
{ name: 'Apple', total: 6 },
{ name: 'Mango', total: 10 },
{ name: 'Orange', total: 7 },
{ name: 'Passion', total: 3 }
]
7 Answers 7
you can do it by loop also. compare the array elements and json object name and than increase the total of that corresponding json object.
let lst = [
{ name: 'Apple', total: 4 },
{ name: 'Mango', total: 9 },
{ name: 'Orange', total: 6 },
{ name: 'Passion', total: 2 }
];
let newlst = ["Apple","Orange","Banana","Mango","Apple","Pineapple","Passion"];
for(i=0;i<lst.length;i++){
for(j=0;j<newlst.length;j++){
if(lst[i].name === newlst[j]){
lst[i].total++;
}
}
}
console.log(lst)
Comments
You can loop through the lst and update the total by counting the fruits in the newlst using filter()
let lst = [
{ name: 'Apple', total: 4 },
{ name: 'Mango', total: 9 },
{ name: 'Orange', total: 6 },
{ name: 'Passion', total: 2 }
];
let newlst = ["Apple","Orange","Banana","Mango","Apple","Pineapple","Passion"];
lst.forEach(function(fruit){
//get the count in newlst
let count = newlst.filter(f => f == fruit.name).length;
//update the total by adding
fruit.total = fruit.total + count;
});
console.log(lst);
3 Comments
forEach not map? why == instead of ===?lst you again and again filter newlst, which is an O(N*M) solution. It could have been more efficient.Why do you use let instead of const? you shouldn't
You could do it like this
lst.map(({ name, total }) => ({ name, total: total + newlst.filter((x) => x === name).length }))
const lst = [
{ name: 'Apple', total: 4 },
{ name: 'Mango', total: 9 },
{ name: 'Orange', total: 6 },
{ name: 'Passion', total: 2 }
]
const newlst = ["Apple","Orange","Banana","Mango","Apple","Pineapple","Passion"]
console.log(lst.map(({ name, total }) => ({ name, total: total + newlst.filter((x) => x === name).length })))
Comments
Create a
Mapout ofnewlstwhose keys represent they fruit name and values represent the fruit count.Loop over
lstand check if the current fruit is in theMapthen increment count otherwise leave as is.
let lst = [{ name: "Apple", total: 4 }, { name: "Mango", total: 9 }, { name: "Orange", total: 6 }, { name: "Passion", total: 2 }];
let newlst = ["Apple", "Orange", "Banana", "Mango", "Apple", "Pineapple", "Passion"];
const m = newlst.reduce((r, f) => r.set(f, (r.get(f) || 0) + 1), new Map());
console.log(
lst.map((f) => (m.has(f.name) ? { ...f, total: f.total + m.get(f.name) } : f))
)
1 Comment
You can use Map to create a keyValue pair and update all the existing values in an efficient way.
Const dict = new Map()
lst.forEach(item => dict.set(item.name, item))
When you receive the new array
newlst.forEach(item => {
if(!dict.has(item)) { // true if you receive a new element not present in lst
const newObj = {name: item, total: 0}
dict.set(item, newObj);
lts.push(newObj)
}
dict.get(item).total++;
})
Since you haven't changed the reference your initial array has now all the values updated. This will be O(n) time complexity.
Comments
let lst = [
{ name: 'Apple', total: 4 },
{ name: 'Mango', total: 9 },
{ name: 'Orange', total: 6 },
{ name: 'Passion', total: 2 },
{ name: 'test', total: 1 }
];
let newlst = ["Apple","Orange","Banana","Mango","Apple","Pineapple","Passion"];
lst.map(el=>{
newlst.indexOf(el.name)>-1 ? el.total=el.total+1 : el
});
console.log(lst)
Comments
We can map through the list and filter out items in the newList based on the item being mapped in the list. The length of the filter output will give us the number of occurrences of the item in the newList and based on that we can derive the total logic.
const list = [{name: 'Apple', total: 4}, {name: 'Mango', total: 9},
{name: 'Orange', total: 6}, {name: 'Passion', total: 2}];
const newList = ['Apple', 'Orange', 'Banana', 'Mango', 'Apple', 'Pineapple', 'Passion'];
const result = list.map((x) => {
const numberOfOccurences = newList.filter((y) => y === x.name).length
return {
...x,
total: x.total + numberOfOccurences
};
});
console.log(result);