通过对象键创建JavaScript数组

时间:2019-08-15 18:32:58

标签: javascript ecmascript-6

我有以下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 |
 })

1 个答案:

答案 0 :(得分:2)

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

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