简体   繁体   English

对象解构成对象声明?

[英]Object deconstruction into object declaration?

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 extract the values, then defining an object literal with those extracted values and their new keys. 现在,我正在使用解构方法提取值,然后使用提取的值及其新键定义对象文字。

Here is my function: 这是我的功能:

getProductReviewData() {
    const {
        averageRateDisplay,
        rawAverageRate,
        displayReviewCount,
        productReviewIds,
        productReviews
    } = this.productReviewsStore.getAll(); // this is deconstruction of an object
    return {
        ratingDisplay: averageRateDisplay,
        rating: rawAverageRate,
        ratingCount: displayReviewCount,
        reviewIds: productReviewIds,
        reviewMap: productReviews
    };
}

However, I was wondering if is a shorthand way to do this, so use one line for both the deconstruction and the declaration. 但是,我想知道是否是这样做的捷径,所以在解构和声明时都使用一行。 Does anyone know if this is possible? 有人知道这是否可能吗?

I don't think there's anything to put them in the same statement , especially with the renaming. 我认为没有什么可以将它们放在同一条语句中 ,尤其是在重命名时。 You could of course write your own helper function for renaming object properties. 您当然可以编写自己的帮助程序功能来重命名对象属性。

I think it would be much cleaner though to assign the object to one variable and then repeat that multiple times, than repeating every property/variable name twice: 我认为将对象分配给一个变量然后重复多次比将每个属性/变量名称重复两次要干净得多:

getProductReviewData() {
    const all = this.productReviewsStore.getAll();
    return {
        ratingDisplay: all.averageRateDisplay,
        rating:        all.rawAverageRate,
        ratingCount:   all.displayReviewCount,
        reviewIds:     all.productReviewIds,
        reviewMap:     all.productReviews
    };
}

You can also use destructuring into object properties to swap the two sides if you 1 like that better: 如果您更喜欢1 ,还可以使用分解为对象属性来交换两侧:

getProductReviewData() {
    let res = {};
    ({
        averageRateDisplay: res.ratingDisplay,
        rawAverageRate:     res.rating,
        displayReviewCount: res.ratingCount,
        productReviewIds:   res.reviewIds,
        productReviews:     res.reviewMap
    } = this.productReviewsStore.getAll());
    return res;
}

1: Personally I think it's just unnecessary confusing - and one line longer! 1:就我个人而言,这只是不必要的混乱-再加一行!

UPD: Simple is better than complex! UPD:简单胜于复杂! I like Bergi's answer =) 我喜欢Bergi的答案 =)

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]}))
    )
}

You can destructure and give new key/variable values in one go. 您可以一次性分解并提供新的键/变量值。 But as far as I know you can't destructure directly into a new object. 但是据我所知,您不能直接将其分解为一个新对象。

const start = {
  averageRateDisplay: 1,
  rawAverageRate: 2,
  displayReviewCount: 3,
  productReviewIds: 4,
  productReviews: 5
 };

 // destructuring with renaming of variables
 const {
   // name to match : newName
   averageRateDisplay: ratingDisplay,
   rawAverageRate: rating,
   displayReviewCount: ratingCount,
   productReviewIds: reviewIds,
   productReviews: reviewMap
} = start;

// creating new object, using property value shorthand
// https://ariya.io/2013/02/es6-and-object-literal-property-value-shorthand
const end = {
    ratingDisplay,
  rating,
  ratingCount,
  reviewIds,
  reviewMap
};

console.log(end)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM