简体   繁体   中英

How can I put the results in an array after use each in javascript?

Hi I have this function:

changeTarea: function() {

    var self = this;
    $("#select_tarea_id").change(function() {
        var id_tarea = $("#select_tarea_id").val();

        $.each(self.objTareasFlot, function(index,value) {

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

                if(value[i].Id == id_tarea) {

                    self.objTareasFlotFinal['id']=value[i].Id;
                    self.objTareasFlotFinal['id_pro']=value[i].Id_proyecto;
                    self.objTareasFlotFinal['tarea']=value[i].nombre_tarea;
                    self.objTareasFlotFinal['porcentaje']=value[i].porcentaje;

                    console.info(self.objTareasFlotFinal);
                }
            }
       });

    });
}

And the function print :

在此处输入图片说明

But I need the 3 results in one array

for example :

在此处输入图片说明

How can I do that with that function? Sorry for my english I did try to explain of the better way

You can declare an array and push populated object into it. Something like this:

changeTarea: function(){

var self = this;
var container[];

$("#select_tarea_id").change(function() {
    var id_tarea = $("#select_tarea_id").val();

$.each(self.objTareasFlot, function(index,value) {

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

        if(value[i].Id == id_tarea){

        self.objTareasFlotFinal['id']=value[i].Id;
        self.objTareasFlotFinal['id_pro']=value[i].Id_proyecto;
        self.objTareasFlotFinal['tarea']=value[i].nombre_tarea;
        self.objTareasFlotFinal['porcentaje']=value[i].porcentaje;

        console.info(self.objTareasFlotFinal);
        container.push(self.objTareasFlotFinal);
        }

    }


 });

});},

Create an array variable var result = []; inside your function.

Within your loop push() the objects into it;

results.push(self.objTareasFlotFinal);
    var newArray = [];

    self.objTareasFlotFinal['id']=value[i].Id;
                        self.objTareasFlotFinal['id_pro']=value[i].Id_proyecto;
                        self.objTareasFlotFinal['tarea']=value[i].nombre_tarea;
                        self.objTareasFlotFinal['porcentaje']=value[i].porcentaje;

    newArray.push(self.objTareasFlotFinal);

// console.log should show the results
console.log(newArray);

If this array is meant to be global and accessible outside the function, you might want to define newArray outside the function first and remove the var from it within the function.

Then every time somebody runs the function, a new object is added to the array.

Alternatively, you could just return the array as the final value:

     var newArray = [];

        self.objTareasFlotFinal['id']=value[i].Id;
                            self.objTareasFlotFinal['id_pro']=value[i].Id_proyecto;
                            self.objTareasFlotFinal['tarea']=value[i].nombre_tarea;
                            self.objTareasFlotFinal['porcentaje']=value[i].porcentaje;

        newArray.push(self.objTareasFlotFinal);

return newArray;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM