javascript - 如何在javascript中通过id从对象数组中获取唯一数据
问题描述
大家好,我需要帮助我正在尝试通过 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);
推荐阅读
- vba - VBA-打开和过滤文件夹
- julia - 在 Julia 中用微分方程的解定义函数
- python - 使用 Kibana API 导出的仪表板无法在 Kibana UI 中手动导入
- c# - .NET Core/5 使用自定义 NewtonSoft 转换器反序列化,但模型仍绑定为 null
- wordpress - 在 Wordpress 中添加页面加载功能
- html - 使用 CSS 动画元素的大小
- javascript - 池关闭mysql2不和谐机器人
- flutter - CircleAvatar 异常:flutter 中的图像数据句柄无效
- python - 从 OpenCV/cv2 到 nginx 服务器的 RTMP 流,但客户端只得到一个绿色窗口
- dynamics-crm - Dynamics CRM 365 的 DocuSign 集成 - GetSignatures 失败