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

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

基于另外兩個(gè)對(duì)象數(shù)組創(chuàng)建對(duì)象數(shù)組

基于另外兩個(gè)對(duì)象數(shù)組創(chuàng)建對(duì)象數(shù)組

慕標(biāo)琳琳 2023-06-15 15:45:20
我有兩個(gè)對(duì)象數(shù)組,我想基于它們創(chuàng)建第三個(gè)對(duì)象并添加一些新的鍵/值?;A(chǔ)數(shù)據(jù)const oldArray = [{id: 1, name: 'Test', age: 25, skill: 120},?{id: 2, name: 'Test25', age: 10, skill: 120}];修改后的數(shù)據(jù)const newArray = [{id: 1, name: 'Test2', age: 50, skill: 200},{id: 2, name: 'Test25', age: 25, skill: 120}];我想收到類(lèi)似的東西:const expectedResultArray = [? {id: 1, oldName: 'Test', newName: 'Test2', oldAge: 25, newAge: 50, ageDifference: 25, oldSkill: 120, newSkill: 200, skillDifference: 80 },? {id: 2, oldName: 'Test25', newName:'Test25', oldAge: 10, newAge: 25, ageDifference: 15, oldSkill:120, newSkill: 120, skillDefference: 0}]如您所見(jiàn),預(yù)期數(shù)組是基于兩個(gè)數(shù)組的舊計(jì)算和全新計(jì)算的組合。我怎樣才能做到這一點(diǎn)?我試過(guò)reduce()但是我被困在嘗試中:var createNewArray = (...objects) =>?? Object.values(objects.reduce((a, e) => {? ? a[e.id] = a[e.id] || {id: e.id};? // struggling to proceed here?? ? return a;? }, {}));console.log(createNewArr(...oldArray, ...newArray));
查看完整描述

2 回答

?
陪伴而非守候

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

如果對(duì)象的順序相同,那么您可以試試這個(gè)。否則我們需要根據(jù)id.


const oldArray = [

{id: 1, name: 'Test', age: 25, skill: 120}, 

{id: 2, name: 'Test25', age: 10, skill: 120}];


const newArray = [

{id: 1, name: 'Test2', age: 50, skill: 200},

{id: 2, name: 'Test25', age: 25, skill: 120}];


const Result = newArray.map((item, i) => {

  let oldConverted = renameObject(oldArray[i], 'old');

  let newConverted = renameObject(item, 'new');

  newConverted.ageDifference = newConverted.newAge - oldConverted.oldAge;

  newConverted.skillDefference = newConverted.newSkill - oldConverted.oldSkill;

  return Object.assign({}, newConverted, oldConverted);

});


function renameObject(obj, type) {

  let newObj = {};

  newObj.id = obj.id;

  Object.keys(obj).forEach((key) => {

    if (key != 'id') {

      newObj[type + capitalize(key)] = obj[key];

    }

  });

  return newObj;

}


function capitalize(string) {

  return string.charAt(0).toUpperCase() + string.slice(1);

}

console.log(Result);


查看完整回答
反對(duì) 回復(fù) 2023-06-15
?
倚天杖

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

好吧,解決方案比我預(yù)期的要容易......(Facepalm)


const oldArray = [{id: 1, name: 'Test', age: 25, skill: 120},  {id: 2, name: 'Test25', age: 10, skill: 120}];


const newArray = [{id: 1, name: 'Test2', age: 50, skill: 200}, {id: 2, name: 'Test25', age: 25, skill: 120}];



const generateNewArr = () => {

 return oldArray.map((row,index) => {

   return {

     id: row.id,

     oldName: row.name,

     newName: newArray[index].name,

     oldAge: row.age,

     newAge: newArray[index].age,

     ageDifference: newArray[index].age - row.age,

     oldSkill: row.skill,

     newSkill: newArray[index].skill,

     skillDifference: newArray[index].skill - row.skill

   }

 })

}


  

console.log(generateNewArr())


查看完整回答
反對(duì) 回復(fù) 2023-06-15
  • 2 回答
  • 0 關(guān)注
  • 186 瀏覽
慕課專(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)