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){
     

    }
  }
  

8

3 Answers 3

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)

Sign up to request clarification or add additional context in comments.

Comments

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);

    })

2 Comments

OP looks like a beginner. You should probably avoid destrucutring
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.
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)
)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.