0

I have this function:

flotLinea:function(tareas){

var self = this;
console.info(tareas);
     var aTar = new Array();

        for(var i = 0;i<tareas.length;i++){

          var val = new Array(new Date(tareas[i].fecha_registro),tareas[i].porcentaje);
                aTar.push(val);
        }

    console.info(aTar);

},

Using console.info(tareas); print this :

enter image description here

And using console.info(aTar); print :

enter image description here

(The data from tareas always is changing because the data comes from a dropdown)

I need create an new array for each id_usu using the same data , how can I do this?

For example in this case I need an array for id_usu = 4 ( are two id_usu = 4, so i need the data where id_usu = 4) , one array with id_usu = 6 and one array with id_usu = 9

I need do this , because this data are for a chart, so, after , each user ( id_usu ) will be a different color in that chart.

5
  • 1
    Can you add an example of the desired output? Commented Aug 24, 2015 at 19:44
  • Use multidimensional arrays. stackoverflow.com/questions/966225/… Commented Aug 24, 2015 at 19:45
  • 1
    It's not really clear at all what you're trying to do? Do you want to join the values so that when the ID is the same, those values are somehow bundled together ? Commented Aug 24, 2015 at 19:46
  • @adeneo yes mm.. If I selected an option in a dropdown, the data from that option is save in "tareas" . With those data I need draw a chart, and I need a different color for each user, but that is after, now i need separate each user(id_usu) when the data is in that function ( sorry my english ) Commented Aug 24, 2015 at 19:53
  • @Jeanbf I've provided a solution with whatever I could understand from your problem. Commented Aug 24, 2015 at 22:15

1 Answer 1

1

From whatever I have understood form your problem statement and the code you have provided, I've provided a solution below.

flotLinea:function(tareas){

    var self = this;
    console.info(tareas);
     var aTar = new Array();
     var idArray = [];

        for(var i = 0;i<tareas.length;i++){
            if(idArray.indexOf(tareas[i].id_usu) == -1){
              var val = new Array(new Date(tareas[i].fecha_registro),
                  tareas[i].porcentaje);
                idArray.push(tareas[i].id_usu);
                aTar.push(val);
               }
             else{
                   for(var j = 0; j < aTar.length; j++){
                        if(tareas[i].id_usu == aTar[j][0].id_usu){
                           aTar[j].length = new Array(new Date(tareas[i].fecha_registro)
                      ,tareas[i].porcentaje);     
                         }
                     }
                 }
        }

    console.info(aTar);

}

I'm using Brute-Force kind of solution, performance can always be increased.

I've created on new array above as idArray to hold the unique id_usus, and am comparing if the current tareas[i].id_usu already is there in that array, if not push the new value to aTar array and tareas[i].id_usu to idArray, else loop over the aTar array and find the array which already has the object with the current tareas[i].id_usu and push the new values at aTar[j].length.

Sign up to request clarification or add additional context in comments.

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.