Use array.reduce with objects (2023)

An array.reduce primer

Let's take a quick look at how Array.Reduce works. You can skip this if you are already familiar.

Array.Reduce reduces an array to a single value. The resulting value can be from any type - it does not have to be an array. Array.Reduce differs from other array methods such as "card" and "filter".A declaration of reduction that returns the sum of a number of numbers:

Example 1:

-Codespracheprache-js-Konst numbers = [1, 2, 3, 4, 5];Const sum = Number.Reduce ((further, number) => {return Next + Number;}, 0);

Array.Reduce accepts two arguments:

1. A return function that is carried out in series for each element in the array and receives the following parameters:

  • ThatAccumulator(In the example "called next"), what the work value is. In the first iteration, the accumulator is set to the initial value (0). For all subsequent iterations, it is the value that is returned by the previous iteration.
  • The current array element ('number' in the example).
  • The current array index (not used in the example).
  • The array that is reduced (not used in the example).
  • The initial value for the accumulator.example 1The initial value is 0.

2. The reduction of the instruction in 'Example 1' leads the return function five times with the following values:

  1. Accumulator (next):0 (the initial value);Value (number):1;Returns:1;
  2. Accumulator:1;Wert:2;Returns:3;
  3. Accumulator:3;Wert:3;Returns:6;
  4. Accumulator:6;Wert:4;Returns:10;
  5. Accumulator:10;Wert:5;Returns:fifteen;

The final value of 'sum' will be '15'.

(Video) Convert JavaScript Arrays to Objects using reduce

Array.reduce applied to objects

Remember that array.reduce can use the first and return values of all kinds, which makes it very flexible. Leave us examine how we can use it to carry out some common tasks with simple objects.

1. Converting an array of objects into a single object that is characterized by ID:

