I am new to stackoverflow so I am sorry if this question has been answered, what did find just led to more confusion. Been at this for hours.
My issue is when I try to add multiple keys with the same value, I only get the last key and value to store inside of the object. I used if/else statements to try and achieve my goal. Here is my code.
var itemData = [{
category: 'fruit',
itemName: 'apple',
onSale: false
},
{
category: 'canned',
itemName: 'beans',
onSale: false
},
{
category: 'canned',
itemName: 'corn',
onSale: true
},
{
category: 'frozen',
itemName: 'pizza',
onSale: false
},
{
category: 'fruit',
itemName: 'melon',
onSale: true
},
{
category: 'canned',
itemName: 'soup',
onSale: false
},
];
let list = { }
for (let i = 0; i < items.length; i ) {
if (items[i].category === 'fruit' && items[i].onSale === false) {
Object.assign(list, {fruit : [`${items[i].itemName}`]})
}
else if (items[i].category === 'fruit' && items[i].onSale === true) {
list.fruit.push(`${items[i].itemName}${'($)'}`)
}
else if (items[i].category === 'canned' && items[i].onSale === false) {
Object.assign(list, {canned : [`${items[i].itemName}`,] })
list.canned.push(`${items[i].itemName}`)
}
}
console.log(list) shows
{
canned: ["soup", "soup"],
fruit: ["apple", "melon($)"]
}
but im expecting
{
canned: ["beans", "soup"],
fruit: ["apple", "melon($)"]
}
Any help would be appreciated. Thank you.
CodePudding user response:
Object.assign(list, {canned : [`${items[i].itemName}`,] })
in your last else if statement overwrites the existing list canned
in the second loop iteration.
The easiest solution is to add cannes
right at the beginning when your create list
let list = {cannes: []}
Then you can drop the Object.assign
line completely.
For fruit
you should do the same and replace the Object.assign
line with list.fruit.push()
If you only want to create cannes
if any item is found which meets your condition, just add a if/else statement which checks if cannes
exists or not.
if ( ! 'canned' in list )
Object.assign(list, {canned : [`${items[i].itemName}`,] })
else
list.canned.push(`${items[i].itemName}`)
CodePudding user response:
var itemData = [{
category: 'fruit',
itemName: 'apple',
onSale: false
},
{
category: 'canned',
itemName: 'beans',
onSale: false
},
{
category: 'canned',
itemName: 'corn',
onSale: true
},
{
category: 'frozen',
itemName: 'pizza',
onSale: false
},
{
category: 'fruit',
itemName: 'melon',
onSale: true
},
{
category: 'canned',
itemName: 'soup',
onSale: false
},
];
let list = {};
let counter = 0;
itemData.forEach(isOnSale);
console.log(list);
function isOnSale(item) {
if (item['onSale'] === false && item['category'] === 'canned') {
//Initially Creating array and then pushing items
if (counter > 0) {
list['canned'].push(item['itemName'])
} else {
list['canned'] = Array(item['itemName'])
}
counter ;
} else if (item['category'] === 'fruit') {
if (counter > 0) {
(item['onSale'] === false) ? list['fruit'].push(item['itemName']): list['fruit'].push(`${item['itemName'].concat('($)')}`);
} else {
list['fruit'] = (item['onSale'] === false) ? Array(item['itemName']) : Array (`${item['itemName'].concat('($)')}`);
}
}
}