第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

合并數(shù)組內(nèi)的JSON對(duì)象

合并數(shù)組內(nèi)的JSON對(duì)象

紅顏莎娜 2021-04-08 16:13:59
我有一個(gè)JSON數(shù)組如下[{"Name" : "Arrow","Year" : "2001"},{"Name" : "Arrow","Type" : "Action-Drama"},{ "Name" : "GOT","Type" : "Action-Drama"}]我正在嘗試將其轉(zhuǎn)換為[  {     "Name" : "Arrow",    "Year" : "2001",    "Type" : "Action-Drama",  },  {   "Name" : "GOT",   "Type" : "Action-Drama"  }]任何幫助,不勝感激。
查看完整描述

3 回答

?
精慕HU

TA貢獻(xiàn)1845條經(jīng)驗(yàn) 獲得超8個(gè)贊

您可以使用reduce()和findIndex()


let data = [

{"Name" : "Arrow",

"Year" : "2001"

},


{"Name" : "Arrow",

"Type" : "Action-Drama"

},

{ "Name" : "GOT",

"Type" : "Action-Drama"

}

]



let res = data.reduce((ac,a) => {

  let ind = ac.findIndex(x => x.Name === a.Name);

  ind === -1 ? ac.push({...a}) : ac[ind] = {...ac[ind],...a};

  return ac;



},[])

console.log(res)


查看完整回答
反對(duì) 回復(fù) 2021-04-22
?
慕的地8271018

TA貢獻(xiàn)1796條經(jīng)驗(yàn) 獲得超4個(gè)贊

使用reduce和Object.assign合并數(shù)組中的項(xiàng)目:


const data = [{

  "Name" : "Arrow",

  "Year" : "2001"

}, {

  "Name" : "Arrow",

  "Type" : "Action-Drama"

}, {

  "Name" : "GOT",

  "Type" : "Action-Drama"

}];




function mergeByProp (prop, xs) {

  return xs.reduce((acc, x) => {

    if (!acc[x[prop]]) {

      acc[x[prop]] = x;

    } else {

      acc[x[prop]] = Object.assign(acc[x[prop]], x);

    }

    return acc;

  }, {});

}


function objToArr (obj) {

  return Object.keys(obj).map(key => obj[key]);

}


console.log(objToArr(mergeByProp('Name', data)));


查看完整回答
反對(duì) 回復(fù) 2021-04-22
  • 3 回答
  • 0 關(guān)注
  • 232 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)