var x = {jason: ["2", "5"], alice: ["12", "35"], samuel: ["32", "132"]}
I am trying to add the numbers for each key so I am doing
var out = 0;
$.each(x, function (key, value) {
out += key + ':';
$.each(value, function (key, value) {
out += parseInt(value, 10);
});
});
console.log(out)
Here is my fiddle /
It is adding them as strings. So instead of jason = 7 I am getting jason = 25 and so on Cant seem to figure out what am I doing wrong
var x = {jason: ["2", "5"], alice: ["12", "35"], samuel: ["32", "132"]}
I am trying to add the numbers for each key so I am doing
var out = 0;
$.each(x, function (key, value) {
out += key + ':';
$.each(value, function (key, value) {
out += parseInt(value, 10);
});
});
console.log(out)
Here is my fiddle http://jsfiddle/sghoush1/1ezwwnm1/5/
It is adding them as strings. So instead of jason = 7 I am getting jason = 25 and so on Cant seem to figure out what am I doing wrong
Share Improve this question asked Jun 10, 2015 at 5:14 soumsoum 1,1593 gold badges21 silver badges49 bronze badges 3- What is your desired output? – Alex Pan Commented Jun 10, 2015 at 5:17
- @AlexPan -- Desired output would be jason:7 alice:47 samuel: 164 – soum Commented Jun 10, 2015 at 5:19
- 1 If u r using like javascript object var x = {jason: [2, 5]} or using as a json just convert from string to object like var x = JSON.stringify(x) – albert Jegani Commented Jun 10, 2015 at 5:23
11 Answers
Reset to default 3You are reconverting them to strings when adding them to the string out
. To prevent this, sum all numbers, then append it to the string.
var out = "";
$.each(x, function (key, value) {
out += key + ':';
var num = 0;
$.each(value, function (key, value) {
num += parseInt(value, 10);
});
out += num;
});
console.log(out)
Cycle over the keys, and reduce the values:
var x = { "jason": [ "2", "5" ], "alice": [ "12", "35" ], "samuel": [ "32", "132" ] };
Object.keys( x ).forEach( function ( key ) {
x[ key ] = x[ key ].reduce( function ( a, b ) {
return ( +a ) + ( +b );
});
});
document.body.textContent = JSON.stringify( x );
You start with out
being 0
. As soon as you add a string to it (in this instance, key
), it gets converted to a string; and after that you just keep concatenating further strings.
You need to separate the variable that you keep for string output from a variable that calculates a sum for you. This is a minimal modification of your code:
var out = "";
$.each(x, function (key, value) {
out += key + ':';
var sum = 0;
$.each(value, function (key, value) {
sum += parseInt(value, 10);
});
out += sum + " ";
});
console.log(out);
A bit nicer solution would involve functional approach:
var out = Object.keys(x).map(function(key) {
return key + ":" +
x[key].reduce(function(a, b) { return a + parseInt(b, 10); }, 0);
}).join(' ');
This gives you the output asked for in your ment:
var x = {jason: ["2", "5"], alice: ["12", "35"], samuel: ["32", "132"]}
var out = {};
$.each(x, function (key, value) {
out[key] = 0;
$.each(value, function (key2, value) {
out[key] += parseInt(value, 10);
});
});
console.log(out)
You are adding numbers to a string, Take integer temp as 0, add numbers to it, and then add it back to the string out
out =0
out+="key"
converts out to a string
try this:-
var x = {jason: ["2", "5"], alice: ["12", "35"], samuel: ["32", "132"]}
var out = "";
$.each(x, function (key, value) {
out += key + ':';
temp = 0;
$.each(value, function (key, value) {
temp += parseInt(value, 10);
});
out += temp;
});
console.log(out)
http://jsfiddle/1ezwwnm1/8/
try:
var x = {jason: ["2", "5"], alice: ["12", "35"], samuel: ["32", "132"]}
var out = '';
$.each(x, function (key, value) {
var counter = 0;
$.each(value, function (key1, value1) {
counter += parseInt(value1, 10);
});
out += key + ':' + counter + ' ';
});
console.log(out)
//jason:7 alice:47 samuel:164
You're appending strings in your example. I have updated your fiddle.
var x = {jason: ["2", "5"], alice: ["12", "35"], samuel: ["32", "132"]}
var out = 0;
$.each(x, function (key, value) {
out += key + ':';
var sum = 0;
$.each(value, function (key, value) {
sum += parseInt(value, 10);
});
out += sum;
});
check this out
var x = {jason: ["2", "5"], alice: ["12", "35"], samuel: ["32", "132"]};
var out = {};
for(var i in x){
out[i] = parseInt(x[i][0])+parseInt(x[i][1]);
}
console.log(out);
var x = {jason: ["2", "5"], alice: ["12", "35"], samuel: ["32", "132"]};
var str = 0;
$.each(x, function (key, value) {
str += key + ':';
var out = 0;
$.each(value, function (key, value) {
out += parseInt(value, 10);
});
str += out;
});
console.info(str);
is that your designed?
Apart from all the other solutions, you can also use .reduce()
to pute the sum.
var x = {
jason: ["2", "5"],
alice: ["12", "35"],
samuel: ["32", "132"]
}
var out = {};
$.each(x, function (key, value) {
out[key] = value.reduce(function (a, b) { return +a + +b; }); // <-- +a is same as parseInt(a, 10)
});
alert(JSON.stringify(out));
Here is a demo http://jsfiddle/dhirajbodicherla/1ezwwnm1/14/
You are appending all the the values in the string, you need to get the get the sum then append that to the key.
var x = {jason: ["2", "5"], alice: ["12", "35"], samuel: ["32", "132"]}
var out = '';
$.each(x, function (key, value) {
out += key + ':';
var Keyvalue = 0;
$.each(value, function (key, value) {
Keyvalue += parseInt(value, 10);
});
out += Keyvalue + " ";
});
console.log(out);
Fiddle here