-
饮歌长啸
您可以使用Array.map和Object destructuring。let data = [{balance:4444,playerInfo:{age:"18",gender:"Male",level:"2",name:"Joe"}},{balance:3333,playerInfo:{age:"45",gender:"Male",level:"3",name:"Angel"}},{balance:2222,playerInfo:{age:"20",gender:"Female",level:"11",name:"Luce"}}]const formatData = (data) => { return data.map(({balance, playerInfo}) => ({ balance, level: playerInfo.level, name: playerInfo.name }))}console.log(formatData(data))let data = [{balance:4444,playerInfo:{age:"18",gender:"Male",level:"2",name:"Joe"}},{balance:3333,playerInfo:{age:"45",gender:"Male",level:"3",name:"Angel"}},{balance:2222,playerInfo:{age:"20",gender:"Female",level:"11",name:"Luce"}}]const formatData = (data) => { return data.map(({ balance, playerInfo: { level, name }}) => ({ balance, level, name }))}console.log(formatData(data));
-
白衣非少年
简短的'n甜蜜是这样的:let rawData = [{ balance: 4444, playerInfo: { age: "18", gender: "Male", level: "2", name: "Joe" }}, { balance: 3333, playerInfo: { age: "45", gender: "Male", level: "3", name: "Angel" }}, { balance: 2222, playerInfo: { age: "20", gender: "Female", level: "11", name: "Luce" }}]let formattedData = rawData.map(({ balance, playerInfo: { level, name } }) => ({ balance, level, name }))console.log(formattedData)
-
跃然一笑
您可以直接使用map方法进行转换。let input = [{ balance: 4444, playerInfo: { age: "18", gender: "Male", level: "2", name: "Joe" }}, { balance: 3333, playerInfo: { age: "45", gender: "Male", level: "3", name: "Angel" }}, { balance: 2222, playerInfo: { age: "20", gender: "Female", level: "11", name: "Luce" }}];let output = input.map(obj => ({ balance: obj.balance, level: obj.playerInfo?.level, name: obj.playerInfo?.name,}));console.log(output);