Reputation: 96
so I have an object of arrays, each array contains a certain object, now what I wanna do is when I click on any of those objects to be able to select it so I can preview it in VueJs
here's my object:
shopitems:{
pizza:[
{
"id": 1,
"name": "id labore ex et quam laborum",
"email": "[email protected]",
"body": "Margherita",
"img" : " https://media.onebite.app/62/b2/c5/f7/62b2c5f7-a995-47a6-af1b-64d892692386.jpg",
"brand" : "pizza",
"price": "12",
},
{
"id": 2,
"name": "odio adipisci rerum aut animi",
"body": "Fresh Veggie",
"img" : "https://res-2.cloudinary.com/crunchbase-production/image/upload/c_lpad,h_256,w_256,f_auto,q_auto:eco/r6krxkmxkkthsbw9fm9f",
"brand" : "pizza",
"price": "30",
},
],
salad:[
{
"id":3,
"name": "id labore ex et quam laborum",
"email": "[email protected]",
"body": "Salad u",
"img" : "https://nitrocdn.com/KZJKWDkEwMJCwERIlnRsPNdqobwBIlEo/assets/static/optimized/rev-83d0269/wp-content/uploads/2018/05/brazilian-chopped-salad-10-256x256.jpg",
"brand" : "salad",
"price": "12",
},
{
"id": 4,
"name": "odio adipisci rerum aut animi",
"body": "Salad V",
"img" : "https://bigoven-res.cloudinary.com/image/upload/d_recipe-no-image.jpg,t_recipe-256/mixed-greens-salad-with-tomato-443eb4.jpg",
"brand" : "salad",
"price": "25",
},
],
},
this is my function that I tried to use
this.$store.state.eCommerce.shopitems.find((items) =>
items.forEach((item) => item.id = id))
return this.item_data = this.item
please kindly tell me what am I doing wrong
Upvotes: 0
Views: 657
Reputation: 1
It might be a good idea to transform your object to an array. There are multiple ways to achieve that.
let objectKeys=Object.keys(yourObject);
let yourObjectArray=[];
objectKeys.forEach(x=>{
yourObjectArray.push(yourObject[x])
});
Or maybe
let yourObjectArray=[...yourObject]
Once you got the array, you can use filter() as usual
Upvotes: 0
Reputation: 81
If you are trying to access/modify pizza array, you are accessing wrong object
this.$store.state.eCommerce.shopitems.pizza.find((items) =>
items.forEach((item) => item.id = id))
return this.item_data = this.item
Upvotes: 1
Reputation: 1
shopitems is an object not array so you can't use .find(). you can do like this initialize empty array and then add shopitems to this array and use find function
`var myArray = []
myArray.push( this.$store.state.eCommerce.shopitems)
myArray.find((items) =>
items.forEach((item) => item.id = id))return this.item_data = this.item
this.$store.state.eCommerce.shopitems = myArray[0] ? myArray[0] : {}`
Upvotes: 0