[Chartjs]-Is there a JavaScript Method/ Function to loop Objects via Keys into a array?

5๐Ÿ‘

โœ…

Try (we use standard map and arrow function)

let d = [{"id":3,"name":"Audi","price":11},{"id":2,"name":"Mercedes","price":22},{"id":1,"name":"BMW","price":99},{"id":4,"name":"Trabant","price":113}];

let r= d.map(x=>x.name);

console.log(r);

3๐Ÿ‘

You can use the function map.

let result = arr.map(({name}) => name);

The array result will have the name of the brands.

Example

let arr = [{"id":3,"name":"Audi","price":11},{"id":2,"name":"Mercedes","price":22},{"id":1,"name":"BMW","price":99},{"id":4,"name":"Trabant","price":113}];

let result = arr.map(({name}) => name);
console.log(result);
.as-console-wrapper { min-height: 100%; }

Leave a comment