Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

324
Views
Why is my Objects.values() returning false when my array of Objects has a matching value?

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:

enter image description here

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

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);
about 4 years ago · Juan Pablo Isaza Report

0

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)

about 4 years ago · Juan Pablo Isaza Report

0

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) {
      ....

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!