How to create and use an array of objects in JavaScript

3 Answers

0 votes
const workers = [
    { id: 13451, name: 'Axel'},
    { id: 87134, name: 'Blaze'},
    { id: 79372, name: 'Arti'},
    { id: 39810, name: 'Isla'}
];
  
for (let i = 0; i < workers.length; i++) {
    console.log(workers[i].id + " " + workers[i].name);
}
  
  
  
/*
run:
      
13451 Axel
87134 Blaze
79372 Arti
39810 Isla

*/
 

 
 

 



answered Nov 1, 2019 by avibootz
edited May 28, 2025 by avibootz
0 votes
const workers = [
    {
        id: 82927,
        name: "Axel",
        age: 37
    },
    {
        id: 98272,
        name: "Blaze",
        age: 41
    },
    {
        id: 76362,
        name: "Isla",
        age: 31
    }
];
  
  
console.log(workers[0].id + " " + workers[0].name + " " + workers[0].age);
console.log(workers[2].id + " " + workers[2].name + " " + workers[2].age);


  
/*
run:
      
82927 Axel 37
76362 Isla 31
   
*/

 



answered Nov 1, 2019 by avibootz
edited May 28, 2025 by avibootz
0 votes
const workers = [
    {
        id: 82927,
        name: "Axel",
        age: 37,
        show_details() {
            return(this.id + " " + this.name + " " + this.age);
        }
    },
    {
        id: 98272,
        name: "Blaze",
        age: 41
    },
    {
        id: 76362,
        name: "Isla",
        age: 31
    }
            
];
  
console.log(workers[0].show_details());
 
  
/*
run:
      
82927 Axel 37
   
*/

 



answered Nov 2, 2019 by avibootz
edited May 28, 2025 by avibootz
...