首页 > 解决方案 > 从对象键创建 JavaScript 数组

问题描述

我有以下 JavaScript 数组:

 supportedCurrencies = [  
    {  
       "currencyCode":"USD",
    },
    {  
       "currencyCode":"CAD",
    },
    {  
       "currencyCode":"GBP",
    }
 ]

我的目标是获取 currencyCode 值并创建一个数组(例如 [USD, CAD, GBP...],然后将这些数组与 | ... 连接起来以获得最终的字符串输出 USD|CAD|GBP

 supportedCurrencies.map(key => {
  // Map through JS object
  // Get currencyCode values
  // Create new array with values
  // Join these values with |
 })

标签: javascriptecmascript-6

解决方案


 supportedCurrencies = [  
    {  
       "currencyCode":"USD",
    },
    {  
       "currencyCode":"CAD",
    },
    {  
       "currencyCode":"GBP",
    }
 ]

    const info = supportedCurrencies.map(el => el.currencyCode).join("|")
    
    console.log(info)


推荐阅读