0

I am trying to create a JS object which has this structure

{ node1: 
  [ 'test1.1',
   'test1.2'],
  node2:
   ['test2.1',
   'test2.2']
}

This is my code

    for (var k = 0; k < keys.length; i++){
            key = keys[k];
            var result = {};
            var r = [];
            for (var i = 0; i < elements.length; i++){
                r.push(elements[i]);
            }
            result[key] = r;
}

the result looks a bit different from my expectation and is not a valid JSON:

{ node1: 
   [ 'test1.1',
     'test1.2'] }
{ node2: 
   [ 'test2.1',
     'test2.2' ] }

I am not sure what is wrong about the code.

Luca Kiebel
  • 9,790
  • 7
  • 29
  • 44
jhg
  • 9
  • 2
    You are working exclusively with Javascript objects and then talk about JSON. The latter is inspired by the former, but they're not the same. Makes the question confusing. – RemcoGerlich Aug 27 '18 at 13:18
  • But the problem is perhaps that you reset the result to an empty object each time you go through the outer loop. – RemcoGerlich Aug 27 '18 at 13:19
  • 1
    Picking up on what RemcoGerlich said: JSON is a *textual notation* for data exchange. [(More here.)](http://stackoverflow.com/a/2904181/157247) If you're dealing with JavaScript source code, and not dealing with a *string*, you're not dealing with JSON. :-) – T.J. Crowder Aug 27 '18 at 13:19
  • 1
    Please update your question with a [mcve] demonstrating the problem, ideally a **runnable** one using Stack Snippets (the `[<>]` toolbar button; [here's how to do one](https://meta.stackoverflow.com/questions/358992/)). – T.J. Crowder Aug 27 '18 at 13:20
  • Welcome to Stack Overflow! Please take the [tour] (you get a badge!), have a look around, and read through the [help], in particular [*How do I ask a good question?*](/help/how-to-ask) – T.J. Crowder Aug 27 '18 at 13:21
  • Add the complete code in the question. – Suhail Akhtar Aug 27 '18 at 13:21
  • 1
    @T.J.Crowder: the first is one object, the second is two objects. Apparently he also prints inside the loop and then doesn't show us that. – RemcoGerlich Aug 27 '18 at 13:21
  • @RemcoGerlich - Ah, thanks! Very subtle. – T.J. Crowder Aug 27 '18 at 13:21

1 Answers1

1

Declare var result = {}; outside the for loop and it will work as currently a new object is created inside the loop.

var result = {};
for (var k = 0; k < keys.length; k++) {
  key = keys[k];
  var r = [];
  for (var i = 0; i < elements.length; i++) {
    r.push(elements[i]);
  }
  result[key] = r;
}

You also have i++ in the first loop so change that to k++ otherwise there will be a infinite loop.

Ankit Agarwal
  • 30,378
  • 5
  • 37
  • 62
  • This certainly starts them off the right way, but we have to know what `keys` and `elements` are. This will just put the same elements in both objects. – T.J. Crowder Aug 27 '18 at 13:24
  • Once they fix that, then if the OP wants to create JSON (since it seems he/she may be printing this out or something), they'd use `JSON.stringify(result)` to create the JSON to output. – T.J. Crowder Aug 27 '18 at 13:24