Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

javascript reduce array of objects

var objs = [
  {name: "Peter", age: 35},
  {name: "John", age: 27},
  {name: "Jake", age: 28}
];

objs.reduce(function(accumulator, currentValue) {
  return accumulator + currentValue.age;
}, 0); // 35 + 27 + 28 = 90
Comment

arry to object using reduce

const posts = [
    {id: 1, category: "frontend", title: "All About That Sass"},
    {id: 2, category: "backend", title: "Beam me up, Scotty: Apache Beam tips"},
    {id: 3, category: "frontend", title: "Sanitizing HTML: Going antibactirial on XSS attacks"}
];

const categoryPosts = posts.reduce((acc, post) => {
    let {id, category} = post;
    return {...acc, [category]: [...(acc[category] || []), id]};
}, {});
Comment

reduce object to array javascript

var arr = [{x:1},{x:2},{x:4}];

arr.reduce(function (a, b) {
  return {x: a.x + b.x}; // returns object with property x
})

// ES6
arr.reduce((a, b) => ({x: a.x + b.x}));

// -> {x: 7}
Comment

array of obj to obj with reduce

const convertArrayToObject = (array, key) => {
  const initialValue = {};
  return array.reduce((obj, item) => {
    return {
      ...obj,
      [item[key]]: item,
    };
  }, initialValue);
};
Comment

reduce method in javascript array of bjects

var arr = [{x:1}, {x:2}, {x:4}];
arr.reduce(function (acc, obj) { return acc + obj.x; }, 0); // 7
console.log(arr);
Comment

reduce an array of objects into a single object

var object = arr.reduce((obj, item) => (obj[item.key] = item.value, obj) ,{});
Comment

PREVIOUS NEXT
Code Example
Javascript :: iconify/react - npm 
Javascript :: how to reverse loop in javascript 
Javascript :: f string javascript 
Javascript :: react hooks delete item from array 
Javascript :: set cursor type javascript 
Javascript :: date.tolocaledatestring is not a function 
Javascript :: react-bootstrap navbar nav link refreshes page 
Javascript :: javascript traverse 
Javascript :: pushing to an array 
Javascript :: How to Loop Through an Array with a for…in Loop in JavaScript 
Javascript :: how to convert array to uppercase in javascript 
Javascript :: string replace in javascript 
Javascript :: console.time in javascript 
Javascript :: javascript replace doublequote with empty string 
Javascript :: getvalue data from datetimepicker 
Javascript :: sum numbers recursively js 
Javascript :: javascript for...of index 
Javascript :: javascript template string examples 
Javascript :: (0 , _reactRouterDom.useHistory) is not a function 
Javascript :: do you need a semicolon in javascript 
Javascript :: sort array of objects javascript by date 
Javascript :: js make node with string 
Javascript :: node redis json set key 
Javascript :: capitalize the string 
Javascript :: jquery index of element 
Javascript :: javascript add function to onchange event 
Javascript :: check if there is page has scrollbar x js 
Javascript :: moment date without timezone 
Javascript :: knex.js count 
Javascript :: javascript hard reload 
ADD CONTENT
Topic
Content
Source link
Name
1+6 =