1

I try to create a dynamic JSON whith data returned from my database : What I want to have :

var myJSON = {  
              "value" : [ { 
"icon_name": "value", 
"factor_name": "value", 
"number": "value", 
"priority": "value" 
}, 
{ 
"icon_name": "value", 
"factor_name": "value", 
"number": "value", 
"priority": "value" 
} ],
                  "value" : [ { 
"icon_name": "value", 
"factor_name": "value", 
"number": "value", 
"priority": "value" 
}, 
{ 
"icon_name": "value", 
"factor_name": "value", 
"number": "value", 
"priority": "value"  
} ],
...
  };

Each "value" is dynamic and depend of my sql response. My sql response looks like this.

[ RowDataPacket {
    row_names: 587,
    icon_name: 'small_event',
    factor_name: 'Petit évènement',
    number: 10,
    priority: 1 },
  RowDataPacket {
    row_names: 587,
    icon_name: 'cold',
    factor_name: 'Température basse',
    number: 10,
    priority: 7 },
  RowDataPacket {
    row_names: 587,
    icon_name: 'rain',
    factor_name: 'Pluie',
    number: 10,
    priority: 12 },
  RowDataPacket {
    row_names: 588,
    icon_name: 'small_event',
    factor_name: 'Petit évènement',
    number: 10,
    priority: 1 },
  RowDataPacket {
    row_names: 588,
    icon_name: 'cold',
    factor_name: 'Température basse',
    number: 10,
    priority: 7 }]

There are several values (icon_name, factor_name, ...) for one row_names. I want to regroup all these values for each unique row_names.

Here my code (rows[3] is my sql output):

var result = []
var r_names = "";
var j = 0;
var id = "";

for(i = 0; i < rows[3].length; i++) {
  if(r_names != rows[3][i].row_names) {
    j = 0;
    id = ''+rows[3][i].row_names;
    result[id] = [];

    r_names = rows[3][i].row_names
  }
  result[id].push({});
  result[id][j]["icon_name"] = rows[3][i].icon_name,
  result[id][j]["factor_name"] = rows[3][i].factor_name,
  result[id][j]["number"] = rows[3][i].number,
  result[id][j]["priority"] = rows[3][i].priority
  j++;
}

Here my result :

 ...
  ,
  ,
  ,
  ,
  ,
  ,
  ,
  ,
  ,
  ,
  ,
  [ { icon_name: 'small_event',
      factor_name: 'Petit évènement',
      number: 10,
      priority: 1 },
    { icon_name: 'temperate',
      factor_name: 'Température modéré',
      number: 10,
      priority: 7 },
    { icon_name: 'rain',
      factor_name: 'Pluie',
      number: 10,
      priority: 12 } ],
  ,
  ,
  ,
  ,
  ,
  ,
...
  ,
  ,
  ,
  ,
  ,
  ,
  [ { icon_name: 'small_event',
      factor_name: 'Petit évènement',
      number: 10,
      priority: 1 },
    { icon_name: 'temperate',
      factor_name: 'Température modéré',
      number: 10,
      priority: 7 },
    { icon_name: 'rain',
      factor_name: 'Pluie',
      number: 10,
      priority: 12 } ] ]

I have no idea why I have so much ','. I didn't succeed to find my error. Have you an idea ?

1 Answer 1

3

The problem is, you take value of row_names as index for the array and that is at start 587. With that index, you generate a sparse array.

id = ''+rows[3][i].row_names;
result[id] = [];

To prevent this, you could use an object, then you could use row_names's value as key for the result.

var result = {};
Sign up to request clarification or add additional context in comments.

2 Comments

In fact i tried to convert my row_names as a string to avoid errors, but it didn't works. I'm sorry but I'm not sure to totally understand your solution. How can I use object here. It will change nothing no ?
change var result = [] to var result = {}

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.