0

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" that 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:

enter image description here

3
  • How should the output look like? Commented Mar 31, 2018 at 13:04
  • Thanks for your help, the living room I want it to be: [ { "idlibrosiva": 1, "iddoc": 1, "false": 1 },] Commented Mar 31, 2018 at 13:11
  • Please check the answer I have shared and revert. Commented Mar 31, 2018 at 13:17

2 Answers 2

1

You can use reduce, map and concat

var output = obj.Doc.reduce( (acc, c) => 
     acc.concat( obj.data.map( s => 
      ({ idlibrosiva : s.idlibrosiva, iddoc : c.iddoc, "false": 1 })  //return new object from map callback
     ) //concat statement ended
) , []) //initialize accumulator to []

Demo

var obj = {
  "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"
};

var output = obj.Doc.reduce((acc, c) =>
  acc.concat(obj.data.map(s =>
      ({
        idlibrosiva: s.idlibrosiva,
        iddoc: c.iddoc,
        "false": 1
      }) //return new object from map callback
    ) //concat statement ended
  ), []) //initialize accumulator to []

console.log(output);

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

Comments

0

The requirement is to map each object from "data" to each object from "Doc".

Let us extract them individually.

const { data, Doc:doc } = response;
// aliasing Doc as doc, just for variable declaration convention.

Now iterate through both using forEach and push the desired objects in result array.

const result = [];
data.forEach(dataObj => {
  const { idlibrosiva } = data;
  doc.forEach(docObj => {
    const { iddoc } = doc;
    result.push({
      idlibrosiva,
      iddoc,
      'false': 1
    };
  };
};

console.log('result --> ', result);

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.