使用javascript从上到下获取json数据

我正在尝试从上到下获取 json 数据



小唯快跑啊
浏览 51回答 3
3回答

一只甜甜圈

尝试这个:    const finalResult = {};    const data = {        "data": [          {            "persons": {              "Dupont nicolas": "President",              "George frimaolo": "engineer",              "Tiprana masilo": "football player"            }          },          {            "persons": {              "Balack martini": "author",              "Dupont nicolas": "Student",              "Joseph Allen": "dentist"            }          },          {            "persons": {              "Fred Samanta": "baker",              "Romero flagipi": "actor",              "Fred Samanta": "astronaut",              "Joseph Allen": "pilot",              "Anne Hedley": "teacher"            }          }        ]      }        for (let i in data.data) {      for (let j in data.data[i].persons) {        finalResult[j] = finalResult[j] ? finalResult[j] : data.data[i].persons[j];      }    }        console.log(finalResult);

不负相思意

使用reduce和Object.assignconst combine = (arr) =>  arr.reduce((acc, { persons }) => Object.assign(acc, persons), {});const data = [  {    persons: {      "Dupont nicolas": "President",      "George frimaolo": "engineer",      "Tiprana masilo": "football player",    },  },  {    persons: {      "Balack martini": "author",      "Dupont nicolas": "Student",      "Joseph Allen": "dentist",    },  },  {    persons: {      "Fred Samanta": "baker",      "Romero flagipi": "actor",      "Fred Samanta": "astronaut",      "Joseph Allen": "pilot",      "Anne Hedley": "teacher",    },  },];console.log(combine(data))

芜湖不芜

一个简单的Array.reduce应该可以实现你的目标。const obj = {  // json data shown in your question}const result = obj.data.reduce(  (acc, cur) => ({    ...cur.persons,    ...acc,  }),  {});
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript