简体   繁体   English

应该如何将数据推送到数组中过滤对象的一部分?

[英]How should data be pushed to a section of the filtered object in an array?

I'm getting a service from a server and the json that returns (see the reference) I need to disarm it to arm it according to the need that I have. 我从服务器和返回的json获得服务(参见参考资料)我需要根据我的需要撤防它以武装它。

JSON FROM SERVICE 服务中的JSON

[
  {
    "id_clasificacion":"1",
    "pj_clasificacion":"0",
    "pg_clasificacion":"0",
    "pe_clasificacion":"0",
    "pp_clasificacion":"0",
    "dg_clasificacion":"0",
    "ta_clasificacion":"0",
    "tr_clasificacion":"0",
    "jl_clasificacion":"0",
    "pts_clasificacion":"0",
    "id_equipo":"1",
    "nombre_equipo":"Real Madrid CF",
    "nombre_grupo":"Grupo A"
  },
  {
    "id_clasificacion":"2",
    "pj_clasificacion":"0",
    "pg_clasificacion":"0",
    "pe_clasificacion":"0",
    "pp_clasificacion":"0",
    "dg_clasificacion":"0",
    "ta_clasificacion":"0",
    "tr_clasificacion":"0",
    "jl_clasificacion":"0",
    "pts_clasificacion":"0",
    "id_equipo":"2",
    "nombre_equipo":"Barcelona CF",
    "nombre_grupo":"Grupo B",
  },
  {
    "id_clasificacion":"3",
    "pj_clasificacion":"0",
    "pg_clasificacion":"0",
    "pe_clasificacion":"0",
    "pp_clasificacion":"0",
    "dg_clasificacion":"0",
    "ta_clasificacion":"0",
    "tr_clasificacion":"0",
    "jl_clasificacion":"0",
    "pts_clasificacion":"0",
    "id_equipo":"3",
    "nombre_equipo":"Bayern Munchen",
    "nombre_grupo":"Grupo B"
  },
  {
    "id_clasificacion":"5",
    "pj_clasificacion":"0",
    "pg_clasificacion":"0",
    "pe_clasificacion":"0",
    "pp_clasificacion":"0",
    "dg_clasificacion":"0",
    "ta_clasificacion":"0",
    "tr_clasificacion":"0",
    "jl_clasificacion":"0",
    "pts_clasificacion":"0",
    "id_equipo":"4",
    "nombre_equipo":"Atletico de Madrid",
    "nombre_grupo":"Grupo C"
  },
  {
    "id_clasificacion":"6",
    "pj_clasificacion":"0",
    "pg_clasificacion":"0",
    "pe_clasificacion":"0",
    "pp_clasificacion":"0",
    "dg_clasificacion":"0",
    "ta_clasificacion":"0",
    "tr_clasificacion":"0",
    "jl_clasificacion":"0",
    "pts_clasificacion":"0",
    "id_equipo":"5",
    "nombre_equipo":"Chelsea FC",
    "nombre_grupo":"Grupo C"
  }
];

All these data have a common characteristic, which is called "nombre_grupo" , with this I intend to organize the data in the frontend as when they are ordered with a header in alphabetical order but in this case I will use the above mentioned: 所有这些数据都有一个共同的特征,称为"nombre_grupo" ,我打算在前端组织数据,就像按字母顺序排列标题一样,但在这种情况下,我将使用上面提到的:

|---------------------|------------------|
|        GRUPO A      |      GRUPO B     |
|---------------------|------------------|
|     Real Madrid     |     Chelsea      |
|     Barcelona       |  Manchester City |
|     Atl. Madrid     |  Manchester Untd |

This is the type of arrangement that I should arrive with the data captured from the service: 这是我应该从服务中获取的数据到达的安排类型:

[
 {
   grupo: Grupo A,
   equipos :
   [
     {
       id_clasificacion:'information',
       nombre_equipo:'information',
       pj_clasificacion:'information',
       pg_clasificacion:'information',
       pe_clasificacion:'information',
       pp_clasificacion:'information',
       dg_clasificacion:'information',
       ta_clasificacion:'information',
       tr_clasificacion:'information',
       jl_clasificacion:'information',
       pts_clasificacion:'information',
     }
   ]
 }
];

PROBLEM 问题

then, my problem is that when trying to push the data to the new arrangement I do not know how to push them to the corresponding section, in this case it inserts them as a new object, but it must belong to the corresponding category: 那么,我的问题是,当试图将数据推送到新的排列时,我不知道如何将它们推送到相应的部分,在这种情况下,它将它们作为新对象插入,但它必须属于相应的类别:

0: Object { name_group: "Grupo A", teams_group: (1) […] }​
1: Object { name_group: "Grupo B", teams_group: (1) […] }​
2: Object { name_group: "Grupo B", teams_group: (1) […] }​
3: Object { name_group: "Grupo C", teams_group: (1) […] }​
4: Object { name_group: "Grupo C", teams_group: (1) […] }

WHAT I'VE DONE? 我做了什么?

