-1

I have a javascript array of object like this

let Object  = [
   {"Form Factor": "2.5 inch"},
   {"Capacity": "1 TB"},
   {"Memory Components": "NAND 3"}
]

i want to reduce it to just an object like this

resultObject = {
  "Form Factor" : "2.5 Inch",
  "Capacity": "1 TB",
  "Memory Components" : "NAND 3"
}

2 Answers2

3

You could spread the objects and assign them to a new object.

const
    array  = [{ "Form Factor": "2.5 inch" }, { "Capacity": "1 TB" }, { "Memory Components": "NAND 3" }],
    object = Object.assign({}, ...array);

console.log(object);
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
0

let data  = [
   {"Form Factor": "2.5 inch"},
   {"Capacity": "1 TB"},
   {"Memory Components": "NAND 3"}
]

const result=data.reduce((acc,curr)=>{
const keys=Object.keys(curr)
acc[keys[0]]=curr[keys[0]]
 return acc;
},{});

console.log(result)
.as-console-wrapper { max-height: 100% !important; top: 0; }
Nilesh Patel
  • 3,193
  • 6
  • 12