在一系列对象中获取第一个重复项JavaScript
我有以下我从API中获得的数据结构:
[
{
id: '10000844',
text_id: '10000844-01',
},
{
id: '10000844',
text_id: '10000844-02',
},
{
id: '12000844',
text_id: '12000844-03',
},
{
id: '12000844',
text_id: '12000844-07',
},
{
id: '12000814',
text_id: '12000844-07',
},
{
id: '12002812',
text_id: '12000844-07',
},
{
id: '12000814',
text_id: '12000844-08',
},
]
清洁代码的完美结果将是此结果,基本上仅返回第一个发现的ID:
[
{
id: '10000844',
text_id: '10000844-01',
},
{
id: '12000844',
text_id: '12000844-03',
},
{
id: '12000814',
text_id: '12000844-07',
},
{
id: '12002812',
text_id: '12000844-07',
},
]
但是当前它仅返回最后发现的副本,以唯一的数组,而当前代码:
let uniqueArray = [...new Map(data.map(item =>
[item.id, item])).values()];
I have the following data structure that i receive from the api:
[
{
id: '10000844',
text_id: '10000844-01',
},
{
id: '10000844',
text_id: '10000844-02',
},
{
id: '12000844',
text_id: '12000844-03',
},
{
id: '12000844',
text_id: '12000844-07',
},
{
id: '12000814',
text_id: '12000844-07',
},
{
id: '12002812',
text_id: '12000844-07',
},
{
id: '12000814',
text_id: '12000844-08',
},
]
The perfect outcome of cleaning the code would be this result, basically returning only the first found id:
[
{
id: '10000844',
text_id: '10000844-01',
},
{
id: '12000844',
text_id: '12000844-03',
},
{
id: '12000814',
text_id: '12000844-07',
},
{
id: '12002812',
text_id: '12000844-07',
},
]
But currently it only returns the last found duplicate in a unique array, with the current code:
let uniqueArray = [...new Map(data.map(item =>
[item.id, item])).values()];
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
您可以使用
object.values()
和redion()
与:更新带有
?? =
文档逻辑无效分配
(x ?? = y)
操作员仅分配如果x为nullish(nullish或null或Undefined)
。You can use
Object.values()
andreduce()
same as :Update with
??=
documentThe logical nullish assignment
(x ??= y)
operator only assignsif x is nullish (null or undefined)
.您可以这样做:
You can do it like this:
概念
循环通过数据中的所有对象,并检查结果数组中是否找到ID。如果找不到,请将其推入结果数组;否则,请跳过。
代码
Concept
Loop through all the objects in the data and check if the id is found in result array. If not found then push it into the result array; otherwise, skip it.
Code
这是实现您渴望输出的另一种方法。
我们使用
REDY
用于更改数据并使用查找
函数以获取当前ID
是否已在prev> prev
中是否存在不是
然后,我们使用filter
函数来滤除相同的id
对象,然后首先按下。Here is another way to achieve you desire output.
we use
reduce
for changing in data and usefind
function to get currentid
is already inprev
or not ifnot
then we usefilter
function to filter out sameid
object and push only first.您可以 阵列中仅存储每个ID首次出现的对象,然后提取
values
来自此对象。You can
reduce
the array into an object that stores only the first occurrence of every id and then extract thevalues
from this object.一些答案已经正确。
此答案仅在您使用lodash 时才
可以使用 uniqby 。
这将返回一个阵列的无重复版本,其中第一次出现将保留一个阵列:
因此,在您的情况下,
Some of the answers are already correct.
THIS ANSWER IS ONLY IF YOU ARE USING LODASH
You can use uniqBy.
This will return a duplicate-free version of an array where the first occurrence is the one that will be kept:
So in your case, something like
有时
附加文档
/p>
object> object> object.values.values
code>Sometimes a simple loop is the way forward.
Additional documentation
Destructuring assignment
Logical nullish assignment
Rest parameters
Object.values