0

I have the following JavaScript Array:

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

My goal is to get the currencyCode values and create an array (ex. [USD, CAD, GBP...] and then join these arrays with | ...to get final string output USD|CAD|GBP

 supportedCurrencies.map(key => {
  // Map through JS object
  // Get currencyCode values
  // Create new array with values
  // Join these values with |
 })
Brian
  • 1,026
  • 1
  • 15
  • 25
Michael
  • 403
  • 1
  • 9
  • 28
  • 4
    That's not a valid object. It has to be an array of objects most likely: `supportedCurrencies = [ { "currencyCode":"USD", }, { "currencyCode":"CAD", }, { "currencyCode":"GBP", } ]` – Clarity Aug 15 '19 at 18:35
  • Ah you are right. I updated the object. Thanks – Michael Aug 15 '19 at 18:37

1 Answers1

2

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

    const info = supportedCurrencies.map(el => el.currencyCode).join("|")
    
    console.log(info)
user1984
  • 5,990
  • 2
  • 13
  • 32
  • 1
    This extracts all values from the objects instead of just the currencyCode, and includes them comma separated in the result, but the OP gave no indication that they wanted that. Also `return [...temp];` may as well just be `return temp;`. There is no point on making a copy of that array. – Paul Aug 15 '19 at 18:46
  • You are right. @hiad just corrected my answer. – user1984 Aug 15 '19 at 18:57
  • 1
    Seems better, but this also adds a `|` to the start of the string, so you might want to add a `.slice(1)` at the end to remove it – Paul Aug 15 '19 at 19:03
  • Thanks @Paulpro you are a good person. I resorted to `map` and `join` which seem more appropriate here. – user1984 Aug 15 '19 at 19:25
  • Which I just saw you already used in your comments above :D – user1984 Aug 15 '19 at 19:26