代码之家  ›  专栏  ›  技术社区  ›  S S

如何将新数据合并到旧阵列?

  •  0
  • S S  · 技术社区  · 6 年前

    0: {id:1, product_name: 'aaa', val1:1, val2:2, val3:4}
    1: {id:2, product_name: 'bbb', val1:1, val2:2, val3:4}
    2: {id:3, product_name: 'ccc', val1:1, val2:2, val3:4}
    3: {id:4, product_name: 'ddd', val1:1, val2:2, val3:4}
    

    我想得到val1、val2和val3的和,并显示在total列中,并将“%”符号附加到这3个值上。

    temp = temp.map((row) => {
        const newRow= {};
        let total = 0;
    
        total = row['val1'] +
                row['val2'] +
                row['val3'] +
    
        newRow['total'] = total;
    
        newRow['val1'] = row['val1'] + '%';
        newRow['val2'] = row['val2'] + '%';
        newRow['val3']   = row['val3'] + '%';
    
        return newRow;
    });
    

    使用此代码,我只返回数组中的val1、val2和val3。如何附加其他42列并获得最终结果。

    3 回复  |  直到 6 年前
        1
  •  1
  •   Nina Scholz    6 年前

    您可以获取所需密钥的数组,并为单个数组映射新属性。

    var data = [{ id: 1, product_name: 'aaa', val1: 1, val2: 2, val3: 4 }, { id: 2, product_name: 'bbb', val1: 1, val2: 2, val3: 4 }, { id: 3, product_name: 'ccc', val1: 1, val2: 2, val3: 4 }, { id: 4, product_name: 'ddd', val1: 1, val2: 2, val3: 4 }],
        keys = ['val1', 'val2', 'val3'],
        result = data.map(o => Object.assign(...keys.map(
            (t => k => ({ total: t += o[k], [k]: o[k] + '%' }))
            (0)
        )));
    
    console.log(result);
    .as-console-wrapper { max-height: 100% !important; top: 0; }

    对于对象,使用rest。

    var data = [{ id: 1, product_name: 'aaa', val1: 1, val2: 2, val3: 4 }, { id: 2, product_name: 'bbb', val1: 1, val2: 2, val3: 4 }, { id: 3, product_name: 'ccc', val1: 1, val2: 2, val3: 4 }, { id: 4, product_name: 'ddd', val1: 1, val2: 2, val3: 4 }],
        result = data.map(({ id, product_name, ...o, total = 0 }) => Object.assign(
            { id, product_name },
            ...Object.entries(o).map(([k, v]) => (total += v, { [k]: v + '%' })),
            { total }
        ));
    
    console.log(result);
    .作为控制台包装{最大高度:100%!重要;顶部:0;}
        2
  •  1
  •   vrintle Jake    6 年前

    你可以这样做

    var temp = [{id:1, product_name: 'aaa', val1:1, val2:2, val3:4},
    {id:2, product_name: 'bbb', val1:1, val2:2, val3:4},
    {id:3, product_name: 'ccc', val1:1, val2:2, val3:4},
    {id:4, product_name: 'ddd', val1:1, val2:2, val3:4}];
    
    temp = temp.map(row => {
      const newRow = row;
      let total = 0;
    
      total = parseInt(row['val1']) +
        parseInt(row['val2']) +
        parseInt(row['val3']);
    
      newRow['total'] = total;
    
      newRow['val1'] = row['val1'] + '%';
      newRow['val2'] = row['val2'] + '%';
      newRow['val3'] = row['val3'] + '%';
    
      return newRow;
    });
    $("#result").append(JSON.stringify(temp) + "<br />");
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <div id='result'>
    
    </div>
        3
  •  0
  •   Titovskyi    6 年前

    您需要将const newRow的定义更改为:

    let temp = this.some.map((row) => {
      const newRow = {...row};
      let total = 0;
      total = row['val1'] +
              row['val2'] +
              row['val3'];
      newRow['total'] = total;
    
      newRow['val1'] = row['val1'] + '%';
      newRow['val2'] = row['val2'] + '%';
      newRow['val3'] = row['val3'] + '%';
    
      return newRow;
    });