Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

foreach array with one key [duplicate]


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.

like image 668
cfarhad Avatar asked Jul 19 '26 17:07

cfarhad


1 Answers

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);
like image 115
Nina Scholz Avatar answered Jul 22 '26 06:07

Nina Scholz



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!