admin 管理员组文章数量: 1086019
I have an array as follows:
const arr = [
{pany: 'a', date: '1'},
{pany: 'b', date: '1'},
{pany: 'c', date: '1'},
{pany: 'a', date: '2'},
{pany: 'a', date: '1'},
{pany: 'b', date: '2'},
]
I just want to know how to get the unique objects inside it. I tried using lodash with this mand:
uniqBy(arr, 'date');
But it only returns:
[
{pany: "a", date: "1"},
{pany: "a", date: "2"}
]
I want something like this one:
[
{pany: "a", date: "1"},
{pany: "a", date: "2"},
{pany: "b", date: "1"},
{pany: "b", date: "2"},
{pany: "c", date: "1"},
]
Is there a way in lodash or vanilla JS to have this done?
I have an array as follows:
const arr = [
{pany: 'a', date: '1'},
{pany: 'b', date: '1'},
{pany: 'c', date: '1'},
{pany: 'a', date: '2'},
{pany: 'a', date: '1'},
{pany: 'b', date: '2'},
]
I just want to know how to get the unique objects inside it. I tried using lodash with this mand:
uniqBy(arr, 'date');
But it only returns:
[
{pany: "a", date: "1"},
{pany: "a", date: "2"}
]
I want something like this one:
[
{pany: "a", date: "1"},
{pany: "a", date: "2"},
{pany: "b", date: "1"},
{pany: "b", date: "2"},
{pany: "c", date: "1"},
]
Is there a way in lodash or vanilla JS to have this done?
Share Improve this question edited Jan 29, 2020 at 15:55 Dimitris Papazacharias 6912 gold badges7 silver badges21 bronze badges asked Jan 27, 2020 at 16:17 Noel RosalesNoel Rosales 614 bronze badges 2- group by or .count may help you – Iria Commented Jan 27, 2020 at 16:19
- Your result is not unique by any normal definition, it looks like you've simply reordered the original array – Jamiec Commented Jan 27, 2020 at 16:20
4 Answers
Reset to default 7With help of reduce()
in pure js you I created a function which takes an array of objects and array of keys as inputs and return the array which is unique by all those keys.
Following are the steps of the algorithm:
- First we need to create an object using
reduce()
. - The
key
of that object will be the values of required keys which are provided each joined by-
(I mentionedkeyString
for each object in the ment of code). - The object which will have same
keyString
means same values for given array ofkeys
will automatically occur only once because object can't have duplicate keys - At last we use
Object.values()
to to create an array.
const arr = [
{pany: 'a', date: '1'}, //keyString = "a-1"
{pany: 'b', date: '1'}, //keyString = "b-1"
{pany: 'c', date: '1'}, //keyString = "c-1"
{pany: 'a', date: '2'}, //keyString = "a-2"
{pany: 'a', date: '1'}, //keyString = "a-1"
{pany: 'b', date: '2'}, //keyString = "b-2"
//The item at index 0 and index 4 have same keyString so only a single of them will remain in object.
]
const uniqueBy = (arr, keys) => {
const obj = arr.reduce((ac, a) => {
let keyString = keys.map(k => a[k]).join('-');
ac[keyString] = a;
return ac;
}, {});
return Object.values(obj);
}
console.log(uniqueBy(arr, ['pany', 'date']))
This lodash function bination should do it:
_.uniqWith(arr, _.isEqual);
If you want to consider only a bination of properties for uniqueness and leave other properties out of it, you use uniqBy() with a custom function of your own that sets the criterion. For example:
const arr = [
{pany: 'a', date: '1', clients: 3},
{pany: 'b', date: '1', clients: 2},
{pany: 'c', date: '1', clients: 2},
{pany: 'a', date: '1', clients: 1}
]
const uniqArr = _.uniqBy(arr, function(obj){
return obj.pany + obj.date;
});
// => [
{pany: 'a', date: '1', clients: 3},
{pany: 'b', date: '1', clients: 2},
{pany: 'c', date: '1', clients: 2}
]
In this example, the client property does not affect uniqueness, so the last object will be excluded because the pany and date properties are the same as the first object.
Group them using pany and date fields and use reduce
const arr = [{
pany: 'a',
date: '1'
},
{
pany: 'b',
date: '1'
},
{
pany: 'c',
date: '1'
},
{
pany: 'a',
date: '2'
},
{
pany: 'a',
date: '1'
},
{
pany: 'b',
date: '2'
},
];
const res = Object.values(arr.reduce((acc, curr) => {
const key = ['pany', 'date'].map(x => curr[x]).join('-');
if (!acc[key]) {
acc[key] = curr;
}
return acc;
}, {}));
console.log(res);
Here with using reduce
method in one line.
const arr = [
{ pany: "a", date: "1" },
{ pany: "b", date: "1" },
{ pany: "c", date: "1" },
{ pany: "a", date: "2" },
{ pany: "a", date: "1" },
{ pany: "b", date: "2" }
];
const updated = Object.values(
arr.reduce(
(acc, curr) => ({
...acc,
[`${curr.pany}-${curr.date}`]: { ...curr }
}),
{}
)
);
console.log(updated);
本文标签: javascriptFinding unique objects in arrayStack Overflow
版权声明:本文标题:javascript - Finding unique objects in array - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://roclinux.cn/p/1744022271a2520014.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论