3

Can anyone hint me on how to iterate the currencies array using forEach method to get the id and name of the object.

  const currencies = [{
        id: 'USD', name: 'US Dollars'
      }, {
        id: 'UGX', name: 'Ugandan Shillings'
      }, {
        id: 'KES', name: 'Kenyan Shillings'
      }, {
        id: 'GHS', name: 'Ghanian Cedi'
      }, {
        id: 'ZAR', name: 'South African Rand'
      }];
var populateCurrencies = (currencies)=>{
    currencies.forEach(function(id,name){
     

    }
  }
  
Dharman
  • 30,962
  • 25
  • 85
  • 135
Iamlimo
  • 183
  • 3
  • 12

3 Answers3

4

Perhaps you're getting confused because your argument names in your forEach callback misrepresent what they actually are.

The first argument of your .forEach callback function is the element that you're currently iterated on. In your case it is the object you're currently on from your currencies array. It is not the id like you have named it.

The second argument in your .forEach callback is the index, however, you do not need this as all you're after is the object (which is the first argument)

So, if the first argument is the object, you can access its name and id properties at each iteration using dot notation.

See example below:

const currencies = [{id:"USD",name:"US Dollars"},{id:"UGX",name:"Ugandan Shillings"},{id:"KES",name:"Kenyan Shillings"},{id:"GHS",name:"Ghanian Cedi"},{id:"ZAR",name:"South African Rand"}];

const populateCurrencies = (currencies) => {
  currencies.forEach(function(obj) {
    console.log(obj.name, obj.id);
  });
}

populateCurrencies(currencies)
Nick Parsons
  • 45,728
  • 6
  • 46
  • 64
3

Add curly braces to extract properties of item passed to the foreach iterator:

const currencies = [{
        id: 'USD', name: 'US Dollars'
      }, {
        id: 'UGX', name: 'Ugandan Shillings'
      }, {
        id: 'KES', name: 'Kenyan Shillings'
      }, {
        id: 'GHS', name: 'Ghanian Cedi'
      }, {
        id: 'ZAR', name: 'South African Rand'
      }];

    currencies.forEach(function({id,name}){
     console.log(id,name);

    })
Mosè Raguzzini
  • 15,399
  • 1
  • 31
  • 43
  • OP looks like a beginner. You should probably avoid destrucutring – adiga Feb 13 '19 at 08:58
  • OP was using destructuring argument in the wrong way, so the better fix to gain knowledge were curly braces IMHO. Also mixing const and var is a bad pratice, map is preferred over forEach and extracting an array with the same form of the original one could be useless. I tried only to address him to a working code. – Mosè Raguzzini Feb 13 '19 at 09:07
0

const currencies = [{
        id: 'USD', name: 'US Dollars'
      }, {
        id: 'UGX', name: 'Ugandan Shillings'
      }, {
        id: 'KES', name: 'Kenyan Shillings'
      }, {
        id: 'GHS', name: 'Ghanian Cedi'
      }, {
        id: 'ZAR', name: 'South African Rand'
      }];
      
currencies.forEach(currency =>    
     console.log(currency.id + " : " + currency.name)
)
Tarun Khurana
  • 887
  • 8
  • 9