Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

283
Views
No se puede acceder a las propiedades del objeto por el argumento de una función

¿Por qué array[i].key (donde key === "apellido") dentro de la función no funciona, mientras que array[i].surname funciona perfectamente?

 let objects = [ { name: 'Jack', surname: 'Jackson' }, { name: 'Ivar', surname: 'Bjornsson' }, { name: 'John', surname: 'Mickelson' } ]; function sort (array, key) { for (let i = 0; i < array.length; i++) { console.log(array[i].key)// Somehow the "key", which is equal to "surname" doesn't work; // here will be undefined; console.log(array[i].surname)//But here writing 'surname' directly works fine; // the correct answer will be there; console.log(key)// However, key === surname } } sort(objects, 'surname');

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Tienes que acceder a la propiedad entre corchetes:

 let objects = [ { name: 'Jack', surname: 'Jackson' }, { name: 'Ivar', surname: 'Bjornsson' }, { name: 'John', surname: 'Mickelson' } ]; function sort (array, key) { for (let i = 0; i < array.length; i++) { console.log(array[i][key])// Somehow the "key", which is equal to "surname" doesn't work; // here will be undefined; console.log(array[i].surname)//But here writing 'surname' directly works fine; // the correct answer will be there; console.log(key)// However, key === surname } } sort(objects, 'surname');

Este

 array[i].key

es equivalente a

 array[i]['key']
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!