#32 后端数据处理


  • 0
    administrators

    从某数据库接口得到如下值:

    {
      rows: [
        ["Lisa", 16, "Female", "2000-12-01"],
        ["Bob", 22, "Male", "1996-01-21"]
      ],
      metaData: [
        { name: "name", note: '' },
        { name: "age", note: '' },
        { name: "gender", note: '' },
        { name: "birthday", note: '' }
      ]
    }
    

    rows 是数据,metaData 是对数据的说明。现写一个函数 parseData,将上面的对象转化为期望的数组:

    [
      { name: "Lisa", age: 16, gender: "Female", birthday: "2000-12-01" },
      { name: "Bob", age: 22, gender: "Male", birthday: "1996-01-21" },
    ]
    

    本题来源:github


  • 1
    管理员

    分享一下代码。。总感觉有更加简洁的写法

    const parseData = (data) => {
      let arr = [];
      data.rows.map((row, index) => {
      	let obj = {};
      	row.map((ele, i) => {
      		obj[data.metaData[i].name] = ele
    
      	});
        arr.push(obj);
      });
      return arr;
    }
    

  • 2

    @陈小俊

    const parseData = ({rows, metaData}) => rows.map(row => row.reduce((p, c, i) => Object.assign({}, p, {[metaData[i].name]: c}), {}))
    

    一行版


  • 0

    const data = {
                rows: [
                    ["Lisa", 16, "Female", "2000-12-01"],
                    ["Bob", 22, "Male", "1996-01-21"]
                ],
                metaData: [{
                    name: "name",
                    note: ''
                }, {
                    name: "age",
                    note: ''
                }, {
                    name: "gender",
                    note: ''
                }, {
                    name: "birthday",
                    note: ''
                }]
            }
    data.rows.map((row) => row.reduce((rowData, value, i) => {
    	rowData[data.metaData[i].name] = value
    	return rowData
    }, {}))
    

    Uncaught SyntaxError: Identifier 'data' has already been declared

    这里用const data为什么会报错?


  • 0

    @labike data不应该是参数么,你又用const是为啥呢


  • 0

    @CodeHz

    假如你不把

    {
                rows: [
                    ["Lisa", 16, "Female", "2000-12-01"],
                    ["Bob", 22, "Male", "1996-01-21"]
                ],
                metaData: [{
                    name: "name",
                    note: ''
                }, {
                    name: "age",
                    note: ''
                }, {
                    name: "gender",
                    note: ''
                }, {
                    name: "birthday",
                    note: ''
                }]
            }
    

    赋给data,那么data.rows.map()里的data是哪里来的?


  • 0

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


  • 0

    @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], {}))
    }


登录后回复
 

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