I have a variable, memberId
which has a unique value. I'm trying to use Object.values()
on my array of Objects from firebase ,to return true for the unique firebase ids in my array. Here is a console.log of my memberArr
which is defined before this onValue
call as let memberArr= [];
[
{
"-N0IZXQkPiHD9yfwi-M1": {
"member_id": "7cCfvX0eAlPGmamCTahFSu4p2xl1"
}
},
{
"-N0KSFc1rg91sufbUftO": {
"member_id": "7cCfvX0eAlPGmamCTahFSu4p2xl1"
},
"-N0KSRwxNp34-JhkLWDE": {
"member_id": "ssKze4rM5ucJWY9oBaJAMY7zUG03"
}
},
{
"-N0bdv6XGGfW-HDA2heI": {
"member_id": "Lu7cfs7stqhgXXzAUp9iA7OxYaH2"
}
},
{
"-N0bnRZKUDBULaCECPfq": {
"member_id": "Lu7cfs7stqhgXXzAUp9iA7OxYaH2"
}
}
]
my memberId
has the same value as those last two values for 2
and 3
where the member_id: 'Lu7cfs..
so after this block of code:
onValue(dbRef, (snapshot) => {
const data = snapshot.val();
for (var key in data) {
if (data.hasOwnProperty(key)) {
groupArr = data[key];
}
}
snapshot.forEach((groupSnapshot) => {
memberArr.push(groupSnapshot.child("members").val());
let memberExists = Object.values(memberArr).includes(memberId);
console.log(memberArr);
});
});
memberExists
should return true for the last two but it returns false for all of them.
Here is a picture of the console.log for the array:
CodePudding user response:
You need to map the array to array of member ids, then count them and and filter out the ones, with more than 1 occurrence.
To get an array of all the ids:
const ids = memberArr
.map(entry => Object.values(entry))
.map(({ member_id }) => member_id);
// ids = ['7cCfvX0eAlPGmamCTahFSu4p2xl1', '7cCfvX0eAlPGmamCTahFSu4p2xl1', 'ssKze4rM5ucJWY9oBaJAMY7zUG03', 'Lu7cfs7stqhgXXzAUp9iA7OxYaH2', 'Lu7cfs7stqhgXXzAUp9iA7OxYaH2']
Here is a full code to get unique ids:
const countMap = memberArr
.map(entry => Object.values(entry))
.map(({ member_id }) => member_id)
.reduce((idsCountMap, id) => {
idsCountMap[id] = idsCountMap[id] || 0;
idsCountMap[id] ;
return idsCountMap;
}, {});
const uniqueIds = Object.entries(countMap)
.filter(([, count]) => count === 1)
.map(([id]) => id);
CodePudding user response:
Got it working! Basically needed to create an array of the Object.values() and do a forEach
on each of those objects, then just do an if statement
comparing the obj.member_id
with my memberId
snapshot.forEach((groupSnapshot) => {
var memberValue = (groupSnapshot.child('members').val())
var uniqueMemberArr = (Object.values(memberValue))
uniqueMemberArr.forEach((memberObj) => {
if(memberObj.member_id == memberId) {
....
CodePudding user response:
You can do:
const data = [{'-N0IZXQkPiHD9yfwi-M1': { member_id: '7cCfvX0eAlPGmamCTahFSu4p2xl1'}},{'-N0KSFc1rg91sufbUftO': {member_id: '7cCfvX0eAlPGmamCTahFSu4p2xl1'},'-N0KSRwxNp34-JhkLWDE': {member_id: 'ssKze4rM5ucJWY9oBaJAMY7zUG03'}},{'-N0bdv6XGGfW-HDA2heI': {member_id: 'Lu7cfs7stqhgXXzAUp9iA7OxYaH2'}},{'-N0bnRZKUDBULaCECPfq': {member_id: 'Lu7cfs7stqhgXXzAUp9iA7OxYaH2'}}]
const ids = data
.map(
o => Object
.values(o)
.map(o => o.member_id)
)
.flat()
const hasDuplicatedIds = (new Set(ids)).size !== ids.length
console.log(hasDuplicatedIds)