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

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

如何對(duì)數(shù)組對(duì)象進(jìn)行分組以獲得所需的數(shù)據(jù)格式?

如何對(duì)數(shù)組對(duì)象進(jìn)行分組以獲得所需的數(shù)據(jù)格式?

幕布斯7119047 2023-08-10 15:26:12
我嘗試使用此對(duì)象數(shù)組對(duì)對(duì)象數(shù)據(jù)進(jìn)行分組:var data = [     { IdUser: 8, Name: "John Smith", transferType: 'download', total: 6 },    { IdUser: 12, Name: "Jane Smith", transferType: 'download', total: 15 },    { IdUser: 11, Name: "Joe Smith", transferType: 'download', total: 20 },    { IdUser: 12, Name: "Jane Smith", transferType: 'upload', total: 7 },    { IdUser: 11, Name: "Joe Smith", transferType: 'upload', total: 16 },    { IdUser: 8, Name: "John Smith", transferType: 'upload', total: 12 } ];將所有對(duì)象的所需輸出格式獲取到新數(shù)組中:  [    {       IdUser: 8,       Name: 'John Smith',       download: [{ IdUser: 8, Name: "John Smith", transferType: 'download', total: 6}],       upload: [{ IdUser: 8, Name: "John Smith", transferType: 'upload', total: 12 }]    },    {        IdUser: 12,       Name: 'Jane Smith',       donwload: [{ IdUser: 12, Name: "Jane Smith", transferType: 'download', total: 15 }],       upload: [{ IdUser: 12, Name: "Jane Smith", transferType: 'upload', total: 7 }]    },    {       IdUser: 11,       Name: 'Joe Smith',       download: [{ IdUser: 11, Name: "Joe Smith", transferType: 'download', total: 20 }],       upload: [{ IdUser: 11, Name: "Joe Smith", transferType: 'upload', total: 16 }]    }  ];我嘗試使用reduce函數(shù),但它不是我得到的所需格式:data.reduce(function (a, b) {      a[b.Name] = a[b.Name] || [];         a[b.Name]["IdUser"] = b.IdUser;        a[b.Name][b.transferType] = a[b.Name][b.transferType] || [];           a[b.Name][b.transferType].push(b);      return a;    }, []);
查看完整描述

4 回答

?
qq_花開(kāi)花謝_0

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

我建議你分兩次做。


第一次使用分組功能按 IdUser 對(duì)數(shù)據(jù)進(jìn)行分組。


然后使用 for in 循環(huán):


var data = [

  {

    "IdUser": 8,

    "Name": "John Smith",

    "transferType": "download",

    "total": 6

  },

  {

    "IdUser": 12,

    "Name": "Jane Smith",

    "transferType": "download",

    "total": 15

  },

  {

    "IdUser": 11,

    "Name": "Joe Smith",

    "transferType": "downloaded",

    "total": 20

  },

  {

    "IdUser": 12,

    "Name": "Jane Smith",

    "transferType": "upload",

    "total": 7

  },

  {

    "IdUser": 11,

    "Name": "Joe Smith",

    "transferType": "upload",

    "total": 16

  },

  {

    "IdUser": 8,

    "Name": "John Smith",

    "transferType": "upload",

    "total": 12

  }

];


var groupBy = function(xs, key) {

  return xs.reduce(function(rv, x) {

    (rv[x[key]] = rv[x[key]] || []).push(x);

    return rv;

  }, {});

};

var tmp =  groupBy(data, 'IdUser');


var formattedData = [];

for(var id in tmp ){

    formattedData.push({

    "IdUser": id,

    "Name": tmp[id][0].Name,

    "download": tmp[id].filter(obj => obj.transferType == "download"),

    "upload":tmp[id].filter(obj => obj.transferType == "upload")

    })

}

console.log(formattedData)


查看完整回答
反對(duì) 回復(fù) 2023-08-10
?
長(zhǎng)風(fēng)秋雁

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

編輯以使用reduce。


var data = [{

    IdUser: 8,

    Name: "John Smith",

    transferType: 'download',

    total: 6

  },

  {

    IdUser: 12,

    Name: "Jane Smith",

    transferType: 'download',

    total: 15

  },

  {

    IdUser: 11,

    Name: "Joe Smith",

    transferType: 'download',

    total: 20

  },

  {

    IdUser: 12,

    Name: "Jane Smith",

    transferType: 'upload',

    total: 7

  },

  {

    IdUser: 11,

    Name: "Joe Smith",

    transferType: 'upload',

    total: 16

  },

  {

    IdUser: 8,

    Name: "John Smith",

    transferType: 'upload',

    total: 12

  }

];


let ret = data.reduce((ret, cur) => {

  let computed = ret.find(record => cur.IdUser === record.IdUser)

  if (computed) {

    computed[cur.transferType].push(cur)

  } else {

    let fresh = {

      IdUser: cur.IdUser,

      Name: cur.Name,

      download: [],

      upload: []

    }

    fresh[cur.transferType].push(cur)

    ret.push(fresh)

  }

  return ret

}, [])


console.log(ret)


查看完整回答
反對(duì) 回復(fù) 2023-08-10
?
至尊寶的傳說(shuō)

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

您可以使用該array.map方法。


const myObj = [

    {

       IdUser: 8,

       Name: 'John Smith',

       download: [{ IdUser: 8, Name: "John Smith", transferType: 'download', total: 6}],

       upload: [{ IdUser: 8, Name: "John Smith", transferType: 'upload', total: 12 }]

    },

    { 

       IdUser: 12,

       Name: 'Jane Smith',

       donwload: [{ IdUser: 12, Name: "Jane Smith", transferType: 'download', total: 15 }],

       upload: [{ IdUser: 12, Name: "Jane Smith", transferType: 'upload', total: 7 }]

    },

    {

       IdUser: 11,

       Name: 'Joe Smith',

       download: [{ IdUser: 11, Name: "Joe Smith", transferType: 'downloaded', total: 20 }],

       upload: [{ IdUser: 11, Name: "Joe Smith", transferType: 'upload', total: 16 }]

    }

  ];


const uploadData = myObject.map(val => val.upload);

const downloadData = myObject.map(val => val.download);

const newData = uploadData.concat(downloadData);


查看完整回答
反對(duì) 回復(fù) 2023-08-10
?
慕絲7291255

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

您可以使用一個(gè)簡(jiǎn)單的循環(huán),例如:

for (let i=0; i<data.length; i++) { ... }

在循環(huán)內(nèi)部,您可以訪問(wèn)每個(gè)對(duì)象和屬性,例如:

data[i].IdUser


查看完整回答
反對(duì) 回復(fù) 2023-08-10
  • 4 回答
  • 0 關(guān)注
  • 214 瀏覽
慕課專(zhuān)欄
更多

添加回答

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