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

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

通過(guò)在 javascript 中保留數(shù)據(jù)(將值推送到數(shù)組中)來(lái)減少相同鍵值的對(duì)象數(shù)組?

通過(guò)在 javascript 中保留數(shù)據(jù)(將值推送到數(shù)組中)來(lái)減少相同鍵值的對(duì)象數(shù)組?

小唯快跑啊 2023-03-03 15:17:13
我有以下格式的數(shù)據(jù),基本上是一個(gè)對(duì)象數(shù)組,我嘗試了幾種方法,我怎樣才能更有效地做到這一點(diǎn)?const overdue =  [        {            "user.employeeId": "10001440",            "objectives": [                "Understand the financial indexes."            ]        },        {            "user.employeeId": "10000303",            "objectives": [                "Document preparation & writing skills"            ]        },        {            "user.employeeId": "10002168",            "objectives": [                "Chia ratio setting for Fuze Tea products L11"            ]        },        {            "user.employeeId": "10002168",            "objectives": [                "Brix parameter differences between Processing and Production of Fuze Tea Lemon-Lemongrass standardization"            ]        },        {            "user.employeeId": "10002168",            "objectives": [                "Paramix Line 9 setting parameter standardization"            ]        },    ]我如何使用 lodash 通過(guò) JavaScript 將其轉(zhuǎn)換為以下內(nèi)容?[        {            "user.employeeId": "10001440",            "objectives": [                "Understand the financial indexs."            ]        },        {            "user.employeeId": "10000303",            "objectives": [                "Document preparation & writing skills"            ]        },        {            "user.employeeId": "10002168",            "objectives": [                "Brix parameter differences between Processing and Production of Fuze Tea Lemon-Lemongrass standardization",                "Paramix Line 9 setting parameter standardization",                "Chia ratio setting for Fuze Tea products L11"            ]        }    ]我試過(guò) Array.map!和正常的邏輯,我們?nèi)绾问褂?lodash reduce 或 arr.reduce 更有效地做到這一點(diǎn)?
查看完整描述

4 回答

?
千萬(wàn)里不及你

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

您可以使用Array.reduce來(lái)獲得所需的結(jié)果,使用數(shù)組作為累加器。

我們枚舉過(guò)期數(shù)組中的每個(gè)對(duì)象,如果它不存在于輸出數(shù)組中,我們將其添加,否則我們將對(duì)象目標(biāo)添加到輸出中的相關(guān)元素。

const overdue =  [ { "user.employeeId": "10001440", "objectives": [ "Understand the financial indexes." ] }, { "user.employeeId": "10000303", "objectives": [ "Document preparation & writing skills" ] }, { "user.employeeId": "10002168", "objectives": [ "Chia ratio setting for Fuze Tea products L11" ] }, { "user.employeeId": "10002168", "objectives": [ "Brix parameter differences between Processing and Production of Fuze Tea Lemon-Lemongrass standardization" ] }, { "user.employeeId": "10002168", "objectives": [ "Paramix Line 9 setting parameter standardization" ] }, ]; 


let result = overdue.reduce((res, row) => {

    let el = res.find(el => el["user.employeeId"] === row["user.employeeId"]);

    // If we find the object in the output array simply update the objectives

    if (el) {

       el.objectives = [...el.objectives, ...row.objectives];

    } else {

    // If we _don't_ find the object, add to the output array.

      res.push({ ...row});

    }

    return res;

}, [])


console.log("Result:",result);


查看完整回答
反對(duì) 回復(fù) 2023-03-03
?
慕蓋茨4494581

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

使用array.reduce對(duì)象并將其傳遞給累加器。在回調(diào)函數(shù)中檢查累加器是否具有與 的值相同的鍵user.employeeId。如果不是,則創(chuàng)建鍵并將迭代中的當(dāng)前對(duì)象添加為它的值。如果已經(jīng)有一個(gè)鍵,那么只需更新目標(biāo)數(shù)組。在檢索值時(shí)Object.valuewhich 將給出一個(gè)數(shù)組


const overdue = [{

    "user.employeeId": "10001440",

    "objectives": [

      "Understand the financial indexes."

    ]

  },

  {

    "user.employeeId": "10000303",

    "objectives": [

      "Document preparation & writing skills"

    ]

  },

  {

    "user.employeeId": "10002168",

    "objectives": [

      "Chia ratio setting for Fuze Tea products L11"

    ]

  },


  {

    "user.employeeId": "10002168",

    "objectives": [

      "Brix parameter differences between Processing and Production of Fuze Tea Lemon-Lemongrass standardization"

    ]

  },

  {

    "user.employeeId": "10002168",

    "objectives": [

      "Paramix Line 9 setting parameter standardization"

    ]

  },

];




