I'm currently creating a grocery-list sorting app and ran into an issue. Because the aisle locations vary from store to store I need to dynamically create objects and add all items in that aisle to the corresponding aisle object.
I don't have a lot of experience creating/modifying objects, so I'm not sure how to approach this.
I'm looking for a result like this:
{
aisles:
{
1:
{[
'pizza',
'ice cream',
]}
2:
{[
'tissues',
'toilet paper',
]}
}
}
Can anyone help me with this?
Thanks so much.
The code above won't run because there are no keys for the inner most object containing arrays. You can do remove the inner most object and leave it as an array.
objectName = {
aisle: {
1: ['pizza', 'ice cream'],
2: ['tissue', 'toilet paper']
}
}
To dynamically add data you can use a function.
function addValues(key, value){
objectName.aisle[key] = value;
}
Now you can call the method to add data into the objectName.
addValues("3", ["data","data"]);