2

I want to add 4 objects the same as in array which should count. How to do so that in localstorage I will have 4 objects with the same properties?

componentDidMount(){
    const productData = [
        {
            productImg: JSON.parse(this.props.data.getProduct.mediaUrl).images[0],
            productPrice: this.props.data.getProduct.minimalPrice,
            productName: this.props.data.getProduct.name,
            productOID: this.props.data.getProduct.oid
        }
    ]

localStorage.setItem('looked-recently', JSON.stringify(productData))
}
3
  • Please explain more if you can Commented Mar 27, 2019 at 4:28
  • if you have multiple products then you better push new product objects in productData array and rest you are doing fine. On getting data from localstorage just parse and map on product data object. if you wanna add keys to product details then change structure lil bit. productData = [{product1:{info: 1}, product2: {info:2} }] Commented Mar 27, 2019 at 4:54
  • if your scope of question is more then ping me :) Commented Mar 27, 2019 at 4:54

2 Answers 2

1
componentDidMount() {

  const productData = {
    productImg: JSON.parse(this.props.data.getProduct.mediaUrl).images[0],
    productPrice: this.props.data.getProduct.minimalPrice,
    productName: this.props.data.getProduct.name,
    productOID: this.props.data.getProduct.oid
  }

  localStorage.setItem('looked-recently', JSON.stringify([
    productData,
    productData,
    productData,
    productData
  ]))
}
Sign up to request clarification or add additional context in comments.

Comments

1

if you have multiple products then you better push new product objects in productData array and rest you are doing fine. On getting data from localstorage just parse and map on product data object. if you wanna add keys to product details then change structure lil bit.

productData = [{product1:{info: 1}, product2: {info:2} }]

If you have any query ping me will update ans. Thanks

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.