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

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

如何將值添加到數(shù)組的重復(fù)鍵中

如何將值添加到數(shù)組的重復(fù)鍵中

一只萌萌小番薯 2021-04-07 16:15:53
我將數(shù)組推入for循環(huán)中。如何在相同的鍵中增加價(jià)值        {            "0156": {                "test": "hi"            }        },        {            "0156": {                "test": "hi2"            }        },我想做這樣的事情            {             "0156": {                "test": "hi"                "test": "hi2"            }這是我嘗試的for(let i in test) {  let getTest = test[i];  const usr = getTest.id  var obj = {};  obj[usr] = {'test' : getTest.data};  getData.push(obj);}我嘗試使用if (typeof getData[0][usr] !== "undefined" ) { }但是還是沒(méi)用。
查看完整描述

1 回答

?
嗶嗶one

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

您可以將值分組到一個(gè)數(shù)組中


const arr = [{

    "0156": {

      "test": "hi",

      "test2": "abc"

    }

  },

  {

    "0156": {

      "test": "hi2",

      "test2": "abc1"

    },

    "0157": {

      "test": "y1"

    }

  },


  {

    "0156": {

      "test": "hi3"

    },

    "0158": {

      "test": "ti2"

    }

  },


  {

    "0156": {

      "test": "hi4"

    },

    "0157": {

      "test": "y"

    }

  },


  {

    "0158": {

      "test": "ti"

    }

  }

]


const res = arr.reduce(function(acc, curr) {


  for (let p in curr) {

    acc[p] = acc[p] || curr[p]


    for (let p1 in curr[p])

      acc[p][p1] = acc[p][p1] != curr[p][p1] ? [].concat(acc[p][p1], curr[p][p1]) : curr[p][p1]

  }




  return acc


}, {})

console.log(res)


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

添加回答

舉報(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)