Answers for "map function syntax"

46

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..." },
];
// ES2016+
// Create new array of post IDs. I.e. [1,2,3]
const postIds = posts.map((post) => post.id);
// Create new array of post objects. I.e. [{ id: 1, title: "Sample Title 1" }]
const postSummaries = posts.map((post) => ({ id: post.id, title: post.title }));

// ES2015
// Create new array of post IDs. I.e. [1,2,3]
var postIds = posts.map(function (post) { return post.id; });
// Create new array of post objects. I.e. [{ id: 1, title: "Sample Title 1" }]
var postSummaries = posts.map(function (post) { return { id: post.id, title: post.title }; });
Posted by: Guest on October-01-2020
24

array map javascript

const array1 = [1, 4, 9, 16];

// pass a function to map
const map1 = array1.map(x => x * 2);

console.log(map1);
// expected output: Array [2, 8, 18, 32]
Posted by: Guest on November-25-2019
5

how the map function works javascript

const array = [2, 5, 9];
let squares = array.map((num) => num * num);

console.log(array); // [2, 5, 9]
console.log(squares); // [4, 25, 81]
Posted by: Guest on April-07-2020
0

mapping functions

//lets practice on mapping over objects in an array, it's so easy :)
  //mappinmg function us used to go run over various functions in an object array and select a certain object depending on command given 
  const theNames = [
    {name: "Kinsley", age: "20", gender: "male", levelOfEducation: "uniStudent"},
    {name: "Grace", age: "20", gender: "female", levelOfEducation: "uniStudent"},
    {name: "Skeetah", age: "12", gender: "female", levelOfEducation: "pupil"},
    {name: "Mishell", age: "16", gender: "female", levelOfEducation: "student"},
    {name: "Mac", age: "24", gender: "male", levelOfEducation: "uniStudent"},
    {name: "Shelby", age: "32", gender: "male", levelOfEducation: "postGraduate"},
    {name: "Sasha", age: "22", gender: "female", levelOfEducation: "uniStudent"}
  ];
  const dataInList = theNames.map((listedNames) => {
    return listedNames.name;
  });
  console.log(dataInList);
Posted by: Guest on September-15-2021

Browse Popular Code Answers by Language