包裝型別為字串的物件屬性 - JavaScript


為此,請結合使用 Object.keys() 和 reduce()。為了顯示結果,我們還需要使用 concat()。

示例

以下是程式碼 -

var details = { name: ["John", "David"], age1: "21", age2: "23" },
   output = Object
      .keys(details)
      .reduce((obj, tempKey) =>
         (obj[tempKey] = [].concat(details[tempKey]), obj), {})
console.log(output)  

要執行以上程式,你需要使用以下命令 -

node fileName.js.

在此,我的檔名是 demo302.js。

輸出

它將在控制檯上輸出以下內容 -

PS C:\Users\Amit\javascript-code> node demo302.js
{ name: [ 'John', 'David' ], age1: [ '21' ], age2: [ '23' ] }

更新時間: 2020 年 11 月 9 日

479 次瀏覽

開啟你的職業之旅

完成課程獲得認證

開始
廣告
© . All rights reserved.