Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

321
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda