3

I've got an object with different values .
let arr = [{"Personal": "1000"},{"sport": "2100"},{"Industrial": "1200"},{"Commercial": "2300"},
{"Fashion": "1300"},{"Documentary": "2600"}]

How can I foreach them and then put them in an object and pick a name for them like this :

"photo_type": {
    "Personal": "1000",
    "sport": "2100",
    "Industrial": "1200",
    "Commercial": "2300",
    "Fashion": "1300",
    "Documentary": "2600"
}

I dont want them to be like 0 and 1 and 2.

1

2 Answers 2

6

You could create an object with Object.assign and by spreading the array.

let array = [{ Personal: "1000" }, { sport: "2100" }, { Industrial: "1200" }, { Commercial: "2300" }, { Fashion: "1300" }, { Documentary: "2600" }],
    object = Object.assign({}, ...array);

console.log(object);

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

1 Comment

Doh! Way better than my multi-step operation.
0

You can try this

const arr = [{"Personal": "1000"},{"sport": "2100"},{"Industrial": "1200"},{"Commercial": "2300"},{"Fashion": "1300"},{"Documentary": "2600"}]
let result = {}
arr.forEach(member => {result = {...result, ...member}}

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.