admin管理员组文章数量:1416631
I'm trying to figure out how to check for duplicate values of keys between objects in the same array, then only keep the most recently added object having that value.
For example, I have an array of objects I'm trying to filter duplicates of m_id
out of, like so:
arr = [
{id: "id-1", m_id: "1", color: "blue"},
{id: "id-2", m_id: "1", color: "green"},
{id: "id-3", m_id: "2", color: "red"},
{id: "id-4", m_id: "2", color: "yellow"},
{id: "id-5", m_id: "5", color: "purple"}
]
The desired result from the above example array of objects would be:
arr = [
{id: "id-2", m_id: "1", color: "green"},
{id: "id-4", m_id: "2", color: "yellow"},
{id: "id-5", m_id: "5", color: "purple"}
]
As you can see, I have a simple id
string value that gets incremented for the collection by design. What I'm trying to do is find if a duplicate m_id
value exists between any two objects in the array, then remove the object(s) having the lower of the two ids. By lower, in this case, I mean non-numeric boolean parison of the string ids (e.g., Boolean("id-1" < "id-2")
returns true
).
I've tried using Array.prototype.filter()
in bination with Array.prototype.some()
in numerous floundering permutations, but I keep running into trouble when I'm looking to pare key values of object elements in the same array inside my filter
when the overall collection is not in scope. I'm at a loss so far in getting the results I need.
I'm trying to figure out how to check for duplicate values of keys between objects in the same array, then only keep the most recently added object having that value.
For example, I have an array of objects I'm trying to filter duplicates of m_id
out of, like so:
arr = [
{id: "id-1", m_id: "1", color: "blue"},
{id: "id-2", m_id: "1", color: "green"},
{id: "id-3", m_id: "2", color: "red"},
{id: "id-4", m_id: "2", color: "yellow"},
{id: "id-5", m_id: "5", color: "purple"}
]
The desired result from the above example array of objects would be:
arr = [
{id: "id-2", m_id: "1", color: "green"},
{id: "id-4", m_id: "2", color: "yellow"},
{id: "id-5", m_id: "5", color: "purple"}
]
As you can see, I have a simple id
string value that gets incremented for the collection by design. What I'm trying to do is find if a duplicate m_id
value exists between any two objects in the array, then remove the object(s) having the lower of the two ids. By lower, in this case, I mean non-numeric boolean parison of the string ids (e.g., Boolean("id-1" < "id-2")
returns true
).
I've tried using Array.prototype.filter()
in bination with Array.prototype.some()
in numerous floundering permutations, but I keep running into trouble when I'm looking to pare key values of object elements in the same array inside my filter
when the overall collection is not in scope. I'm at a loss so far in getting the results I need.
- what do you mean with lower? – Nina Scholz Commented Apr 6, 2021 at 17:15
-
this is a non-numeric parison. e.g.,
Boolean("id-1" < "id-2")
evaluates totrue
– Jetals Commented Apr 6, 2021 at 17:20 -
is the array sorted by
m_id
? – Nina Scholz Commented Apr 6, 2021 at 17:21 - Nope, no sorting necessary :-) – Jetals Commented Apr 6, 2021 at 17:24
2 Answers
Reset to default 3You can turn it into a Map indexed by m_id
, then take the map's values:
const map = new Map(
arr.map(obj => [obj.m_id, obj])
);
const deduplicatedArr = [...map.values()];
(you might be able to use an object here, but only if the respective order of non-duplicate IDs doesn't need to be preserved - since the IDs are numeric, they'll be iterated over in ascending numeric order if they were properties of an object)
By having a sorted array, you could check the predecessor with the actual object and check if m_is
is not equal.
const
array = [{ id: "id-1", m_id: "1", color: "blue" }, { id: "id-2", m_id: "1", color: "green" }, { id: "id-3", m_id: "2", color: "red" }, { id: "id-4", m_id: "2", color: "yellow" }, { id: "id-5", m_id: "5", color: "purple" }],
result = array.filter((b, i, { [i - 1]: a }) => a?.m_id !== b.m_id);
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
With an unsorted array, you could check id
as well.
const
array = [{ id: "id-1", m_id: "1", color: "blue" }, { id: "id-2", m_id: "1", color: "green" }, { id: "id-3", m_id: "2", color: "red" }, { id: "id-4", m_id: "2", color: "yellow" }, { id: "id-5", m_id: "5", color: "purple" }],
result = Object.values(array.reduce((r, o) => {
if (!r[o.m_id] || +r[o.m_id].id.split('-')[1] < +o.id.split('-')[1]) r[o.m_id] = o;
return r;
}, {}));
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
本文标签:
版权声明:本文标题:JavaScript: check if duplicate key values exist in array of objects and remove all but most recently added object having that ke 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745251845a2649869.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论