Developers often have to look up a value in an array with a value from another array.`Property and every profile has a" useride "experience. We have to adapt every user with their profile, whereby '' equal 'profile.userid'. A basic implementation of it is shown inexample 2.

Example 2:

-Code language-js-contral users = [{id: 1, email: 'dontreras@email.tld'}, {id: 2, email: 'afeher@email.tld'}, {id:3, email, email, email, email, email: 'odj@email.tld'},];Const Profiles = [{userid: 1, first name: 'Danielle', Lastname: 'Contreras'}, {Userid: 2, first name: 'Alfredas', Lastname: 'Fehér'}, {Userid: 3, firstname: 'Orpheus', Last name: 'de jong'},];Const UserwithProfles = Users.Map ((user) => {Const profile = profile.find ((profile) => ( === Profile.userid)); return {... user, profile};});// UserwithProfles: // [// {id: 1, e -Mail: 'Dontreras@email.tld', profile: {userid: 1, first name: 'Danielle', Lastname: 'Contreras'}, // {ID: 2, e -Mail: 'afeher@email.tld', profile: {userid: 2, first name: 'Alfredas', Lastname:' Fehér '}}, // {id: 3, e -Mail:' odj@email.tld ', profile: {userid: 3, first name:' Orpheus', Lastname: 'de jong'}}, //]

The problem withExample 2is that it uses array.find in, which is inefficient, which may not be a role in the example for the short arrays, but it becomes a problem when working with longer arrays.The longer is the potential rectification time for a certain profile. We can solve this problem by transforming the "profile" array into an object beforehand, with the "Userid" property being used as the key:

Example 3:

-Code language-js-contral users = [{id: 1, email: 'dontreras@email.tld'}, {id: 2, email: 'afeher@email.tld'}, {id:3, email, email, email, email, email: 'odj@email.tld'},];Const Profiles = [{userid: 1, first name: 'Danielle', Lastname: 'Contreras'}, {Userid: 2, first name: 'Alfredas', Lastname: 'Fehér'}, {Userid: 3, firstname: 'Orpheus', Last name: 'de jong'},];// transform the profiles into an object broken down by the user -density: Const profileesbyuserid = profile.reduce ((next, profile) => {const {userid} = profile; return {... next, [userid]: profile};}, {});// profileesbyuserid: // {// 1: {userid: 1, first name: 'Danielle' ,, Lastname: 'contreras'}, // 2: {userid: 2, firstname: 'alfredas', load name: 'Fehér'}, // 3: {userid: 3, first name: 'Orpheus', Lastname: 'de jong'}, //} // Find the profiles for ID: Const Userwithprofles = User.Map ((user) => {back {... user, profile: profilebyuserid []};});// Userwithprofile: // [// {id: 1, e -Mail: 'Dontreras@email.tld', profile: {userid: 1, first name: 'Danielle', Lastname: 'Contreras'}, // {ID: 2, e -Mail: 'afeher@email.tld', profile: {userid: 2, first name: 'Alfredas', Lastname:' Fehér '}}, // {id: 3, e -Mail:' odj@email.tld ', profile: {userid: 3, first name:' Orpheus', Lastname: 'de jong'}}, //]

Example 3creates the same result asExample 2But will be much faster with long arrays.

2. Copy of an object with filtered properties:

Sometimes you need a copy of an object that only contains certain properties from the original object or omit the certain properties. This is a great application for array.reduce.

Example 4:

-Code Language-Js-// Copy, keep permissible properties: Const Person = {first name: 'Orpheus', Lastname:' de Jong ', phone:' +1 123-456-7890 ', email:'fake@email.tld ',};Const permitted properies = ['first name', 'Lastname'];Const allkeys = object.Keys (person);Const result = allkeys.reduce ((next, key) => {if (permitted properties.includes (key)) {return {... Next, [key]: person [key]};} Else {return next;}}, {});// Result: // {first name: 'Orpheus' ,, Lastname: 'de jong'}
(Video) Using JavaScript Reduce on an Array of Objects

Example 4Reduces the keys from the "person" object into a new object that only contains the properties whose keys in the array "eventual property" are included.Unless they add the permissible properties to the property name.

Example 5:

-Code Language-Js-// Copy an object without not approved properties: Const person = {first name: 'Orpheus', Lastname: 'de Jong', phone: '+1 123-456-7890', email:’odj@email.tld ',};Const Disallowedproperties = ['Telephone', 'E -Mail'];Const allkeys = object.Keys (person);Const result = allkeys.reduce ((next, key) => {if (! Disallowedproperties.includes (key)) {return {... Next, [key]: person [key]};} else {return next;}}, {});// Result: // {first name: 'Orpheus'', Lastname: 'de jong'}

Example 5Reduces the keys from the 'person' object into a new object that only contains the properties, the key _not_ in the array 'unclopedproperties'.Unless they add the property name to the non -approved properties. If you would like to ensure that only certain properties are included in the resultExample 4is a better choice, butExample 5It is useful if you only have to make sure that certain properties are never included.

We can also create generic functions for reducing statements in examples 4 and 5:

Example 6:

-Code language-js-konst filterallowedobjectproperties = (object, ehereturn {... next, [key]: object [key]};} else {return next;}}, {});, disallowedproperties = []) => {return object.keys (keeys (objectobj) .reduce ((next, key) => {if (! disallowedproperties.includes (key)) {return {... key]: object [key]};} else {return next;}}, {});}

Merging of two objects, values from one:

Another frequent task is to bring together an object with another object that contains fallback or standard values for some have:

Example 7:

-Code Language-js--konst obj1 = {key1: 'value 1.1', key2: null, key3: 'value 1.3', key4: ''}; const obj2 = {key1: 'value 2.1', key2: 'Wert 2.2 ', Key3:' Wert 2.3 ', Key4:' Wert 2.4 ', Key5:' Wert 2.5 '}; const result = {... obj2, ... obj1}; // result: // {//KEY1: 'Wert 2.1', // key2: null, // key3: 'value 2.3', // key4: '', // key5: 'value 2.5' //};
(Video) Array reduce - Transform array into a single object

Example 7Creates a new object that contains the properties of 'object2' that has been overridden by the properties of 'object1'. The values of 'object2' serve as fallback or standard for the values of 'object1'.that the result keeps the "zero" and empty characters from "object1".Are definedValues. We probably didn't want this result, but "array.reduce" offers a solution.

Example 8:

-Code Language-Js-Konst Obj1 = {Key1: 'Value 1.1', Key2: Null, Key3: 'Value 1.3', Key4: ''};Const object2 = {Key1: 'Value 2.1', Key2: 'value 2.2', key3: 'value 2.3', key4: 'value 2.4', key5: 'value 2.5'};// spread the keys from both objects into an array.const allkeys = [... object.keys (object1)). Allkeys)];// Reduce the clear key into a new object that contains the value for every key from Obj1 and fall back to the value of object2 if // object1 [key] is wrong.= object1 [key] ||object2 [key];Return {... Next, [key]: value};}, {});// Result: // {// Key1: 'Value 1.1', // Key2: 'Value 2.2', // Key3: 'Value 1.3', // Key4: 'Value 2.4', // Key5: 'Value 2.5', //}

Note thatExample 8Use a naive strategy to decide when the fallback value should be used. The fallback value ('object2 [key]') is used when the preferred value ('object]') isNot correct. That means "undefined", "zero", an empty string "0 or" false ". This may not be appropriate for all cases, since one of these values is acceptable in your application. Overwork the fallback state as requiredReplace 'Const value = object] || object2 [key];' with 'Const value = (object]! == undefined && object1 [key]! == null)? Obj1 [key]: object2 [Key]; 'will ensure that the fallback value is only used if the preferred value is "undefined" or "zero".

Analyze search/query ponds:

After all, we look at another very common task that developers often use with a third party library to analyze search signs (sometimes referred to as query baths). Modern web browser offerUrlsearchparamsTo work quickly, but maybe you don't write a code for a browser or have to support the Internet Explorer, or you just want to try it for yourself, because so we learn. What is always your reason, "array.reduce" is your friend.

Firstly, we need a scene.

-Code Language-js --`const {search = ''} = uselocation (); `

However you get the search string, you have to prepare you first:

In the way of p:

-Code Language-Js-// Call the search string (/^\?/, '');// Share the string on the ampersand to create an array of key value signs = value%201 ',' key2 = value%202 ',' key3 = value%203 '];

Next, reduce the key value chains into an object by splinging them up on the same character.

(Video) Javascript Array Reduce (examples with object & array)

Example 9b:

-CODE Language-js--konst params = pairs.ReDuce ((nächstes, pair) => {const [Schlüssel, value] = pair.split ('='); const decodedValue = decodeuricomponent (value); return {...Next, [Schlüssel]: decodedValue};}, {}); // params: // {// key1: 'value 1', // key2: 'value 2', // key3: 'value 3'//}

The parser inKesmal P/GießenIf the job is done in many cases, but it is incomplete. Search signs can contain several values for each key, and this parser only keeps the last value for each key. Let us fix it.

Example 10:

-Code Language-Js-Konst search = '?Key1 = value%201 & key2 = value%202 & key3 = value%203.1 & key3 = value%203.2 & key3 = value%203.3 ';Const query = search.replace (/^\?/, '');Const pairs = query.plit ('&');Const params = pairs.rect ((next, pair) => {const [key, value] = pair.plit ('='); const decodedvalue = decodeuricomponent (value); constvalue = next [key]; let nextvalue;if (forebeat! Nextvalue = [forecast, decodedvalue];} else {nextvalue = decodedvalue;} return {... further, [key]: nextvalue}, {}); // params: // {//Key1: 'Value:' Value1 ', // Key2:' Value 2 ', // Key3: [' Value 3.1 ',' Value 3.2 ',' Value 3.3 '], //}

Example 10Creates the scene as well asIn the way of pThe difference is how the recall reduces the value for each key.

  1. The key value sign sequence (pair) is divided into = in order to obtain separate consequences for the key and the value.
  2. The value is decoded with decoduric component.
  3. The accumulator (next) is checked to determine whether there is an earlier value for the key.
  4. If there is an earlier value (previous value! == undefined), it is checked whether it is an array.
  5. If the previous value is an array, the decoded value is attached to it. (Nextvalue = [... previous value, decodedvalue];) If the previous value is not an array, a new array is created that contains the previous and decoded values. (NextValue = [previous value, decodedvalue];););
  6. If there is no previous value, the next value will be set to the decoded value (NextValue = Decodedvalue;)

The resulting Params object contains string values for Key1 and Key2 and an array that contains the three strings for key3 in the order in which they were displayed in the scene.

How we did itexample 1We can clarify the process by examining the condition of each iteration:

  1. Accumulator (next):{} (the initial value);Value (couple):'Key1 = value%201;Returns:{key1: 'value 1'};
  2. Accumulator:{key1: 'value 1'};Wert:'Key2 = value%202;Returns:{key1: 'value 1', key2: 'value 2'};
  3. Accumulator:{key1: 'value 1', key2: 'value 2'};Wert:'Key3 = value%203.1;Returns:{Key1: 'value 1', key2: 'value 2', key3: 'value 3.1'};
  4. Accumulator:{Key1: 'value 1', key2: 'value 2', key3: 'value 3.1'};Wert:'Key3 = value%203.2;Returns:{Key1: 'value 1', key2: 'value 2', key3: ['value 3.1', 'value 3.2']};
  5. Accumulator:{Key1: 'value 1', key2: 'value 2', key3: ['value 3.1', 'value 3.2']};Wert:'Key3 = value%203.3;Returns:{Key1: 'value 1', key2: 'value 2', key3: ['value 3.1', 'value 3.2', 'value 3.3']};


Array.Reduce is a kind of Swiss army knife with which you can solve a variety of problems. I encourage you to explore and try to use it in situations that you may not have taken into account.

(Video) Learn JavaScript Array Reduce In 10 Minutes


1. Javascript Nuggets - Reduce (object example)
(Coding Addict)
2. Array Reduce Explained With Examples - JavaScript Tutorial
3. The Magic of the reduce Array Method
(All Things JavaScript, LLC)
4. 5 Real Life Examples of Array Reduce in JavaScript
5. Array Reduce (Create New Object)
(Coding Addict)
6. JavaScript Array Reduce Method Practice in 5 Minutes
(James Q Quick)
Top Articles
Latest Posts
Article information

Author: Kelle Weber

Last Updated: 02/21/2023

Views: 6248

Rating: 4.2 / 5 (53 voted)

Reviews: 84% of readers found this page helpful

Author information

Name: Kelle Weber

Birthday: 2000-08-05

Address: 6796 Juan Square, Markfort, MN 58988

Phone: +8215934114615

Job: Hospitality Director

Hobby: tabletop games, Foreign language learning, Leather crafting, Horseback riding, Swimming, Knapping, Handball

Introduction: My name is Kelle Weber, I am a magnificent, enchanting, fair, joyous, light, determined, joyous person who loves writing and wants to share my knowledge and understanding with you.