#32 后端数据处理


  • 0

    @labike 不是让你写成函数么。。。那个就是函数参数啊。。。而且测试用例里面的数据不止这一个,可能还是随机生成的。。


  • -1

    @CodeHz

    我为什么非要写成函数,为什么一定要让data作为函数的参数呢!你为什么不试试下面这个呢,这样并没有问题!

    data.rows.map((row) => row.reduce((rowData, value, i) => {
    	rowData[data.metaData[i].name] = value
    	return rowData
    }, {}))
    
    const data ={}
    

  • 0
    administrators

    @labike 因为题目要求写成函数


  • 0

    const parseData = data => {
      var keys = data.metaData.map(info=>info.name);
      return data.rows.map(row=>{
        var obj = {};
        keys.forEach((k,i)=>{
            obj[k] = row[i];
        });
        return obj;
      });
    }
    

  • 0

    const parseData = ({ rows, metaData }) =>
        rows.map(v => {
          const newObj = {}
          for (let i in v) {
            newObj[metaData[i].name] = v[i]
          }
          return newObj
     })
    

  • 0

    简洁版

    const parseData = (data) => {
      const metaDataArr = data['metaData'].map(d => d.name);
      const rows = data['rows'];
      return rows.map(row =>{
        return {
            [metaDataArr[0]]:row[0],
            [metaDataArr[1]]:row[1],
            [metaDataArr[2]]:row[2],
            [metaDataArr[3]]:row[3]
          }
        }
      )
    }
    

  • 0

    const parseData = (data) => {
      let rows = data.rows;
      let metaData = data.metaData;
      let arr = [];
      rows.forEach((v) => {
        let obj = {};
        metaData.forEach((key, index) => {
          obj[key['name']] = v[index];
        });
        arr.push(obj);
      });
      return arr;
    }
    

  • 0

    const parseData = (data) => {
    let resultArr = [];
    data.rows.map((rowItem,rowsIndex) => {
    let obj = {};
    data.metaData.map((metaItem,metaIndex) => {
    obj[metaItem.name] = rowItem[metaIndex];
    });
    resultArr.push(obj);
    });
    return resultArr;
    }


  • 0

    forEach写法
    const parseData = (data) => {
    let a = []
    data.rows.forEach(function(item, index) {
    a[index] = {}
    data.metaData.forEach(function(itemInner, indexInner) {
    a[index][itemInner.name] = item[indexInner]
    }, this);
    }, this);
    return a
    }


  • 0

    const parseData = (data) => data.rows.map(x => {
      return data.metaData.reduce((acc, meta, i) => ({ ...acc, [meta.name]: x[i] }) , {})
    })
    

  • 0

    es5写法
    const parseData = (data) => {
    var arr = []
    for(var i =0;i<data.rows.length;i++){
    var obj = {}
    for (var j =0;j<data.metaData.length;j++){
    obj[data.metaData[j].name] = data.rows[i][j]
    }
    arr.push(obj)
    }
    return arr
    }


  • 0

    const parseData = (data) => {
       var rows = data.rows, metaData = data.metaData, arr = [];
       for (let i = 0; i <= rows.length - 1; i++) {
           let obj = {};
           for (let j = 0; j <= rows[i].length - 1; j++) {
               obj[metaData[j].name] = rows[i][j];
           }
           arr.push(obj);
       }
    
       return arr;
    };

  • 0

    const parseData = (data) => {
    return data.rows.map(item => data.metaData.reduce((obj, col, index) => obj[col.name] = item[index], {}))
    }


  • 0

    const parseData = (data) => {
      const {rows, metaData} = data
      return rows.map(item => {
        let obj = {}
        metaData.forEach((meta, index) => {
          obj[meta.name] = item[index]
        })
        return obj
      })
    }

  • 0

    一行版的格式化版

    const parseData = ({rows, metaData}) => {
      return rows.map(row => {
        // reduce中转入2个参数,第一个是处理函数,第二个是默认值(空对象)
        return row.reduce((obj, value, index) => {
          // assign只要传入2个参数即可,一行版第一个空对象多余了,因为上面已给了默认值(空对象)
          return Object.assign(obj, {
            [metaData[index].name]: value
          })
        }, {}) // 这里,累加器的默认值是个空对象
      })
    }
    

  • 0

    const parseData = (data) => {
      const { rows, metaData } = data;
      if (!Array.isArray(rows) || !Array.isArray(metaData) || rows.length === 0 || metaData.length === 0) {
        return []
      }
      return rows.map(row => {
        return row.reduce((total, current, currentIndex) => {
          const key = metaData[currentIndex].name;
          total[key] = current;
          return total
        }, {})
      });
    };
    

登录后回复
 

与 ScriptOJ 的连接断开,我们正在尝试重连,请耐心等待