web-profile

JavaScript map

var companies = [
  {name: "Google", category: "Technology"},
  {name: "Tesla", category: "Auto"},
  {name: "AirBnb", category: "Retail"}
];
var companyNames = companies.map(function(item) {
	return item.name;
});
console.log(companyNames); // ["Google", "Tesla", "AirBnb"]

Alternative syntax with arrow function:

var companies = [
  {name: "Google", category: "Technology"},
  {name: "Tesla", category: "Auto"},
  {name: "AirBnb", category: "Retail"}
];
var companyNames = companies.map(item => item.name);
console.log(companyNames); // ["Google", "Tesla", "AirBnb"]

Leave a Reply

Your email address will not be published.