what trying achieve filtering of objects
array array of objects unique actors.
so have:
var objects = [{ actor: { account: null, degraded: false, mbox: null, mbox_sha1sum: "843c56da78f9eb888274d2d4e12ab1d748ec46234", name: "name", openid: null }, capture: 'value' }, { actor: { account: null, degraded: false, mbox: null, mbox_sha1sum: "843c56da78f9eb888274d2d4e12ab1d748ec12345", name: "name2", openid: null }, capture: 'value2' }, { actor: { account: null, degraded: false, mbox: null, mbox_sha1sum: "843c56da78f9eb888274d2d4e12ab1d748ec46234", name: "name", openid: null }, capture: 'value3' } ]; objects.filter((value, index, self) => { return self.indexof(value) === index; }).map(ele => { console.log(ele.capture); });
but can see, @ moment not filter of array elements.
at first thought return self.indexof(value.value) === index;
solve issue, returns empty array.
the desired outcome array not consist of last array element actor property matches first array element.
the expected result is:
[{ actor: { account: null, degraded: false, mbox: null, mbox_sha1sum: "843c56da78f9eb888274d2d4e12ab1d748ec46234", name: "name", openid: null }, capture: 'value' }, { actor: { account: null, degraded: false, mbox: null, mbox_sha1sum: "843c56da78f9eb888274d2d4e12ab1d748ec12345", name: "name2", openid: null }, capture: 'value2' } ];
the objects in array different objects, if happen have properties same values. .indexof()
function comparing references, not property values.
also, in practice, none of 3 objects have identical properties because have different .capture
value.
use .findindex()
instead of .indexof()
, can compare properties find matching objects:
objects.filter((value, index, self) => { return self.findindex(v => v.actor.name === value.actor.name) === index; })
here i'm using .actor.name
property, of course compare additional properties if needed.
(note .findindex()
es6 function, assume that's fine given code using arrow functions. or can polyfill it.)
var objects = [{ actor: { account: null, degraded: false, mbox: null, mbox_sha1sum: "843c56da78f9eb888274d2d4e12ab1d748ec46234", name: "name", openid: null }, capture: 'value' }, { actor: { account: null, degraded: false, mbox: null, mbox_sha1sum: "843c56da78f9eb888274d2d4e12ab1d748ec12345", name: "name2", openid: null }, capture: 'value2' }, { actor: { account: null, degraded: false, mbox: null, mbox_sha1sum: "843c56da78f9eb888274d2d4e12ab1d748ec46234", name: "name", openid: null }, capture: 'value3' } ]; objects.filter((value, index, self) => { return self.findindex(v => v.actor.name === value.actor.name) === index; }).map(ele => { console.log(ele.capture); });
Comments
Post a Comment