admin管理员组文章数量:1335371
I'm trying to fix this, but I'm a little bit stuck and need some help/advise. I'm trying to get to know es6 better and better, but I'm clueless on what's the best way to fix my problem.
I have a large json I'm fetching which looks somewhat like this:
[
{
"pany": "Google",
"location": "USA",
"email": null
},
{
"pany": "Microsoft",
"location": "USA",
"email": "[email protected]"
},
{
"pany": "Google",
"location": "NLD",
"email": "[email protected]"
}
]
I display these in a table and want to add checkbox filters but I also want to add a count next to it, like so:
[x] Google (2)
[ ] Microsoft (1)
// other function call
[ ] [email protected] (2)
I have this function I call every every key (pany, location, email):
function filterArr(data, key) {
data.forEach(element => {
let countedData = data.filter((el) => {
return el[key] == element[key]
}).length;
// console.log(element[key] + ": " + countedData);
});
data = data.filter((item, index, self) => self.findIndex( t => t[key] === item[key] && item[key] != null) === index )
// console.log(data)
return data;
}
filterArr(data, "pany");
The output I'm trying to achieve with the above function is: Google: 2 Microsft: 1
The foreach is correctly counting the key values, but obviously logging the following: Google: 2 Microsoft: 1 Google: 2
And the filter console.log shows Google and Microsoft (just once, like I want :)
Now I need to bine these 2, but I'm not sure how to and what's the best way to do so. (see my fiddle: /)
Do you know what to do next?
I'm trying to fix this, but I'm a little bit stuck and need some help/advise. I'm trying to get to know es6 better and better, but I'm clueless on what's the best way to fix my problem.
I have a large json I'm fetching which looks somewhat like this:
[
{
"pany": "Google",
"location": "USA",
"email": null
},
{
"pany": "Microsoft",
"location": "USA",
"email": "[email protected]"
},
{
"pany": "Google",
"location": "NLD",
"email": "[email protected]"
}
]
I display these in a table and want to add checkbox filters but I also want to add a count next to it, like so:
[x] Google (2)
[ ] Microsoft (1)
// other function call
[ ] [email protected] (2)
I have this function I call every every key (pany, location, email):
function filterArr(data, key) {
data.forEach(element => {
let countedData = data.filter((el) => {
return el[key] == element[key]
}).length;
// console.log(element[key] + ": " + countedData);
});
data = data.filter((item, index, self) => self.findIndex( t => t[key] === item[key] && item[key] != null) === index )
// console.log(data)
return data;
}
filterArr(data, "pany");
The output I'm trying to achieve with the above function is: Google: 2 Microsft: 1
The foreach is correctly counting the key values, but obviously logging the following: Google: 2 Microsoft: 1 Google: 2
And the filter console.log shows Google and Microsoft (just once, like I want :)
Now I need to bine these 2, but I'm not sure how to and what's the best way to do so. (see my fiddle: https://jsfiddle/z359qo1d/)
Do you know what to do next?
Share Improve this question asked Dec 19, 2017 at 22:51 elDrimmelDrimm 1061 silver badge9 bronze badges2 Answers
Reset to default 7Array.prototype.reduce
is a perfect match for what you want
function filterArr(data, key){
return data.reduce( (result, current) => {
if(!result[current[key]]){
result[current[key]] = 1;
} else {
result[current[key]] += 1;
}
return result;
}, {})
}
The above will return an object like this
{
Google: 2,
Microsoft: 1
}
I'd do it a little bit differently:
let _in = [
{
"pany": "Google",
"location": "USA",
"email": null
},
{
"pany": "Microsoft",
"location": "USA",
"email": "[email protected]"
},
{
"pany": "Google",
"location": "NLD",
"email": "[email protected]"
}
]
function countEm(accum, each) {
if (! accum[each.pany] )
accum[each.pany] = 0
accum[each.pany] += 1
return accum
}
console.log(_in.reduce(countEm, {}))
本文标签: JavaScriptCount and remove duplicates from array of objects with es6Stack Overflow
版权声明:本文标题:JavaScript - Count and remove duplicates from array of objects with es6 - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742381490a2464183.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论