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:
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 CT", "estado": 1 }, { "idlibrosiva": 2, "nombre": "Contribuyentes", "descripcion": "Artículo 141. CT- ", "estado": 1 }, ], "Doc": [{ "iddoc": 1, "documento": "CREDITO FISCAL", "descripcion": "ART. 107 CC", "estado": 1 }, { "iddoc": 2, "documento": "NOTA DE CREDITO", "descripcion": "ART. 110 CC- ", "estado": 1 }, { "iddoc": 3, "documento": "FACTURA", "descripcion": "ART. 107 CC", "estado": 1 }, { "iddoc": 4, "documento": "NOTA DE DEBITO", "descripcion": "ART. 110 CC-", "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);
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);
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.