var sArray = {856:”users”, 857:”avatars”, 858:”emails”};and I want to use forEach in a way to get key and value from that:
key = 856value = userMy $.each code doesn”t return the result I”m expecting, and I get instead:
856:userI must be separate that with : to get key and value from this array.
Đang xem: Looping javascript arrays using for, foreach & more
My code is:
$.each(template_array, function(key, value) { console.log(“key: ” + “value: ” + value);});How to access key and value without separate?
Trending sort Trending sort is based off of the default sorting method — by highest score — but it boosts votes that have happened recently, helping to surface more up-to-date answers.
It falls back to sorting by highest score if no posts are trending.
Switch to Trending sort
Highest score (default) Trending (recent votes count more) Date modified (newest first) Date created (oldest first)
var sArray = { 856: “users”, 857: “avatars”, 858: “emails”};Object.keys(sArray).forEach(function (key) { document.write(“key: ” + key + “, value: ” + sArray
var template_array = { 856: “users”, 857: “avatars”, 858: “emails”};$.each(template_array, function(key, value) { console.log(“key:” + key + “, value:” + value);});
var template_array = <"856: users", "857: avatars", "858: emails">;template_array.forEach(function(v) { v = v.split(“:”); console.log(“key:” + v<0> + “, value:” + v<1>);});
result is 0:856:users 1:857:avatars 2:858:emails i want to have key: 856 , value: users your code is not correct
try this one
var template_array = { 856: “users”, 857: “avatars”, 858: “emails”};var date = <>;$.each(template_array,function (key , val){ date.push({key:key, value:val}) });console.log(date)
var template_array = { 856: “users”, 857: “avatars”, 858: “emails”};for (key in template_array) { console.log(“key:” + key + “, value:” + template_array
Thanks for contributing an answer to Stack Overflow!
Please be sure to answer the question. Provide details and share your research!
But avoid …
Asking for help, clarification, or responding to other answers.Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Post Your Answer Discard
By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy
Site design / logo © 2022 Stack Exchange Inc; user contributions licensed under CC BY-SA. rev2022.8.25.42913
Your privacy
By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy.