Answers for "js map filter reduce"

0

map filter reduce javascript

Map, Filter, Reduce javascript
//=================================
let myarray = [
  {
    name: "John",
    city: "london",
    dept: "computer",
    salary: 1000,
  },
  {
    name: "bella",
    city: "new york",
    dept: "fianance",
    salary: 1000,
  },
  {
    name: "justine",
    city: "paris",
    dept: "computer",
    salary: 1000,
  },
];
console.log("myarray : ", myarray);
// ----------------------------------
// map
let mapArray = myarray.map(function (item) {
  return item.name;
});
console.log("mapArray : ", mapArray);
// ----------------------------------
//filter
let filterArray = myarray.filter(function (item) {
  if (item.dept == "computer") {
    return true;
  } else {
    return false;
  }
});
console.log("filterArray : ", filterArray);
// ----------------------------------
//reduce
// 1.
let reduceArray1 = myarray.reduce(function (acc, item) {
  return acc + item.salary;
}, 0);
console.log("reduceArray1 : ", reduceArray1);
// ------
// 2.
let reduceArray2 = myarray.reduce(
  function (acc, item) {
    if (item.dept == "computer") {
      acc.com.push(item.name);
    } else {
      acc.finance.push(item.name);
    }
    return acc;
  },
  { com: [], finance: [] }
);
console.log("reduceArray2 : ", reduceArray2);
// ----------------------------------
Posted by: Guest on September-28-2021
1

js map filter reduce

const numbers = [1, 2, 3, 4];
const doubled = numbers.map(item => item * 2);
console.log(doubled); // [2, 4, 6, 8]
Posted by: Guest on April-01-2020
1

js map filter reduce

const numbers = [1, 2, 3, 4];
const sum = numbers.reduce(function (result, item) {
  return result + item;
}, 0);
console.log(sum); // 10
Posted by: Guest on April-01-2020
0

js map filter reduce

const students = [
  { name: 'Quincy', grade: 96 },
  { name: 'Jason', grade: 84 },
  { name: 'Alexis', grade: 100 },
  { name: 'Sam', grade: 65 },
  { name: 'Katie', grade: 90 }
];

const studentGrades = students.filter(student => student.grade >= 90);
return studentGrades; // [ { name: 'Quincy', grade: 96 }, { name: 'Alexis', grade: 100 }, { name: 'Katie', grade: 90 } ]
Posted by: Guest on April-01-2020
0

js map filter reduce

const numbers = [1, 2, 3, 4];
const evens = numbers.filter(item => item % 2 === 0);
console.log(evens); // [2, 4]
Posted by: Guest on April-01-2020

Code answers related to "Javascript"

Browse Popular Code Answers by Language