let newData = overdue.reduce((acc, curr) => {

  if (!acc[curr['user.employeeId']]) {

    acc[curr['user.employeeId']] = curr;

  } else {

    curr.objectives.forEach((item) => {

      acc[curr['user.employeeId']]['objectives'].push(item)

    })

  }

  return acc;

}, {});



console.log(Object.values(newData))


查看完整回答
反對(duì) 回復(fù) 2023-03-03
?
largeQ

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

我不確定如何在 lodash 中執(zhí)行此操作,但這是我僅使用 vanilla Javascript 執(zhí)行此操作的方法:


const overdue = [{

    "user.employeeId": "10001440",

    "objectives": [

      "Understand the financial indexes."

    ]

  },

  {

    "user.employeeId": "10000303",

    "objectives": [

      "Document preparation & writing skills"

    ]

  },

  {

    "user.employeeId": "10002168",

    "objectives": [

      "Chia ratio setting for Fuze Tea products L11"

    ]

  },


  {

    "user.employeeId": "10002168",

    "objectives": [

      "Brix parameter differences between Processing and Production of Fuze Tea Lemon-Lemongrass standardization"

    ]

  },

  {

    "user.employeeId": "10002168",

    "objectives": [

      "Paramix Line 9 setting parameter standardization"

    ]

  },

];


const combined = {};


overdue.forEach(o => {

  const id = o["user.employeeId"];

  const obj = o["objectives"][0];

  

  if(!combined[id]) { combined[id] = [obj]; }

  else { combined[id].push(obj); }

  

});


const result = [];


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

  result.push({"user.employeeId" : key, "objectives" : combined[key]});

});


console.log(result);


查看完整回答
反對(duì) 回復(fù) 2023-03-03
?
慕姐8265434

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

如果你可以使用 lodash,那么你就可以超越 reduce 并使用更具體的東西來(lái)滿足你的需求。在這種情況下,您可以考慮使用該_.groupBy方法,該方法將創(chuàng)建一個(gè)由user.employeeId值作為鍵的對(duì)象,其中包含值作為每個(gè)對(duì)象的數(shù)組。然后,您可以將對(duì)象的(即:具有相同 employeeId 的對(duì)象數(shù)組)映射到每個(gè)數(shù)組合并的合并對(duì)象objectives。最后,您可以獲取分組對(duì)象的值以獲得結(jié)果:

const overdue = [{ "user.employeeId": "10001440", "objectives": [ "Understand the financial indexes." ] }, { "user.employeeId": "10000303", "objectives": [ "Document preparation & writing skills" ] }, { "user.employeeId": "10002168", "objectives": [ "Chia ratio setting for Fuze Tea products L11" ] }, { "user.employeeId": "10002168", "objectives": [ "Brix parameter differences between Processing and Production of Fuze Tea Lemon-Lemongrass standardization" ] }, { "user.employeeId": "10002168", "objectives": [ "Paramix Line 9 setting parameter standardization" ] }, ];


const group = _.flow(

  arr => _.groupBy(arr, "user.employeeId"),

  g => _.mapValues(g, arr => _.mergeWith(...arr, (objV, srcV) => {

    if (_.isArray(objV)) return objV.concat(srcV);

  })),

  _.values

);


console.log(group(overdue));

<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.15/lodash.min.js"></script>

或者您可以將 vanilla JS 與.reduce()a 一起使用Map,這使用了 JS 的更多現(xiàn)代功能(節(jié)點(diǎn) 14.0.0 均支持),例如可選鏈接 ?.空合并運(yùn)算符 ??

const overdue = [{ "user.employeeId": "10001440", "objectives": [ "Understand the financial indexes." ] }, { "user.employeeId": "10000303", "objectives": [ "Document preparation & writing skills" ] }, { "user.employeeId": "10002168", "objectives": [ "Chia ratio setting for Fuze Tea products L11" ] }, { "user.employeeId": "10002168", "objectives": [ "Brix parameter differences between Processing and Production of Fuze Tea Lemon-Lemongrass standardization" ] }, { "user.employeeId": "10002168", "objectives": [ "Paramix Line 9 setting parameter standardization" ] }, ];


const group = (arr, key) => Array.from(arr.reduce((m, obj) =>

  m.set(

    obj[key], 

    {...obj, objectives: [...(m.get(obj[key])?.objectives ?? []), ...obj.objectives]}

  ), new Map).values()

);


console.log(group(overdue, "user.employeeId"));



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

添加回答

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