I am currently working with Angular 5 and I have a task to update the array values based on the selectedMobile array. Below is my code snippet:
const mobilelist = [
{ 'key': 'apple', 'name': 'Apple', 'checked': false },
{ 'key': 'sumsung', 'name': 'Sumsung', 'checked': false },
{ 'key': 'oneplus', 'name': 'Oneplus', 'checked': false },
{ 'key': 'mi', 'name': 'Mi', 'checked': false }
];
const selectedMobile = ['apple'];
const newmobilelist: any = [];
if (selectedMobile.length > 0) {
mobilelist.forEach(element => {
if (selectedMobile.find(x => x == element.key) != null) {
newmobilelist.push({ 'key': element.key, 'name': element.name, 'checked': true });
} else {
newmobilelist.push({ 'key': element.key, 'name': element.name, 'checked': false });
}
});
}
console.log(newmobilelist);
Upon checking the console, I am expecting the following result:
[
{ 'key': 'apple', 'name': 'Apple', 'checked': true },
{ 'key': 'sumsung', 'name': 'Sumsung', 'checked': false },
{ 'key': 'oneplus', 'name': 'Oneplus', 'checked': false },
{ 'key': 'mi', 'name': 'Mi', 'checked': false }
];
However, the code is currently returning:
[
{ 'key': 'apple', 'name': 'Apple', 'checked': false },
{ 'key': 'sumsung', 'name': 'Sumsung', 'checked': false },
{ 'key': 'oneplus', 'name': 'Oneplus', 'checked': false },
{ 'key': 'mi', 'name': 'Mi', 'checked': false }
];
I am unsure of why this discrepancy is happening. Any help to resolve this issue would be greatly appreciated.