简体   繁体   English

Javascript中两个数组的连接

[英]join of two array in Javascript

I need to join two arrays, from the server I get the next json. 我需要连接两个数组,从服务器获取下一个json。 an array that says "data" that has 2 objects and another that is called "Doc" that is made up of 4 objects. 一个表示“数据”的数组,该数组具有2个对象,另一个数组称为“ Doc”,该数组由4个对象组成。

I want to create a matrix that contains 8 elements equal to the following (I did it by hand): 我想创建一个包含8个等于以下元素的矩阵(我手工完成):

[
    {
        "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 这是我从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 您可以使用reducemapconcat

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. 现在,使用forEach进行遍历,并将所需对象推入结果数组。

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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