Object deconstruction into object declaration?

前端 未结 3 2134
野性不改
野性不改 2020-12-21 21:06

I have an object which has several values I want to extract and place into another object with different keys for those values. Right now I\'m using deconstruction to extrac

3条回答
  •  我在风中等你
    2020-12-21 21:40

    UPD: Simple is better than complex! I like Bergi's answer =)

    Probably you can declare new keys and then change them in iterations ¯_(ツ)_/¯

    getProductReviewData() {
        //declare new keys
        const newKeys = {
            averageRateDisplay: "ratingDisplay",
            rawAverageRate:     "rating",
            displayReviewCount: "ratingCount",
            productReviewIds:   "reviewIds",
            productReviews:     "reviewMap"
        }
        const productReviewsStore = this.productReviewsStore.getAll();
    
        //return the object with replaced keys
        return Object.assign({},
          ...Object.keys(productReviewsStore)
               .filter(key => newKeys[key])
               .map(key => ({[newKeys[key]]: productReviewsStore[key]}))
        )
    }
    

提交回复
热议问题