Problems with 2 arrays

1

I currently have the following code:

articulo.prototype.crearEspecificAsoc = function ($table) {
    var lista = this.getColumnasEspec();
    //this.View.initTablaEspecificacion($table, this.lstEspecificacionesView, lista); 
    this.View.initTablaEspecificacion($table, this.lstEspecificacionesAsociadas, lista); 
    console.log(lista, ' lista');

    var lista2 = this.lstEspecificacionesAsociadas;

    var lista3 = lista2.sort();
    lista2[4] = lista.field;



    console.log(this.lstEspecificacionesAsociadas, 'this.lstEspecificacionesAsociadas');
    console.log(lista2, ' lista2');

    return true;
};

articulo.prototype.getColumnasEspec = function () {
    var arr = [];
    var objColumns = {};

    objColumns['field'] = 'IdNew';
    objColumns['title'] = 'Id';
    arr.push(objColumns);

    _.forEach(this.lstEspecificaciones, function (item, index) {

        objColumns = {};

            objColumns['field'] = item.Nombre;//'Valor';//Valor[i]/*.Valor*/;
            objColumns['title'] = item.Nombre;
            arr.push(objColumns);

            //objColumns = {};
            //objColumns['field'] = 'Valor';//NuevasColumnasAsoc[i].Valor;
            //objColumns['title'] = Espec_list.Nombre;
            //arr.push(objColumns);
        });

        return arr;
    }

What I want to do is, that 2 arrays become 1, I explain:

Both in the second and third, it shows the same array1, what you want to do is place it in a single column, which says more or less what the first says, that columns are made that say the following: Id = 2, Name = Celestial Test, Weight = 85. I appreciate the help.

This is the answer I'm getting now.

    
asked by Broodwing009 07.11.2017 в 22:15
source

1 answer

1

Ok. Suppose you have a dictionary of the type:

var diccionario= {
  2 : 'Nombre',
  6 : 'Peso'
};

Which tells us that the Espec_Id 2 indicates the name and Espec_Id 6 indicates the weight.

The list variable has been a list of columns, equivalent to declare:

var columnas = [
    {field:"IdNew", title:"Id"},
    {field:"Nombre", title:"Nombre"},
    {field:"Peso", title:"Peso"}
];

in your final list you want the first element to be the columns, so you could put

var listado = [columnas];

Then you have a list (which would be your lista2 ) with elements that represent attributes and values of the same IdNew . For example

var elementos = [
    {Espec_Id: 2, IdNew: 2, valor: 'Prueba 1'},
    {Espec_Id: 6, IdNew: 2, valor: '85'},
    {Espec_Id: 2, IdNew: 3, valor: 'Prueba 2'},
    {Espec_Id: 6, IdNew: 3, valor: '44'},
];

To convert that list into something more usable (and since you're using underscore ), you can use the reduce method)

var lista_agrupada = _.reduce(lista2, function(acumulado, elemento) {
  acumulado[elemento.idNew] = acumulado[elemento.idNew] || {}; 
  acumulado[elemento.idNew]['idNew'] = elemento.idNew;

  // el significado de Espec_Id es tomado del diccionario
  var llave = diccionario[elemento.Espec_Id];
  acumulado[elemento.idNew][llave]=elemento.valor;
  return acumulado;
},{});

To end this loop you would have lista_agrupada is an object:

{
    2:  {IdNew:2, Nombre: 'Prueba 1', Peso: '85' },
    3:  {IdNew:3, Nombre: 'Prueba 2', Peso: '44' },
}

You can iterate over that object and add each element to your listado

_.each(lista_agrupada,function(elemento) {
    var fila = [];
    fila.push({field: 'idNew',  title: elemento.idNew});
    fila.push({field: 'Nombre',  title: elemento.Nombre});
    fila.push({field: 'Peso',  title: elemento.Peso});
    listado.push(fila);
});

At the end of which listado would have the form

[
    [
        {field:"IdNew", title:"Id"},
        {field:"Nombre", title:"Nombre"},
        {field:"Peso", title:"Peso"}
    ],
    [
        {field:"IdNew", title:"2"},
        {field:"Nombre", title:"Prueba 1"},
        {field:"Peso", title:"85"}
    ],
    [
        {field:"IdNew", title:"3"},
        {field:"Nombre", title:"Prueba 2"},
        {field:"Peso", title:"44"}
    ]
]

I must say that almost all this I did guessing what you wanted to say with your question, and that your data structure is really very messy.

    
answered by 08.11.2017 / 13:18
source