【问题标题】:Error when creating array of objects in a javascript loop在 javascript 循环中创建对象数组时出错
【发布时间】:2021-05-04 05:05:57
【问题描述】:

我在创建对象数组时遇到问题。问题是您应该只创建 2 个对象,但最终创建了 8 个对象,因为属性的每个值都插入到一个新对象中。 我有一个称为列的对象数组,它是动态的,因为值和属性正在变化


我需要的是根据行属性的值创建对象,例如只包含相同行值的对象。

 example: [   
    { string1: 'CONTRAPARTIDA',string2: 'NOMBRE',string3: null, number1: 'Orden', number2: 'VALOR ENV.' number3: null},      object with row value = 1   
    { string1: 'CONTRAPARTIDA',string2: 'NOMBRE',string3: null, number1: 'Orden', number2: 'VALOR ENV.' number3: null},      //object with row value = 2    

使用这段代码,我尝试创建我的 2 个对象,但不幸的是,创建的对象比预期的要多。

 for(let a in columnas){
 //I can't do this validation because as the column array is dynamic I don't know how many rows come in //the array.
     //if(columnas[a].fila ==1)  
    
         arrayFinal.push({
      
    // validations to match the campoDb with the new array property and insert the value
                         string1: (columnas[a].campoDb==='STRING1' ? columnas[a].nombre : null),
                         string2:(columnas[a].campoDb==='STRING2' ? columnas[a].nombre : null),
                         string3: null,
                         number1: (columnas[a].campoDb==='NUMBER1' ? columnas[a].nombre : null),
                         number2:(columnas[a].campoDb==='NUMBER2' ? columnas[a].nombre : null),
                         number3: null,
                         });


但是作为图像中的附件,创建了 8 个对象,并且只填充了一个属性,正确的做法是只有 2 个具有这些值的对象。

 Example: [   
{ string1: 'CONTRAPARTIDA',string2: 'NOMBRE',string3: null, number1: 'Orden', number2: 'VALOR ENV.' number3: null},   
{ string1: 'CONTRAPARTIDA',string2: 'NOMBRE',string3: null, number1: 'Orden', number2: 'VALOR ENV.' number3: null},

const columnas = [
  { nombre: 'ORDEN', fila: 1, campoDb: 'NUMBER1' },
  { nombre: 'CONTRAPARTIDA', fila: 1, campoDb: 'STRING1' },
  { nombre: 'NOMBRE', fila: 1, campoDb: 'STRING2' },
  { nombre: 'VALOR ENV.', fila: 1, campoDb: 'NUMBER2' },
  { nombre: 'ORDEN', fila: 2, campoDb: 'NUMBER1' },
  { nombre: 'CONTRAPARTIDA', fila: 2, campoDb: 'STRING1' },
  { nombre: 'NOMBRE', fila: 2, campoDb: 'STRING2' },
  { nombre: 'VALOR ENV.', fila: 2, campoDb: 'NUMBER2' },
];  

let arrayFinal = [];
 for(let a in columnas){
  if(columnas[a].fila ==1) //no puedo hacer esta validacion xq como el arreglo columna es dinamico no se cuantas filas vengan en el areglo
    arrayFinal.push({
     string1: (columnas[a].campoDb==='STRING1' ? columnas[a].nombre : null),
     string2:(columnas[a].campoDb==='STRING2' ? columnas[a].nombre : null),
     string3: null,
     number1: (columnas[a].campoDb==='NUMBER1' ? columnas[a].nombre : null),
     number2:(columnas[a].campoDb==='NUMBER2' ? columnas[a].nombre : null),
     number3: null,
     });arrayFinal


 }
   console.log(arrayFinal);

我需要一些解决方案或建议,说明我可以做什么或可以从我的代码中更改什么,以使我想做的事情更容易。

【问题讨论】:

    标签: javascript for-loop matrix javascript-objects


    【解决方案1】:
    const columnas = [
      { nombre: 'ORDEN', fila: 1, campoDb: 'NUMBER1' },
      { nombre: 'CONTRAPARTIDA', fila: 1, campoDb: 'STRING1' },
      { nombre: 'NOMBRE', fila: 1, campoDb: 'STRING2' },
      { nombre: 'VALOR ENV.', fila: 1, campoDb: 'NUMBER2' },
      { nombre: 'ORDEN', fila: 2, campoDb: 'NUMBER1' },
      { nombre: 'CONTRAPARTIDA', fila: 2, campoDb: 'STRING1' },
      { nombre: 'NOMBRE', fila: 2, campoDb: 'STRING2' },
      { nombre: 'VALOR ENV.', fila: 2, campoDb: 'NUMBER2' },
    ];  
    
    // 1. split the columns into two groups based on 'fila'
    const rows = [1, 2].map((n) => columnas.filter(row => row.fila === n) )
    
    // 2. iterate through each group
    const results = rows.map(row => {
      // these are the values of 'campoDb' we are looking for
      return [ "STRING1", "STRING2", "STRING3", "NUMBER1", "NUMBER2", "NUMBER3" ]
          .reduce((acc, campoDb) => {
          // 2a. try to find an object that contains "campoDb"
          const item = row.find(_ => _.campoDb === campoDb)
    
          // 2.b if found, use that object's 'nombre', else use null
          acc[campoDb.toLowerCase()] = item ?  item.nombre : null
    
          return acc
      }, {})
    })
    
    console.log(results)
    

    【讨论】:

      【解决方案2】:

      您必须根据条件过滤列并将结果缩减为一个对象。

      const columnas = [
        { nombre: 'ORDEN', fila: 1, campoDb: 'NUMBER1' },
        { nombre: 'CONTRAPARTIDA', fila: 1, campoDb: 'STRING1' },
        { nombre: 'NOMBRE', fila: 1, campoDb: 'STRING2' },
        { nombre: 'VALOR ENV.', fila: 1, campoDb: 'NUMBER2' },
        { nombre: 'ORDEN', fila: 2, campoDb: 'NUMBER1' },
        { nombre: 'CONTRAPARTIDA', fila: 2, campoDb: 'STRING1' },
        { nombre: 'NOMBRE', fila: 2, campoDb: 'STRING2' },
        { nombre: 'VALOR ENV.', fila: 2, campoDb: 'NUMBER2' },
      ]; 
      
      var result = [1, 2].map(i => columnas.filter(c => c.fila === i).reduce((acc, cur) => { acc[cur.campoDb] = cur.nombre; return acc }, {}));
      
      console.log(result)

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-07-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-08-22
        • 2020-08-08
        • 2018-11-23
        • 2020-07-02
        相关资源
        最近更新 更多