亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

使用javascript從上到下獲取json數據

使用javascript從上到下獲取json數據

小唯快跑啊 2024-01-18 16:59:02
我正在嘗試從上到下獲取 json 數據
查看完整描述

3 回答

?
一只甜甜圈

TA貢獻1836條經驗 獲得超5個贊

嘗試這個:


    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);


查看完整回答
反對 回復 2024-01-18
?
不負相思意

TA貢獻1777條經驗 獲得超10個贊

使用reduce和Object.assign


const 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))


查看完整回答
反對 回復 2024-01-18
?
蕪湖不蕪

TA貢獻1796條經驗 獲得超7個贊

一個簡單的Array.reduce應該可以實現你的目標。


const obj = {

  // json data shown in your question

}


const result = obj.data.reduce(

  (acc, cur) => ({

    ...cur.persons,

    ...acc,

  }),

  {}

);



查看完整回答
反對 回復 2024-01-18
  • 3 回答
  • 0 關注
  • 195 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號