how to loop through this array , I want to loop and and add name value to an empty array , when I console .log it gives the results below, could you please help , the result of my code is empty array
this.props.names it shows
(3) [{…}, {…}, {…}] 0: {name: "abc", id: 1} 1: {name: "def", id: 2} 2: {name: "gh", id: 3} length: 3 proto: Array(0)
let titles = []
let cats = []
cats = this.props.names
let len = this.props.names.length;
for( i = 0 ;i< cats.length ; i++){
titles.push(cats[i].id)
}
return titles;
1< cats.lenshould bei< cats.lengthbut you can do it more readable with map:const titles=cats.map(cat=>cat.id)lengthinstead oflenor even better; use the map.this.props.names=[a,b,c,d]and the expected and current resultExpected result:[x,y,z]; current result : [e,r,t]return this.props.names.map(cat=>cat.id)