+ 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 :)
5 Respostas
+ 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
+ 5
Nnabuife Chidozie Chinonso Here is another simple way :))
https://code.sololearn.com/WAd8863WdjMZ/?ref=app
+ 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" }
+ 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
+ 1
I am working with javascript.