ECMAScript 之如何將 Object 轉成 Array ?

栏目: JavaScript · 发布时间: 6年前

内容简介:由於 ECMAScript 提供了 Object Literal,因此建立 object 非常簡單,特別適合將 function 的 argument 以 object 表示,又由於 ECMAScript 的 object 與 array 非常類似,若能將 object 轉成 array,就能善用 Array.prototype 下豐富的 method。ECMAScript 2017使用 Object Literal 宣告

由於 ECMAScript 提供了 Object Literal,因此建立 object 非常簡單,特別適合將 function 的 argument 以 object 表示,又由於 ECMAScript 的 object 與 array 非常類似,若能將 object 轉成 array,就能善用 Array.prototype 下豐富的 method。

Version

ECMAScript 2017

Object.entries

var foo = {
  firstName: 'Sam',
  lastName: 'Xiao',
};

Object.entries(foo)
  .forEach(item => console.log(`${item[0]}: ${item[1]}`));

使用 Object Literal 宣告 foo object,再使用 ECMAScript 2017 的 Object.entries() 將 object 轉成 array,如此就能使用 Array.prototype.forEach() 等方式處理 object。

ECMAScript 之如何將 Object 轉成 Array ?

Practice

實務上該怎麼使用這個特性呢 ? 傳統我們喜歡將 arguement 以 primitive type 傳入 function,但這樣很容易造成 parameter 個數爆炸,也就是 Refactoring 所謂的 Primitive ObsessionLong Parameter List 兩個 Code Smell。

若 argument 改用 object 方式傳遞,則 function 會清爽許多,且可讀性也更高。

this.$store.commit('RecipientInfo/setRecipientName', this.selectedItem.name);
this.$store.commit('RecipientInfo/setRecipientMobilePhone', this.selectedItem.mobile);
this.$store.commit('RecipientInfo/setRecipientLocalPhone', this.selectedItem.phone);
this.$store.commit('RecipientInfo/setRecipientAddressCityId', this.selectedItem.city);
this.$store.commit('RecipientInfo/setRecipientAddressCity', this.selectedItem.cityName);
this.$store.commit('RecipientInfo/setRecipientAddressTownId', this.selectedItem.town);
this.$store.commit('RecipientInfo/setRecipientAddressTown', this.selectedItem.townName);
this.$store.commit('RecipientInfo/setRecipientAddress', this.selectedItem.address);

如使用 Vuex 時,常會看到類似的 code,目的是呼叫 mutation,將值一一寫入 state。

const mutation = {
  recipientName: this.selectedItem.name,
  recipientMobilePhone: this.selectedItem.mobile,
  recipientLocalPhone: this.selectedItem.phone,
  recipientAddressCityId: this.selectedItem.city,
  recipientAddressCity: this.selectedItem.cityName,
  recipientAddressTownId: this.selectedItem.town,
  recipientAddressTown: this.selectedItem.townName,
  recipientAddress: this.selectedItem.address,
};

commitMutation.call(this, 'RecipientInfo', mutation);

將 mutation 與實際值以 key / value 形式組成 mutation object,自己寫一個 commitMutation() helper,將 module name 與 mutation object 傳入。

由於要使用 this.$store.commit() 呼叫 mutation,因此要使用 call() 將代表 Vue Instance 的 this 傳入。

export const commitMutation = function (moduleName, mutation) {
  Object.entries(mutation)
    .forEach(item => {
      const mutationName = `set${capitalize(item[0])}`;
      this.$store.commit(`${moduleName}/${mutationName}`, item[1]);
    });
};

由於 mutation 是 object,但我們希望以 array 的方式來執行 this.$store.commit() ,因此使用 Object.entries() 將 object 轉成 array,如此就能使用 forEach() 執行,其中 item[0] 相當於 object 的 key,而 item[1] 相當於 object 的 value。

Conclusion

  • Object.entries() 相當好用,能將 object parameter 轉成 array 後,就能夠使用 Array.prototype 下豐富的 method

Reference

MDN , Object.entries()


以上所述就是小编给大家介绍的《ECMAScript 之如何將 Object 轉成 Array ?》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

算法学

算法学

哈雷尔 / 第1版 (2006年2月1日) / 2006年2月1日 / 38.0

本书的意图在于按序学习或研究,而不是作为一个参考。因而按照每章依赖于前面章节的结构组织本书,且流畅易读。第一部分预备知识中的大部分材料对于那些具有程序设计背景的人是熟悉的。无论是否恰当,本书包含了计算机科学家当前感兴趣的研究专题的简明讨论。这本教科书的书后有每章详细参考书目的注记,并通过“后向”指针把教科书中的讨论与相关文献联系起来。目前的版本包含大量习题,以及大约三分之一的题解。可用题解作为教科......一起来看看 《算法学》 这本书的介绍吧!

CSS 压缩/解压工具
CSS 压缩/解压工具

在线压缩/解压 CSS 代码

html转js在线工具
html转js在线工具

html转js在线工具

UNIX 时间戳转换
UNIX 时间戳转换

UNIX 时间戳转换