簡體   English   中英

如何通過javascript中的id從對象數組中獲取唯一數據

[英]How to get unique data from array of object by id in javascript

您好,我需要幫助我正在嘗試通過 JavaScript 中的 id 從對象數組中獲取唯一數據我無法在下面獲取它是我的數組

[
  {id: 408, customer_id: 2, bill_no: 381, bill_period: 'weekly', from_date: '2021-10-10'}
  {id: 409, customer_id: 3, bill_no: 382, bill_period: 'weekly', from_date: '2021-10-10'}
  {id: 410, customer_id: 4, bill_no: 383, bill_period: 'weekly', from_date: '2021-10-10'}
  {id: 411, customer_id: 6, bill_no: 384, bill_period: 'weekly', from_date: '2021-10-10'}
  {id: 412, customer_id: 7, bill_no: 385, bill_period: 'weekly', from_date: '2021-10-10'}
  {id: 413, customer_id: 8, bill_no: 386, bill_period: 'weekly', from_date: '2021-10-10'}
  {id: 414, customer_id: 9, bill_no: 387, bill_period: 'weekly', from_date: '2021-10-10'}
  {id: 387, customer_id: 2, bill_no: 360, bill_period: 'weekly', from_date: '2021-10-03'}
  {id: 388, customer_id: 3, bill_no: 361, bill_period: 'weekly', from_date: '2021-10-03'}
  {id: 389, customer_id: 4, bill_no: 362, bill_period: 'weekly', from_date: '2021-10-03'}
  {id: 390, customer_id: 6, bill_no: 363, bill_period: 'weekly', from_date: '2021-10-03'}
  {id: 391, customer_id: 7, bill_no: 364, bill_period: 'weekly', from_date: '2021-10-03'}
  {id: 392, customer_id: 8, bill_no: 365, bill_period: 'weekly', from_date: '2021-10-03'}
  {id: 393, customer_id: 9, bill_no: 366, bill_period: 'weekly', from_date: '2021-10-03'}
  {id: 380, customer_id: 2, bill_no: 353, bill_period: 'weekly', from_date: '2021-09-26'}
  {id: 381, customer_id: 3, bill_no: 354, bill_period: 'weekly', from_date: '2021-09-26'}
  {id: 382, customer_id: 4, bill_no: 355, bill_period: 'weekly', from_date: '2021-09-26'}
  {id: 383, customer_id: 6, bill_no: 356, bill_period: 'weekly', from_date: '2021-09-26'}
  {id: 384, customer_id: 7, bill_no: 357, bill_period: 'weekly', from_date: '2021-09-26'}
  {id: 385, customer_id: 8, bill_no: 358, bill_period: 'weekly', from_date: '2021-09-26'}
  {id: 386, customer_id: 9, bill_no: 359, bill_period: 'weekly', from_date: '2021-09-26'}
]

我想要來自 customer_id 的上述數組的唯一數據和他們的最新 id 我想要像下面的代碼一樣的結果

[
  {id: 408, customer_id: 2, bill_no: 381, bill_period: 'weekly', from_date: '2021-10-10'}
  {id: 409, customer_id: 3, bill_no: 382, bill_period: 'weekly', from_date: '2021-10-10'}
  {id: 410, customer_id: 4, bill_no: 383, bill_period: 'weekly', from_date: '2021-10-10'}
  {id: 411, customer_id: 6, bill_no: 384, bill_period: 'weekly', from_date: '2021-10-10'}
  {id: 412, customer_id: 7, bill_no: 385, bill_period: 'weekly', from_date: '2021-10-10'}
  {id: 413, customer_id: 8, bill_no: 386, bill_period: 'weekly', from_date: '2021-10-10'}
  {id: 414, customer_id: 9, bill_no: 387, bill_period: 'weekly', from_date: '2021-10-10'}
]

沒有現成的功能,你需要自己動手; 但這應該很簡單。 減速機的絕佳用例!

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/Reduce

 const arr = [ {id: 408, customer_id: 2, bill_no: 381, bill_period: 'weekly', from_date: '2021-10-10'}, {id: 409, customer_id: 3, bill_no: 382, bill_period: 'weekly', from_date: '2021-10-10'}, {id: 410, customer_id: 4, bill_no: 383, bill_period: 'weekly', from_date: '2021-10-10'}, {id: 411, customer_id: 6, bill_no: 384, bill_period: 'weekly', from_date: '2021-10-10'}, {id: 412, customer_id: 7, bill_no: 385, bill_period: 'weekly', from_date: '2021-10-10'}, {id: 413, customer_id: 8, bill_no: 386, bill_period: 'weekly', from_date: '2021-10-10'}, {id: 414, customer_id: 9, bill_no: 387, bill_period: 'weekly', from_date: '2021-10-10'}, {id: 387, customer_id: 2, bill_no: 360, bill_period: 'weekly', from_date: '2021-10-03'}, {id: 388, customer_id: 3, bill_no: 361, bill_period: 'weekly', from_date: '2021-10-03'}, {id: 389, customer_id: 4, bill_no: 362, bill_period: 'weekly', from_date: '2021-10-03'}, {id: 390, customer_id: 6, bill_no: 363, bill_period: 'weekly', from_date: '2021-10-03'}, {id: 391, customer_id: 7, bill_no: 364, bill_period: 'weekly', from_date: '2021-10-03'}, {id: 392, customer_id: 8, bill_no: 365, bill_period: 'weekly', from_date: '2021-10-03'}, {id: 393, customer_id: 9, bill_no: 366, bill_period: 'weekly', from_date: '2021-10-03'}, {id: 380, customer_id: 2, bill_no: 353, bill_period: 'weekly', from_date: '2021-09-26'}, {id: 381, customer_id: 3, bill_no: 354, bill_period: 'weekly', from_date: '2021-09-26'}, {id: 382, customer_id: 4, bill_no: 355, bill_period: 'weekly', from_date: '2021-09-26'}, {id: 383, customer_id: 6, bill_no: 356, bill_period: 'weekly', from_date: '2021-09-26'}, {id: 384, customer_id: 7, bill_no: 357, bill_period: 'weekly', from_date: '2021-09-26'}, {id: 385, customer_id: 8, bill_no: 358, bill_period: 'weekly', from_date: '2021-09-26'}, {id: 386, customer_id: 9, bill_no: 359, bill_period: 'weekly', from_date: '2021-09-26'} ]; // reduce array to only relevant values to a single variable const filtered = arr.reduce((accumulator, current) => { // If customer data is already added to accumulator, don't add if (accumulator.find(x => x.customer_id === current.customer_id)) { return accumulator; } accumulator.push(current); return accumulator; }, []); console.log(filtered);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM