define a map javascript code example
Example 1: javascript map function
const posts = [
{ id: 1, title: "Sample Title 1", description: "Lorem ipsum dolor sit amet, consectetur adipiscing elit..." },
{ id: 2, title: "Sample Title 2", description: "Lorem ipsum dolor sit amet, consectetur adipiscing elit..." },
{ id: 3, title: "Sample Title 3", description: "Lorem ipsum dolor sit amet, consectetur adipiscing elit..." },
];
const postIds = posts.map((post) => post.id);
const postSummaries = posts.map((post) => ({ id: post.id, title: post.title }));
var postIds = posts.map(function (post) { return post.id; });
var postSummaries = posts.map(function (post) { return { id: post.id, title: post.title }; });
Example 2: Map Javascript - Example
let contacts = new Map()
contacts.set('Jessie', {phone: "213-555-1234", address: "123 N 1st Ave"})
contacts.has('Jessie')
contacts.get('Hilary')
contacts.set('Hilary', {phone: "617-555-4321", address: "321 S 2nd St"})
contacts.get('Jessie')
contacts.delete('Raymond')
contacts.delete('Jessie')
console.log(contacts.size)