1

I have just this array :

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 = 856
value = user

My $.each code doesn't return the result I'm expecting, and I get instead:

856:user

I must be separate that with : to get key and value from this array.

My code is:

$.each(template_array, function(key, value) {
 console.log("key: " + "value: " + value);
});

How to access key and value without separate?

Gargaroz
3109 silver badges28 bronze badges
asked Sep 7, 2015 at 7:45
2
  • You have "value: " + value, why not "key: " + key Commented Sep 7, 2015 at 7:57
  • I think issue is in your array, is this the exact code you are using? Commented Sep 7, 2015 at 7:59

4 Answers 4

4

Just take Object.keys for the keys and Array.prototype.forEach for the values in plain Javascript.

var sArray = { 856: 'users', 857: 'avatars', 858: 'emails'};
Object.keys(sArray).forEach(function (key) {
 document.write('key: ' + key + ', value: ' + sArray[key] + '<br>');
});

answered Sep 7, 2015 at 7:57
Sign up to request clarification or add additional context in comments.

Comments

1

Just concatenate them using +

var template_array = {
 856: 'users',
 857: 'avatars',
 858: 'emails'
};
$.each(template_array, function(key, value) {
 console.log('key:' + key + ', value:' + value);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>

UPDATE : I think you have an array then use the following code

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]);
});

answered Sep 7, 2015 at 7:48

3 Comments

result is 0:856:users 1:857:avatars 2:858:emails i want to have key: 856 , value: users your code is not correct
@mahdipishguy : check the second snippet
The second snippet is exactly what the question asks not to have to do "I must be separate that with : to get key and value from this array."
0

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)
answered Sep 7, 2015 at 7:58

1 Comment

@mahdi pishguy: try this one
0

var template_array = {
 856: 'users',
 857: 'avatars',
 858: 'emails'
};
for (key in template_array) {
 console.log('key:' + key + ', value:' + template_array[key]);
});

answered Dec 8, 2020 at 9:28

1 Comment

Please don't post only code as an answer, but also provide an explanation of what your code does and how it solves the problem of the question. Answers with an explanation are usually more helpful and of better quality, and are more likely to attract upvotes

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.