Using the in
keyword you're looping over each key in the object and those are string types and when using the +
operator on a string will cause it to do a concatenation assignment.
If you want to perform an arithmetic assignment both the left and the right value of the operator have to be an integer type. A solution would be to try to parse the string to an integer:
console.log(parseInt("0") + 1); // 1
However...
You should be looping over the value instead:
var data = [1, 2, 3];
for(var key in data){
console.log('a: data['+ key + '] = ' + data[key]);
}
for (var i = 0; i < data.length; i++) {
console.log('b: data['+ i + '] = ' + data[i]);
}
data.forEach(function(value, index){
console.log('c: data[' + index + '] = ' + value);
});
You could use the ES6 method to loop over the value alone:
for(let v of [1, 2, 3]) {
console.log(v);
}
The drawback of this method is the incompatibility of older android devices since its a somewhat new standard.
If you're using jQuery, you can also take a look at $.each as it allows for a key, value iteration in a nice one-liner that is compatible with older devices.
$.each([1, 2, 3], function(index, value) {
console.log( index + ": " + value );
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>