Union of two array in Javascript

1

I need to join two arrays, from the server I get the next json. an array that says "data" that has 2 objects and another that is called "Doc" which is made up of 4 objects.

  

I want to create a matrix that contains 8 elements equal to the following (I did it by hand):

[
    {
        "idlibrosiva": 1,
        "iddoc": 1,
        "false": 1
    },{
        "idlibrosiva": 1,
        "iddoc": 2,
        "false": 1
    },{
        "idlibrosiva": 1,
        "iddoc": 3,
        "false": 1
    },{
        "idlibrosiva": 1,
        "iddoc": 4,
        "false": 1
    },{
        "idlibrosiva": 2,
        "iddoc": 1,
        "false": 1
    },{
        "idlibrosiva": 2,
        "iddoc": 2,
        "false": 1
    },{
        "idlibrosiva": 2,
        "iddoc": 3,
        "false": 1
    },{
        "idlibrosiva": 1,
        "iddoc": 4,
        "false": 1
    }]

This is what I get from the server in json

{
"status": "success",
"code": 200,
"data": [
    {
        "idlibrosiva": 1,
        "nombre": "Compras",
        "descripcion": "Art.-141 C.T.",
        "estado": 1
    },
    {
        "idlibrosiva": 2,
        "nombre": "Contribuyentes",
        "descripcion": "Artículo 141. C.T.- ",
        "estado": 1
    },

],
"Doc": [
    {
        "**iddoc**": 1,
        "documento": "CREDITO FISCAL",
        "descripcion": "ART. 107 C.C.",
        "estado": 1
    },
    {
        "iddoc": 2,
        "documento": "NOTA DE CREDITO",
        "descripcion": "ART. 110 C.C- ",
        "estado": 1
    },
    {
        "iddoc": 3,
        "documento": "FACTURA",
        "descripcion": "ART. 107 C.C.",
        "estado": 1
    },
    {
        "iddoc": 4,
        "documento": "NOTA DE DEBITO",
        "descripcion": "ART. 110 C.C.-",
        "estado": 1
    },
       ],
"mensaje": "Todo se ha cargado correctamente"}

I tried to do it like this:

this.Info = response.data;
                this.liva = response.data.filter((item) => item.estado == 1);
                this.docs = response.Doc.filter((item) => item.estado == 1);
                this.DocYlib = this.liva.map((item)=>{
                    const DocumentoUtilizado = new ModeloDocUtilizadosIVA();
                    DocumentoUtilizado.Libro = item.idlibrosiva;
                    DocumentoUtilizado.estado = false;
                    DocumentoUtilizado.documento = this.docs.map((item) => item.iddoc);
                    return DocumentoUtilizado;
                })

But the result has been:

    
asked by jeasomoza 31.03.2018 в 07:16
source

3 answers

3

The simplest way is to use spread separator:

const a = [{id: 1}, {id: 2}, {id: 3}];
const b = [{id: 4}, {id: 5}, {id: 5}];
const c = [...a, ...b];
console.log(c);
    
answered by 02.04.2018 в 22:38
2

already gave you a solution that uses the propagation operator (operator for iterables), I show you an older one (and therefore more compatible) with the use of the concat method that serves to join two arrangements:

var dataJson = {
"status": "success",
"code": 200,
"data": [
    {
        "idlibrosiva": 1,
        "nombre": "Compras",
        "descripcion": "Art.-141 C.T.",
        "estado": 1
    },
    {
        "idlibrosiva": 2,
        "nombre": "Contribuyentes",
        "descripcion": "Artículo 141. C.T.- ",
        "estado": 1
    },

],
"Doc": [
    {
        "**iddoc**": 1,
        "documento": "CREDITO FISCAL",
        "descripcion": "ART. 107 C.C.",
        "estado": 1
    },
    {
        "iddoc": 2,
        "documento": "NOTA DE CREDITO",
        "descripcion": "ART. 110 C.C- ",
        "estado": 1
    },
    {
        "iddoc": 3,
        "documento": "FACTURA",
        "descripcion": "ART. 107 C.C.",
        "estado": 1
    },
    {
        "iddoc": 4,
        "documento": "NOTA DE DEBITO",
        "descripcion": "ART. 110 C.C.-",
        "estado": 1
    },
       ],
"mensaje": "Todo se ha cargado correctamente"}

console.log(dataJson.data.concat(dataJson.Doc));
    
answered by 02.04.2018 в 22:46
1

I do not understand your question very well but maybe this can help you to create a new array by joining two arrays you can use in es6

var _a = [1, 2, 3];
var _b = [4, 5];
_a.push(..._b)

console.log(_a)
// [1, 2, 3, 4, 5]
    
answered by 02.04.2018 в 22:31