I have the following objects:
const allMembers = {
"-Lz8YxHiwp8QZW3TqAFn": {
"first": "foo",
"last": "bar",
"uid": "-Lz8YxHiwp8QZW3TqAFn"
},
"-Lz8YxHqQXWoaGOFRLrO": {
"first": "foo",
"last": "bar",
"uid": "-Lz8YxHqQXWoaGOFRLrO"
},
"-Lz8YxHsMItaaTVNyQRE": {
"first": "foo",
"last": "bar",
"uid": "-Lz8YxHsMItaaTVNyQRE"
},
"-Lz8YxHwuVBMWl0Go6C5": {
"first": "foo",
"last": "bar",
"uid": "-Lz8YxHwuVBMWl0Go6C5"
},
"-Lz8YxHy0S-QkDaE1PkX": {
"first": "foo",
"last": "bar",
"uid": "-Lz8YxHy0S-QkDaE1PkX"
},
}
const attendanceData = {
"-Lz8YxHiwp8QZW3TqAFn": true,
"-Lz8YxHqQXWoaGOFRLrO": true,
"-Lz8YxHsMItaaTVNyQRE": true,
"-Lz8YxHwuVBMWl0Go6C5": false,
"-Lz8YxHy0S-QkDaE1PkX": true,
"-Lz8YxIFA1XGVmaNfNr3": false,
"-Lz8YxIJVZnIIj7RgEzg": false,
}
I need to clean up the allMembers to only return if it's found as true in attendanceData. Here is my attempt but it's obviously not working. Also the way I'm doing it has to loop the first one over the other times.
const membersHere = Object.keys(allMembers).filter(memEl => Object.keys(attendanceData).find(attendEl => memEl == attendEl));
CodePudding user response:
Convert the
allMembers
object into an array of key value pairs using Object.entries.Filter the items that have value
true
using Array.prototype.filter.Transform the array back to an object using Object.fromEntries.
const
allMembers = {'-Lz8YxHiwp8QZW3TqAFn': {first: 'foo',last: 'bar',uid: '-Lz8YxHiwp8QZW3TqAFn'},'-Lz8YxHqQXWoaGOFRLrO': {first: 'foo',last: 'bar',uid: '-Lz8YxHqQXWoaGOFRLrO'},'-Lz8YxHsMItaaTVNyQRE': {first: 'foo',last: 'bar',uid: '-Lz8YxHsMItaaTVNyQRE'},'-Lz8YxHwuVBMWl0Go6C5': {first: 'foo',last: 'bar',uid: '-Lz8YxHwuVBMWl0Go6C5'},'-Lz8YxHy0S-QkDaE1PkX': {first: 'foo',last: 'bar',uid: '-Lz8YxHy0S-QkDaE1PkX'}},
attendanceData = {'-Lz8YxHiwp8QZW3TqAFn': true,'-Lz8YxHqQXWoaGOFRLrO': true,'-Lz8YxHsMItaaTVNyQRE': true,'-Lz8YxHwuVBMWl0Go6C5': false,'-Lz8YxHy0S-QkDaE1PkX': true,'-Lz8YxIFA1XGVmaNfNr3': false,'-Lz8YxIJVZnIIj7RgEzg': false},
filteredMembers = Object.fromEntries(
Object.entries(allMembers).filter(([k]) => attendanceData[k])
);
console.log(filteredMembers);
You can also do it using Array.prototype.reduce
const
allMembers = {'-Lz8YxHiwp8QZW3TqAFn': {first: 'foo',last: 'bar',uid: '-Lz8YxHiwp8QZW3TqAFn'},'-Lz8YxHqQXWoaGOFRLrO': {first: 'foo',last: 'bar',uid: '-Lz8YxHqQXWoaGOFRLrO'},'-Lz8YxHsMItaaTVNyQRE': {first: 'foo',last: 'bar',uid: '-Lz8YxHsMItaaTVNyQRE'},'-Lz8YxHwuVBMWl0Go6C5': {first: 'foo',last: 'bar',uid: '-Lz8YxHwuVBMWl0Go6C5'},'-Lz8YxHy0S-QkDaE1PkX': {first: 'foo',last: 'bar',uid: '-Lz8YxHy0S-QkDaE1PkX'}},
attendanceData = {'-Lz8YxHiwp8QZW3TqAFn': true,'-Lz8YxHqQXWoaGOFRLrO': true,'-Lz8YxHsMItaaTVNyQRE': true,'-Lz8YxHwuVBMWl0Go6C5': false,'-Lz8YxHy0S-QkDaE1PkX': true,'-Lz8YxIFA1XGVmaNfNr3': false,'-Lz8YxIJVZnIIj7RgEzg': false},
filteredMembers = Object.entries(allMembers).reduce(
(r, [k, v]) => (attendanceData[k] && (r[k] = v), r),
{}
);
console.log(filteredMembers);
Other relevant documentations:
CodePudding user response:
You can use reduce
method:
const allMembers = {
"-Lz8YxHiwp8QZW3TqAFn": {
"first": "foo",
"last": "bar",
"uid": "-Lz8YxHiwp8QZW3TqAFn"
},
"-Lz8YxHqQXWoaGOFRLrO": {
"first": "foo",
"last": "bar",
"uid": "-Lz8YxHqQXWoaGOFRLrO"
},
"-Lz8YxHsMItaaTVNyQRE": {
"first": "foo",
"last": "bar",
"uid": "-Lz8YxHsMItaaTVNyQRE"
},
"-Lz8YxHwuVBMWl0Go6C5": {
"first": "foo",
"last": "bar",
"uid": "-Lz8YxHwuVBMWl0Go6C5"
},
"-Lz8YxHy0S-QkDaE1PkX": {
"first": "foo",
"last": "bar",
"uid": "-Lz8YxHy0S-QkDaE1PkX"
},
}
const attendanceData = {
"-Lz8YxHiwp8QZW3TqAFn": true,
"-Lz8YxHqQXWoaGOFRLrO": true,
"-Lz8YxHsMItaaTVNyQRE": true,
"-Lz8YxHwuVBMWl0Go6C5": false,
"-Lz8YxHy0S-QkDaE1PkX": true,
"-Lz8YxIFA1XGVmaNfNr3": false,
"-Lz8YxIJVZnIIj7RgEzg": false,
}
const reduced = Object.entries(allMembers).reduce((data, [key, value]) => {
if (attendanceData[key] ?? false)
data[key] = value;
return data;
}, {});
console.log(reduced);
CodePudding user response:
You can combine Object.keys(), Array.prototype.filter() and Array.prototype.reduce()
Code:
const allMembers = {'-Lz8YxHiwp8QZW3TqAFn': {first: 'foo',last: 'bar',uid: '-Lz8YxHiwp8QZW3TqAFn',},'-Lz8YxHqQXWoaGOFRLrO': {first: 'foo',last: 'bar',uid: '-Lz8YxHqQXWoaGOFRLrO',},'-Lz8YxHsMItaaTVNyQRE': {first: 'foo',last: 'bar',uid: '-Lz8YxHsMItaaTVNyQRE',},'-Lz8YxHwuVBMWl0Go6C5': {first: 'foo',last: 'bar',uid: '-Lz8YxHwuVBMWl0Go6C5',},'-Lz8YxHy0S-QkDaE1PkX': {first: 'foo',last: 'bar',uid: '-Lz8YxHy0S-QkDaE1PkX',},}
const attendanceData = {'-Lz8YxHiwp8QZW3TqAFn': true,'-Lz8YxHqQXWoaGOFRLrO': true,'-Lz8YxHsMItaaTVNyQRE': true,'-Lz8YxHwuVBMWl0Go6C5': false,'-Lz8YxHy0S-QkDaE1PkX': true,'-Lz8YxIFA1XGVmaNfNr3': false,'-Lz8YxIJVZnIIj7RgEzg': false,}
const result = Object
.keys(allMembers)
.filter((k) => attendanceData[k])
.reduce((a, k) => (a[k] = allMembers[k], a), {})
console.log(result)