0

I want loop through 1 to 21 and then use this loop numbers for getting an Array of Strings ['e1.wkh',...'e21.wkh']. But right now the only value I got is ['e21.wkh'].

function calculateStats() {

    var obj = {}

    var deviceId = "34534";

    for (var i = 1; i <= 21; i++) {

        var loo = i.toString();

        console.log(loo);

        obj[deviceId] = ['e' + loo + '.kwh'];

        console.log(obj[deviceId]);
    }

}
2
  • The reason is that you are always using deviceId as the element name here: obj[deviceId] = ['e' + loo + '.kwh']; So you always overwrite the last value of obj[deviceId] How should the elements be named instead? Commented Jun 22, 2015 at 11:40
  • index of your obj remain's same for for whole loop that is your problem in statement obj[deviceId] = ['e' + loo + '.kwh']; you have to Change this and make index incremental to store all the values generated by your loop Commented Jun 22, 2015 at 11:41

4 Answers 4

1

Replace below line

obj[deviceId] = ['e' + loo + '.kwh'];

With

(obj[deviceId])?obj[deviceId].push('e' + loo + '.kwh'):obj[deviceId]=['e' + loo + '.kwh'];
Sign up to request clarification or add additional context in comments.

Comments

1

Here is a possible solution:

The issue was you were overwriting your obj[deviceId].

function calculateStats() {

    var obj = {}

    var deviceId = "357803045265259@rayleigh";

    obj[deviceId] = [];

    for (var i = 1; i <= 21; i++) {

        var loo = i.toString();

        console.log(loo);

        obj[deviceId].push('e' + loo + '.kwh');

        console.log(obj[deviceId]);
    }

}

https://jsfiddle.net/q651uhde/

5 Comments

obj[deviceId] should be equal to the array of 'e' + loo + '.kwh'
I have updated with a jsfiddle. It is an array of 'e' + loo + '.kwh'.
I have got such an error: Uncaught TypeError: Cannot read property 'push' of undefined
Did you add this line before the for loop: obj[deviceId] = []; ?
Alright it is working and both the ternary operation : (obj[deviceId])?obj[deviceId].push('e' + loo + '.kwh'):obj[deviceId]=['e' + loo + '.kwh'];
0

function calculateStats() {

var obj = [];

var deviceId = "1111@rayleigh";

for (var i = 1; i <= 21; i++) {

    var loo = i.toString();

    console.log(loo);

    obj.push('e' + loo + '.kwh');


}

console.log(obj);

} obj carries your array, is that what you want?

Comments

0

Set up the array first and then push each string to it. No need to convert the index to a string as it will be coerced to a string when you push it to the array anyway.

function calculateStats() {

  var obj = {};
  var deviceId = "357803045265259@rayleigh";
  obj[deviceId] = [];

  for (var i = 1; i <= 21; i++) {
    obj[deviceId].push('e' + i + '.kwh');
  }

}

DEMO

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.