How to print an array of objects in TypeScript

1 Answer

0 votes
const obj = [
    { name: 'Matrix', age: 58 },
    { name: 'Blade Runner', age: 90 },
    { name: 'Guardians of the Galaxy', age: 49 }
];

console.log(obj);

console.log(obj[0]);
console.log(obj[1]);
console.log(obj[2]);

console.log(obj[0].name + " : " + obj[0].age);
console.log(obj[1].name + " : " + obj[1].age);
console.log(obj[2].name + " : " + obj[2].age);

for (let i = 0; i < obj.length; i++)
	console.log(obj[i].name  + " -- " + obj[i].age);

  
  
  
  
/*
run:
  
[{
  "name": "Matrix",
  "age": 58
}, {
  "name": "Blade Runner",
  "age": 90
}, {
  "name": "Guardians of the Galaxy",
  "age": 49
}] 
{
  "name": "Matrix",
  "age": 58
} 
{
  "name": "Blade Runner",
  "age": 90
} 
{
  "name": "Guardians of the Galaxy",
  "age": 49
} 
"Matrix : 58" 
"Blade Runner : 90" 
"Guardians of the Galaxy : 49" 
"Matrix -- 58" 
"Blade Runner -- 90" 
"Guardians of the Galaxy -- 49" 
  
*/

 



answered Apr 22, 2022 by avibootz
edited Apr 22, 2022 by avibootz
...