How to pass object literal properties and values as arguments?

I’ve tried searching but didn’t find specifically what I’m looking for.

I have this object:

const candidates = {
  candidate1: {
    name: 'somename',
    party: 'someparty',
    votes: 'x%'
    },
    candidate2: {
      name: 'anothername',
      party: 'anotherparty',
      votes: 'y%'
    },
    candidate3: {
      nombre: 'othername',
      party: 'otherparty',
      votes: 'z%'
    }
};

Then I have:

function someFunction (name, party, votes) {
  (does stuff);
  return something;
}

What would be the best way to iterate so that this happens:

someFunction takes candidate1 values and passes them as arguments.

Then it takes candidate2, repeats.

Then it takes candidate3, repeats.

And so on until n-th candidate?

Answer

You could simply iterate the Object.values of candidates to call your method for each candidate:

const candidates = {
  candidate1: {
    name: 'somename',
    party: 'someparty',
    votes: 'x%'
  },
  candidate2: {
    name: 'anothername',
    party: 'anotherparty',
    votes: 'y%'
  },
  candidate3: {
    nombre: 'othername',
    party: 'otherparty',
    votes: 'z%'
  }
};

function someFunction(name, party, votes) {
  console.log(name, party, votes);
}


Object.values(candidates).forEach(x => someFunction(x.name, x.party, x.votes))