Dismiss
Announcing Stack Overflow Documentation

We started with Q&A. Technical documentation is next, and we need your help.

Whether you're a beginner or an experienced developer, you can contribute.

Sign up and start helping → Learn more about Documentation →

Data Structure now - http://i.stack.imgur.com/sCh7h.png

This is data structure that I have and I think there are redutand things, it would be nice if I could keep just one parent object and then access to data.

In my actions.js I'm fetching data from specific API endpoint, and then store it in product object

export const getSingleProduct = ( {dispatch}, productName) => {
    productService.getSingleProduct(productName).then(result => {
        dispatch(types.GET_SINGLE_PRODUCT, { product: result.json() })
    })
} 

Then I have this in single.js store file, and then single object is nested under product

import { GET_SINGLE_PRODUCT, ADD_TO_BAG } from '../mutation-types'

const state = {
    single: []
}

const mutations = {
    [GET_SINGLE_PRODUCT] (state, product) {
        state.single = product
    }

}

export default {
    state,
    mutations
}

And at the end I have the getter into component, that stor data again into product object

vuex: {
    getters: {
        product: ({single}) => single,
        productName: ({route}) => route.params.productName
    },
    actions: {
        getSingleProduct,
        addToBag
    }
},

So as title say, is there a way to make this more simpler, to make it for e.g only like product>data not product>single>product>data ?

Thanks.

share|improve this question
    
This is more of a general JavaScript question. You can just write a function to take the original object and output your desired object be either creating a new object and only including the fields you want or deleting the fields you don't want from the original object – vbranden Aug 22 at 13:53

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.