+ 1

How do I push Array elements into an Object ? (Using ES5 syntaxes)

I have two arrays : var array1 = [ 5,7,9 ] var array2 = [ "Ann", "Ben", "Ken" ] And an empty object: var obj={ } how do I make array1 to be the object keys and array2 to be the object values. The output should look like this: Ann : 5, Ben : 7, Ken : 9 Please help me here :)

10th May 2020, 3:30 PM
Stonny
Stonny - avatar
5 Respuestas
+ 4
var array1 = [ 5,7,9 ] var array2 = [ "Ann", "Ben", "Ken" ] var obj = {}; obj = array2.reduce( (a,v,i) => ({...a, [v]:array1[i]}), obj); https://code.sololearn.com/WIWplaTFKqTD/?ref=app
10th May 2020, 4:05 PM
Calviղ
Calviղ - avatar
10th May 2020, 4:07 PM
Arb Rahim Badsa
Arb Rahim Badsa - avatar
+ 3
var columns = ["Date", "Number", "Size", "Location", "Age"]; var rows = ["2001", "5", "Big", "Sydney", "25"]; var result = rows.reduce (function(result, field, index){ result[columns[index]] = field; return result; }, {}) console.log(result); Output { Date: "2001", Number: "5", Size: "Big", Location: "Sydney", Age: "25" }
10th May 2020, 3:53 PM
Ayush Kumar
Ayush Kumar - avatar
+ 1
list1=['ayush','kumar','singh'] list2=['1','2','3'] dictonary=dict(zip(list1,list2)) print(dictonary) Use the above syntex to achive the results o sorry I think it may help you https://stackoverflow.com/questions/50802528/convert-js-array-to-dictionary-hashmap
10th May 2020, 3:38 PM
Ayush Kumar
Ayush Kumar - avatar
+ 1
I am working with javascript.
10th May 2020, 3:45 PM
Stonny
Stonny - avatar