-5

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: ""
    }
];
这种形式
2019-09-11 提问
3 个回答
1

已采纳
let result = arr.map(item => ({ name: item[0] || '', age: item[1] || '', content: item[2] || '' }))
1
       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;
        });
1
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);

撰写答案

推广链接