10 examples of 'javascript reduce array of objects' in JavaScript

Every line of 'javascript reduce array of objects' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your JavaScript code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
this disclaimer
58function reduceObjects() {
59 var objs = Array.prototype.slice.call(arguments);
60 var acc = {}, obj;
61 while (objs.length) {
62 obj = objs.shift();
63 if (!obj) { continue; }
64 for (var k in obj) {
65 if (typeof obj[k] == 'undefined' || obj[k] === null) { continue; }
66 if (typeof obj[k] == 'object' && !Array.isArray(obj[k])) {
67 acc[k] = reduceObjects(acc[k], obj[k]);
68 } else {
69 acc[k] = obj[k];
70 }
71 }
72 }
73 return acc;
74}
Important

Use secure code every time

Secure your code as it's written. Use Snyk Code to scan source code in minutes – no build needed – and fix issues immediately. Enable Snyk Code

18export function reduceObject(object, fn, result = {}) {
19 eachProperty(object, function(name, value) {
20 result = fn(result, name, value);
21 });
22 return result;
23}
67function reduce(array, reducer, index) {
68 return array.reduce(function(prev, current) {
69 return reducer(prev, current[index]);
70 }, array[0][index]);
71}
371export function reduce(obj, func, value) {
372 for (const i in obj) {
373 value = func(value, obj[i], i);
374 }
375 return value;
376}
58export async function reduce<T, U>(array: T[] | readonly T[], callback: (accumulator: T | U, currentValue: T, currentIndex: number, array: readonly T[]) => Promise<T | U>, initialValue?: T | U): Promise<T | U> {
59 const hadInitialValue = typeof initialValue === 'undefined';
60 const startingIndex = hadInitialValue ? 1 : 0;
61
62 if (typeof initialValue === 'undefined') {
63 if (array.length === 0) {
64 throw new TypeError('Reduce of empty array with no initial value');
65 }
66
67 initialValue = array[0];
68 }
69
70 let value = initialValue;
71
72 for (let i = startingIndex; i < array.length; i++) {
73 const v = await callback(value, array[i], i, array);
74 value = v;
75 }
76
77 return value;
78}
7export function objectFromArray(arr, key = 'id') {
8 if (arr && arr.length) {
9 return arr.reduce((v, i) => {
10 v[i[key]] = i;
11 return v;
12 }, {});
13 }
14 return {};
15 }
1283function arrayToObjectsById(array, keyOf) {
1284 return array.reduce(function (objectsById, item) {
1285 objectsById[keyOf(item)] = item;
1286 return objectsById;
1287 }, {});
1288}
10function arrayReduce(cb, initial, array) {
11 var length = array.length
12 var result = initial
13 for (var i = 0; i < length; i++) {
14 result = cb(result, array[i], i, array)
15 }
16 return result
17}
34function reduceArray(d) {
35 return d.reduce(function(a, x) {
36 return a + x;
37 }, 0) / d.length;
38}
80function flatMap(array, callback) {
81 return array.reduce((result, item) => result.concat(callback(item)), []);
82}

Related snippets