数组转化对象数组,如下

let arr = [

    ['apple','banana','orange'],
    ['white','black','green'],
    ['cat','dog']
];

转化为
  let obj = [
    {
        name: "apple",
        age: "banana",
        content:"orange"
    },
    {
        name: "white",
        age: "black",
        content:"green"
    }, {
        name: "cat",
        age: "dog",
        content: ""
    }
];
这种形式
阅读 198
评论 2019-09-11 提问
    3 个回答
    let result = arr.map(item => ({ name: item[0] || '', age: item[1] || '', content: item[2] || '' }))
    评论 赞赏 2019-09-11
             let arr = [
                  ['apple','banana','orange'],
                  ['white','black','green'],
                  ['cat','dog']
             ];
             const keys = ["name", "age", "content"];
             const result =  arr.map((item) => {
                  let temp = {};
                  keys.forEach((key, index) => {
                      temp[key] = item[index] ? item[index] : "";
                  });
                  return temp;
              });
      评论 赞赏 2019-09-11
        luozz
        • 5.6k
        let arr = [["apple", "banana", "orange"], ["white", "black", "green"], ["cat", "dog"]];
        const result = arr.reduce((total, cur) => {
            let [name = "", age = "", content = ""] = cur;
            total.push({ name, age, content });
            return total;
        }, []);
        console.log(result);
        评论 赞赏 2019-09-11
          撰写回答

          登录后参与交流、获取后续更新提醒