//Recorremos los datos obtenidos
for (var i = 0; i < data.length; i++)
  {
    //Evaluamos si el arreglo no tiene datos
    if (this.list_groups.length == 0)
    {
      //Empujamos el primer valor para llenar el arreglo
      this.list_groups.push(
        {
          name_group: data[i].nombre_grupo,
          teams_group: [
            {
              id_clasificacion:data[i].id_clasificacion,
              nombre_equipo:data[i].nombre_equipo,
              pj_clasificacion:data[i].pj_clasificacion,
              pg_clasificacion:data[i].pg_clasificacion,
              pe_clasificacion:data[i].pe_clasificacion,
              pp_clasificacion:data[i].pp_clasificacion,
              dg_clasificacion:data[i].dg_clasificacion,
              ta_clasificacion:data[i].ta_clasificacion,
              tr_clasificacion:data[i].tr_clasificacion,
              jl_clasificacion:data[i].jl_clasificacion,
              pts_clasificacion:data[i].pts_clasificacion
            }
          ]
        }
      );
    }
    else
    {
      //Filtramos el arreglo
      let approved = this.list_groups.filter(element => element.name_group == data[i].nombre_grupo);

      //Evaluamos si el arreglo obtuvo un valor vacio
      if (approved.length == 0)
      {
        //Empujamos el valor al arreglo final
        this.list_groups.push(
          {
            name_group: data[i].nombre_grupo,
            teams_group: [
              {
                id_clasificacion:data[i].id_clasificacion,
                nombre_equipo:data[i].nombre_equipo,
                pj_clasificacion:data[i].pj_clasificacion,
                pg_clasificacion:data[i].pg_clasificacion,
                pe_clasificacion:data[i].pe_clasificacion,
                pp_clasificacion:data[i].pp_clasificacion,
                dg_clasificacion:data[i].dg_clasificacion,
                ta_clasificacion:data[i].ta_clasificacion,
                tr_clasificacion:data[i].tr_clasificacion,
                jl_clasificacion:data[i].jl_clasificacion,
                pts_clasificacion:data[i].pts_clasificacion
              }
            ]
          }
        );
      }
      else
      {
        //Empujamos el valor al arreglo final
        this.list_groups.push(
          {
            name_group: data[i].nombre_grupo,
            teams_group: [
              {
                id_clasificacion:data[i].id_clasificacion,
                nombre_equipo:data[i].nombre_equipo,
                pj_clasificacion:data[i].pj_clasificacion,
                pg_clasificacion:data[i].pg_clasificacion,
                pe_clasificacion:data[i].pe_clasificacion,
                pp_clasificacion:data[i].pp_clasificacion,
                dg_clasificacion:data[i].dg_clasificacion,
                ta_clasificacion:data[i].ta_clasificacion,
                tr_clasificacion:data[i].tr_clasificacion,
                jl_clasificacion:data[i].jl_clasificacion,
                pts_clasificacion:data[i].pts_clasificacion
              }
            ]
          }
        );
      }
    }
  }
  console.log("Data: ", this.list_groups);

Logical sequence: 逻辑顺序:

1. Go through the arrangement obtained from the service
2. Evaluate if the secondary array has data equal to 0
   2.1 if you do not have data, you enter the first value obtained by default
   2.2 if it contains data, the secondary array is filtered to identify some match
    2.2.1 if it finds a match, then it will be added only to the found group section.
    2.2.2 if there is no match, the object is registered in the secondary array

Note: If you think there is something to improve in the attached code, let me know. 注意:如果您认为附加代码中有一些需要改进的地方,请告诉我们。 Thank you 谢谢

If I understand the question correctly, you are trying to put each item of the received JSON under their respective groups. 如果我正确理解了这个问题,那么您正试图将收到的JSON的每个项目放在各自的组中。

You can do this in the following way: 您可以通过以下方式执行此操作:

// Store JSON data from the service in 'data' variable.
var data; 

var groupsMap = new Map();

// Iterate over the data array.
data.forEach((element) => {

   const groupName = element.nombre_grupo;

   delete element.nombre_grupo;

   var value;

   // If group already exists in the map, get current value.
   if (groupsMap.has(groupName)) {

     value = groupsMap.get(groupName);

   } else {
       value = {
         'grupo': groupName,
         'equipos': []
       };
   }

   // Add current element to the group's equipos list.
   value.equipos.push(element);

   // Add updated value to the map.
   groupsMap.set(groupName, value);
});

Now, we have a map with group names as keys, and their respective values in the required format. 现在,我们有一个地图,其中组名作为键,以及所需格式的各自值。

We can iterate over the map to access those values. 我们可以迭代地图来访问这些值。

Hope this helps. 希望这可以帮助。 Let me know if you have any questions. 如果您有任何疑问,请告诉我。

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

相关问题 VueJS 将新对象推送到数组,数据不是响应式的 - VueJS pushed new object to array, data is not reactive 使用 axios 将数据推送到现有 object 中的数组 - Data pushed to array in existing object with axios 如何遍历推入数组的Object实例? - How to loop through instances of an Object that are pushed into an array? 当对象推送到数组时如何保持键的顺序 - how to keep the order of keys when when an object pushed to an array Javascript如何在推入数组的对象上创建唯一ID - Javascript how to create unique id on object that is pushed into an array 在数组内过滤数据并返回用组过滤的对象 - Filter data inside array and returns object filtered with group 如何获取在(子)属性数组的项目上过滤的对象数组的子集 - How to get subset of an object array filtered on items of (sub)property array 如何过滤 object 数组,其中包含逐项数组? javascript - How can I filtered array of object with array by item inside it ? javascript 如何在解决promise之前确保将所有数据都推送到阵列? - How to make sure that all data are pushed to the array before resolving promise? 如何从推入数组的数据表中删除数据(可能使用 pop)(javascript) - How to remove a data from datatables pushed an array (maybe with pop) (javascript)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM