admin管理员组文章数量:1022415
I have an object with groups (as an example). Each group object contains one header
id and multiple trigger
ids.
I want to get an array of all the triggers of all groups without duplicates.
An example would be this:
const groups = {
group1: { header: 9, trigger: [10,11] },
group2: { header: 15, trigger: [11, 17] }
}
Currently, I am doing it like so:
const triggers = Array.from(groups, x => x.trigger);
This gives me the following result: [[10,11],[11,17]]
My plan is to get something like this: [10,11,17]
. They don't have to be sorted but the duplicates (in this case 11
) has to be removed. Is there any fast way of doing it? Otherwise I would loop through this array now and then concat to a new array but I think there is a faster and better solution.
I have an object with groups (as an example). Each group object contains one header
id and multiple trigger
ids.
I want to get an array of all the triggers of all groups without duplicates.
An example would be this:
const groups = {
group1: { header: 9, trigger: [10,11] },
group2: { header: 15, trigger: [11, 17] }
}
Currently, I am doing it like so:
const triggers = Array.from(groups, x => x.trigger);
This gives me the following result: [[10,11],[11,17]]
My plan is to get something like this: [10,11,17]
. They don't have to be sorted but the duplicates (in this case 11
) has to be removed. Is there any fast way of doing it? Otherwise I would loop through this array now and then concat to a new array but I think there is a faster and better solution.
3 Answers
Reset to default 8Here is a one-liner using Set and .flatMap()
method
const groups = {
group1: { header: 9, trigger: [10,11] },
group2: { header: 15, trigger: [11, 17] }
}
const triggers = [...new Set(Object.values(groups).flatMap(x=>x.trigger))]
console.log(triggers)
Here's a pretty quick way using a Set
const groups = {
group1: { header: 9, trigger: [10,11] },
group2: { header: 15, trigger: [11, 17] }
}
const addGroupTrigger = (triggersSet, [, group]) => {
for (const trigger of group.trigger) {
triggersSet.add(trigger)
}
return triggersSet
}
console.log(
Array.from(Object.entries(groups).reduce(addGroupTrigger, new Set()))
)
Using the function Array.from
as in your code, I can't make it work like you said.
Btw, I have this snippet, take a look to see if it helps:
const groups = {
group1: { header: 9, trigger: [10,11] },
group2: { header: 15, trigger: [11, 17] }
}
var arr = []
Object.keys(groups).forEach(function (key) {
arr = arr.concat(groups[key].trigger);
});
console.log('Array with DUP: ', arr);
var set = new Set(arr);
arr = Array.from(set);
console.log('Array without DUP: ', arr);
I have an object with groups (as an example). Each group object contains one header
id and multiple trigger
ids.
I want to get an array of all the triggers of all groups without duplicates.
An example would be this:
const groups = {
group1: { header: 9, trigger: [10,11] },
group2: { header: 15, trigger: [11, 17] }
}
Currently, I am doing it like so:
const triggers = Array.from(groups, x => x.trigger);
This gives me the following result: [[10,11],[11,17]]
My plan is to get something like this: [10,11,17]
. They don't have to be sorted but the duplicates (in this case 11
) has to be removed. Is there any fast way of doing it? Otherwise I would loop through this array now and then concat to a new array but I think there is a faster and better solution.
I have an object with groups (as an example). Each group object contains one header
id and multiple trigger
ids.
I want to get an array of all the triggers of all groups without duplicates.
An example would be this:
const groups = {
group1: { header: 9, trigger: [10,11] },
group2: { header: 15, trigger: [11, 17] }
}
Currently, I am doing it like so:
const triggers = Array.from(groups, x => x.trigger);
This gives me the following result: [[10,11],[11,17]]
My plan is to get something like this: [10,11,17]
. They don't have to be sorted but the duplicates (in this case 11
) has to be removed. Is there any fast way of doing it? Otherwise I would loop through this array now and then concat to a new array but I think there is a faster and better solution.
3 Answers
Reset to default 8Here is a one-liner using Set and .flatMap()
method
const groups = {
group1: { header: 9, trigger: [10,11] },
group2: { header: 15, trigger: [11, 17] }
}
const triggers = [...new Set(Object.values(groups).flatMap(x=>x.trigger))]
console.log(triggers)
Here's a pretty quick way using a Set
const groups = {
group1: { header: 9, trigger: [10,11] },
group2: { header: 15, trigger: [11, 17] }
}
const addGroupTrigger = (triggersSet, [, group]) => {
for (const trigger of group.trigger) {
triggersSet.add(trigger)
}
return triggersSet
}
console.log(
Array.from(Object.entries(groups).reduce(addGroupTrigger, new Set()))
)
Using the function Array.from
as in your code, I can't make it work like you said.
Btw, I have this snippet, take a look to see if it helps:
const groups = {
group1: { header: 9, trigger: [10,11] },
group2: { header: 15, trigger: [11, 17] }
}
var arr = []
Object.keys(groups).forEach(function (key) {
arr = arr.concat(groups[key].trigger);
});
console.log('Array with DUP: ', arr);
var set = new Set(arr);
arr = Array.from(set);
console.log('Array without DUP: ', arr);
本文标签: javascriptHow to get an array without duplicates from object elementsStack Overflow
版权声明:本文标题:javascript - How to get an array without duplicates from object elements - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745572381a2156812.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论