0

i'm having some trouble:

I need put some value of a JSON list into a separate JSON object. I have a JSON list that contains rows of data and a JSON object. I would like to traverse se the list and map the rows into different objects in the JSON.

The first and the last elements are empty, then there are a finite number of names each followed by an indefinite number of couples "date", "vote" (see the pastebin for better understanding).

I then would like to collect all the votes under a certain name in the list to that object in JSON.

How i can do that in javascript? I should change the structure of the JSON?

That is the json:

{
    "materie": [
        {
            "elettrotecnica_ed_elettronica": {
                "voti": []
            }
        },

        {
            "lingua_e_letteratura_italiana": {
                "voti": []
            }
        },

        {
            "lingua_inglese": {
                "voti": []
            }
        },

        {
            "matematica_e_complementi_di_matematica": {
                "voti": []
            }
        },

        {
            "scienze_motorie_e_sportive": {
                "voti": []
            }
        },

        {
            "sistemi_automatici": {
                "voti": []
            }
        },

        {
            "storia": {
                "voti": []
            }
        },

        {
            "tecnologie_e_progettazione_di_sistemi_elettrici_ed_elettronici": {
                "voti": []
            }
        }
]
}

And that is the array:

[ '',
  'elettrotecnica ed elettronica',
  '10',
  '7½',
  '8½',
  '5',
  '9',
  '7',
  '4',
  '7-',
  '7+',
  '6',
  '7½',
  'lingua e letteratura italiana',
  '6½',
  '5+',
  '6',
  '7+',
  '5',
  '6½',
  'lingua inglese',
  '6+',
  '7',
  '7-',
  '7½',
  '7',
  '7+',
  'matematica e complementi di matematica',
  '4½',
  '9½',
  '7',
  '6½',
  '9',
  '7½',
  '4',
  '9',
  '7½',
  '7',
  '8',
  '8',
  'scienze motorie e sportive',
  '5',
  '7',
  '6',
  '9',
  '7',
  '10',
  '8',
  '10',
  '7',
  'sistemi automatici',
  '8',
  '8½',
  '10',
  '10',
  '8½',
  '9½',
  '10',
  '10',
  '10',
  '9-',
  'storia',
  '4',
  '6',
  '5½',
  'tecnologie e progettazione di sistemi elettrici ed elettronici',
  's',
  '7-',
  '7',
  '8',
  '8-',
  '7+',
  '7½',
  '6',
  '6',
  '8',
  '6+',
  '7-',
  '7-',
  '6',
  '7',
  '6',
  '8½',
  '' ]
TeraTon
  • 435
  • 6
  • 15
LucaLumetti
  • 339
  • 4
  • 15
  • if you want reduce array to object, you can use [Array.reduce](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Array/Reduce) function – Grundy Apr 17 '15 at 11:30
  • @T.J.Crowder ok sorry, i've fixed – LucaLumetti Apr 17 '15 at 11:35
  • So you want to parse a list containing in sequence a name and then number values ? – TeraTon Apr 17 '15 at 11:37
  • @Grundy thanks, now i see what i can do with that function – LucaLumetti Apr 17 '15 at 11:38
  • @TeraTon see the array, i have 8 string (for example "elettrotecnica ed elettronica") followed by numbers. i want put that numbers in the JSON, under the voice of string that the numbers follow(for example, i put ['10','7½', '8½', '5', '9', '7', '4', '7-', '7+', '6', '7½'] on { materie:{"elettronica_ed_elettrotecnica":{ voti:[HERE] }, ...other string... }. i need to do that with all string. – LucaLumetti Apr 17 '15 at 11:52
  • added an answer below, not 100% correct as i just noticed you don't need to store the dates. – TeraTon Apr 17 '15 at 11:57
  • possible duplicate of [Convert javascript object or array to json for ajax data](http://stackoverflow.com/questions/19970301/convert-javascript-object-or-array-to-json-for-ajax-data) – durron597 Apr 17 '15 at 19:58

2 Answers2

0

If I understood correctly, you just want to parse the array containing names and votes and insert these into a javascript object (JSON object, whatever). ONe way to do this then is to just iterate over the array if you know that the format is always name+date,vote you could check if the first item in the array is a char, if it is its a name, then find the matching object from the JSON array and after this iterate all the values into it until an another char item is found. A fast hack below, not 100% that the loop functions correctly but you should get the idea:

//a function to check for numbers
function isNumber(n) {
   return !isNaN(parseFloat(n)) && isFinite(n);
}   
function isEmpty(str) {
   return (!str || 0 === str.length);
}
var jsonObject //the object we're appending the values to in your example
var name
var i = 1 //ignore the first empty value
for( i < voteArray.length() ){
  //check if a number 
  if (!isNumber(voteArray[i].charAt(0)) )
    if ( isEmpty(voteArray[i])){
      //end of list, break.
      break;
    }
    name = voteArray[i];
    //parse next two values and check if number
    for( i < voteArray.length()){
        if(isNumber(voteArray[i+1]) && isNumber(voteArray[i+2])){
            //add the next numbers to JSON

            jsonObject[name].voti = //sum votes
            jsonOBject[name].date = //append date
            i=i+2 //increment i
        }
        else{
          if(isNumber(voteArray[i]){
            i = i+1;
            break;
          }else (isNumber(voteArray[i]){
            i = i+2;
            break;
          }
        }
    }
}
TeraTon
  • 435
  • 6
  • 15
0

Your array is a little bit hard to handle. This is a workaround to do what you ask for. However note that a materia must have more than 2 chars and the vote less than 3 chars or it will not works properly

var array = [ '',
  'elettrotecnica ed elettronica',
  '10',
  '7½',
  '8½',
  '5',
  '9',
  '7',
  '4',
  '7-',
  '7+',
  '6',
  '7½',
  'lingua e letteratura italiana',
  '6½',
  '5+',
  '6',
  '7+',
  '5',
  '6½',
  'lingua inglese',
  '6+',
  '7',
  '7-',
  '7½',
  '7',
  '7+',
  'matematica e complementi di matematica',
  '4½',
  '9½',
  '7',
  '6½',
  '9',
  '7½',
  '4',
  '9',
  '7½',
  '7',
  '8',
  '8',
  'scienze motorie e sportive',
  '5',
  '7',
  '6',
  '9',
  '7',
  '10',
  '8',
  '10',
  '7',
  'sistemi automatici',
  '8',
  '8½',
  '10',
  '10',
  '8½',
  '9½',
  '10',
  '10',
  '10',
  '9-',
  'storia',
  '4',
  '6',
  '5½',
  'tecnologie e progettazione di sistemi elettrici ed elettronici',
  's',
  '7-',
  '7',
  '8',
  '8-',
  '7+',
  '7½',
  '6',
  '6',
  '8',
  '6+',
  '7-',
  '7-',
  '6',
  '7',
  '6',
  '8½',
  '' ];

var jsonObj = {'materie':[]}
var i = -1;
var currentKey = '';
array.map(function(val){
  if (val.length > 0) {
    if (val.length > 2) {
      currentKey = val.replace(/\s/g,'_');
      var newObj = {};
      newObj[currentKey] = {voti:[]}
      jsonObj['materie'].push(newObj);
      i++;
    } else {
      jsonObj['materie'][i][currentKey]['voti'].push(val);
    }
  }
});

document.write(JSON.stringify(jsonObj));
DrKey
  • 3,365
  • 2
  • 29
  • 46