How to convert array to object in jquery?

inputOptions: "SRB": "Serbia", "UKR": "Ukraine", "HRV": "Croatia" ,How could I convert my array to the needed format, considering that the key will be the same as the value?

So, something like this would be the result:

"aaa123": "aaa123", "Açucena": "Açucena", "Braúnas": "Braúnas", <...>I have tried JSON.stringify, but the output is not what I need:

"<<"aaa123","Açucena","Braúnas","C. Fabriciano","gege","gegeq2","Ipatinga","Joanésia","Mesquita","Rodoviário","teste","teste2","Timóteo","Tomatoentro","ts">>"


javascript jquery arrays sweetalert
tóm tắt
Improve this question
Follow
asked Jul 23, năm 2016 at 0:27
*

PhiterPhiter
14k1414 gold badges5050 silver badges8282 bronze badges
1
địa chỉ a bình luận |

4 Answers 4


Sorted by: Reset to mặc định
Highest score (default) Date modified (newest first) Date created (oldest first)
26
This can be done with a simple reduce call:


// thử nghiệm datavar source = <"someValue1", "someValue2", "someValue3", "other4", "other5">;// This is the "conversion" partvar obj = source.reduce(function(o, val) o = val; return o; , );// kiểm tra outputdocument.write(JSON.stringify(obj));
tóm tắt
Improve this answer
Follow
answered Jul 23, năm 2016 at 0:47

*

AmitAmit
43.9k88 gold badges7373 silver badges106106 bronze badges
4
địa chỉ a phản hồi |
4
Object.assign( , ...

Bạn đang xem: How to convert array to object in jquery?

<"value1", "value2", "value3", "value4", "value5">.map((value) => ( : value, )),)returns

value1: "value1", value2: "value2", value3: "value3", value4: "value4", value5: "value5"No common ESlint rules error.


nội dung
Improve this answer
Follow
answered Aug 3, 2019 at 7:07
*

onlyureionlyurei
43955 silver badges1212 bronze badges
địa chỉ a bình luận |
3
if you are using jQuery;

$.extend(, <"x", "y", "z">);if you not;

Object.assign(, my_array);Another example;


var arr = < name: "a", value: "b", other: "c", name: "d", value: "e", other: "f">;const obj = arr.reduce((total, current) => total = current.value; return total;, );console.log(obj);
mô tả
Improve this answer
Follow
edited Jan 21 at 9:42

*

Mo.
24k3535 gold badges145145 silver badges210210 bronze badges
answered Jul 23, năm 2016 at 0:54
*

Cem ArguvanlıCem Arguvanlı
6241010 silver badges1515 bronze badges
1
địa chỉ a bình luận |
2
The key lớn this is that you can assign properties using the obj<"string"> technique:

function ArrayToObject(arr){ var obj = ; for (var i = 0;i
giới thiệu
Improve this answer
Follow
answered Jul 23, 2016 at 0:33
user3163495user3163495
1,40822 gold badges1717 silver badges3535 bronze badges
0
địa chỉ a bình luận |

Your Answer


Thanks for contributing an answer lớn Stack Overflow!

Please be sure lớn answer the question. Provide details và share your research!

But avoid

Asking for help, clarification, or responding lớn other answers.Making statements based on opinion; back them up with references or personal experience.

Xem thêm: cetaphil moisturizer

To learn more, see our tips on writing great answers.


Draft saved
Draft discarded

Sign up or log in


Sign up using Google
Sign up using Facebook
Sign up using e-mail and Password
Submit

Post as a guest


Name
email Required, but never shown


Post as a guest


Name
email

Required, but never shown


Post Your Answer Discard

By clicking “Post Your Answer”, you agree lớn our terms of service, privacy policy and cookie policy


Not the answer you're looking for? Browse other questions tagged javascript jquery arrays sweetalert or ask your own question.


The Overflow Blog
Featured on Meta
Related
5173
What is the most efficient way to lớn deep clone an object in JavaScript?
4634
How vị I check if an array includes a value in JavaScript?
3186
How can I convert a string lớn boolean in JavaScript?
3811
How lớn insert an tòa tháp into an array at a specific index (JavaScript)
3896
How bởi vì I chạy thử for an empty JavaScript object?
3516
How vị I correctly clone a JavaScript object?
3718
Checking if a key exists in a JavaScript object?
2363
Get all chất lượng values in a JavaScript array (remove duplicates)
3731
Loop through an array in JavaScript
5367
For-each over an array in JavaScript
Hot Network Questions more hot questions

Question feed
Subscribe lớn RSS
Question feed to lớn subscribe lớn this RSS feed, copy và paste this URL into your RSS reader.


lang-js
Stack Overflow
Products
Company
Stack Exchange Network
Site kiến thiết / biểu tượng logo © 2022 Stack Exchange Inc; user contributions licensed under cc by-sa. Rev2022.6.10.42345


Your privacy

By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device & disclose information in accordance with our Cookie Policy.