-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"
}
1
  • So did you try using .reduce? What happened? Commented Dec 24, 2020 at 11:06

2 Answers 2

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);

Sign up to request clarification or add additional context in comments.

Comments

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; }

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.