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

136
Vistas
return an object with similar structured but filtered

I have the following object:

const myObject = {
"-Lz8YxHiwp8QZW3TqAFn": Object {
  "first": "Breanna",
  "last": "Blah",
},
"-Lz8YxHqQXWoaGOFRLrO": Object {
  "first": "Joshua",
  "last": "Blah",
},
"-Lz8YxHsMItaaTVNyQRE": Object {
  "first": "Joziah",
  "last": "Blah",
},
"-Lz8YxHwuVBMWl0Go6C5": Object {
  "first": "Lino",
  "last": "Blah",
},
"-Lz8YxHy0S-QkDaE1PkX": Object {
  "first": "Rebecca",
  "last": "Blah",
},
"-Lz8YxI5IItUiXX6rFn_": Object {
  "first": "Rosario",
  "last": "Blah",
},
"-Lz8YxIB_YTBF8liL855": Object {
  "first": "Alissa",
  "last": "Blah",
}
}

then I have a set with the following:

const mySet = {
  "-Lz8YxMp-V0TfiwrkM49",
  "-Lz8YxNQP2WkkO0qpkRJ",
  "-Lz8YxHy0S-QkDaE1PkX",
  "-MmFNgjyopU3E8z5g-zU",
  "-Lz8YxLVi_uZp_RkcRIH",
  "-MuEgimJOIbPw3GyKBJ3",
  "-Lz8YxOFpVHx2xPI1mUu",
  "-Lz8YxJ-_wuk8bmEyvGT",
  "-Lz8YxKj5WXY1oNwylQ4",
  "-Lz8YxN87U1YM6_fKgq1",
  "-Lz8YxOI4qszJvZhrSde",
  "-Lz8YxI5IItUiXX6rFn_",
}

I need to return myObject with ONLY the objects that match an item in mySet. I have done this with a map but it turns myObject into an array. The following uses map and returns an array which I can't use for this particular situation.

// const addGroupMembers = Object.entries(members)
//   .filter(([key]) => {
//     eachHit++;
//     console.log(key)
//     return attendanceGroupMembers.has(key);
//   })
//   .map(([_, members]) => {
//     eachHit++;
//     console.log('in map')
//     return members;
//   });

I would like to retain the myObject structure but with only the items found. Any idea how I can accomplish this?

about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

You could convert myObject into an array of entries using Object.entries() then use Set.has() along with Array.filter(), and finally use Object.fromEntries() to convert the filtered array of entries back into an object:

const myObject = {
    "-Lz8YxHiwp8QZW3TqAFn": { first: "Breanna", last: "Blah" },
    "-Lz8YxHqQXWoaGOFRLrO": { first: "Joshua", last: "Blah" },
    "-Lz8YxHsMItaaTVNyQRE": { first: "Joziah", last: "Blah" },
    "-Lz8YxHwuVBMWl0Go6C5": { first: "Lino", last: "Blah" },
    "-Lz8YxHy0S-QkDaE1PkX": { first: "Rebecca", last: "Blah" },
    "-Lz8YxI5IItUiXX6rFn_": { first: "Rosario", last: "Blah" },
    "-Lz8YxIB_YTBF8liL855": { first: "Alissa", last: "Blah" },
};

const mySet = new Set([
    "-Lz8YxMp-V0TfiwrkM49",
    "-Lz8YxNQP2WkkO0qpkRJ",
    "-Lz8YxHy0S-QkDaE1PkX",
    "-MmFNgjyopU3E8z5g-zU",
    "-Lz8YxLVi_uZp_RkcRIH",
    "-MuEgimJOIbPw3GyKBJ3",
    "-Lz8YxOFpVHx2xPI1mUu",
    "-Lz8YxJ-_wuk8bmEyvGT",
    "-Lz8YxKj5WXY1oNwylQ4",
    "-Lz8YxN87U1YM6_fKgq1",
    "-Lz8YxOI4qszJvZhrSde",
    "-Lz8YxI5IItUiXX6rFn_",
]);

const myObjectFiltered = Object.fromEntries(
    Object.entries(myObject).filter(([key]) => mySet.has(key))
);

console.log(myObjectFiltered);

about 4 years ago · Juan Pablo Isaza Denunciar

0

Sure, just use .reduce to assign to a new object:

const addGroupMembers = Object.entries(myObject).filter(entry => 
mySet.has(entry[0])).reduce((accum, [k, v]) => {
accum[k] = v;
return accum;
}, {});
about 4 years ago · Juan Pablo Isaza Denunciar

0

The question is to me a bit confusing sorry, but I hope this can contribute :

Basically, in javascript, to directly change an existing object to remove an element, and not create an altered copy, you can use :

delete someObject[key]

it will change directly your Object, which will applies to everything which have it referenced.

as such :

const myObject = {
    "-Lz8YxHiwp8QZW3TqAFn":  {
    "first": "Breanna",
        "last": "Blah",
},
"-Lz8YxHqQXWoaGOFRLrO": {
    "first": "Joshua",
        "last": "Blah",
},
"-Lz8YxHsMItaaTVNyQRE": {
    "first": "Joziah",
        "last": "Blah",
},
"-Lz8YxHwuVBMWl0Go6C5": {
    "first": "Lino",
        "last": "Blah",
},
"-Lz8YxHy0S-QkDaE1PkX": {
    "first": "Rebecca",
        "last": "Blah",
},
"-Lz8YxI5IItUiXX6rFn_": {
    "first": "Rosario",
        "last": "Blah",
},
"-Lz8YxIB_YTBF8liL855": {
    "first": "Alissa",
        "last": "Blah",
}
}

const mySet = [
    "-Lz8YxMp-V0TfiwrkM49",
    "-Lz8YxNQP2WkkO0qpkRJ",
    "-Lz8YxHy0S-QkDaE1PkX",
    "-MmFNgjyopU3E8z5g-zU",
    "-Lz8YxLVi_uZp_RkcRIH",
    "-MuEgimJOIbPw3GyKBJ3",
    "-Lz8YxOFpVHx2xPI1mUu",
    "-Lz8YxJ-_wuk8bmEyvGT",
    "-Lz8YxKj5WXY1oNwylQ4",
    "-Lz8YxN87U1YM6_fKgq1",
    "-Lz8YxOI4qszJvZhrSde",
    "-Lz8YxI5IItUiXX6rFn_",
]

for (const i in myObject)
    if (!mySet.includes(i))
        delete myObject[i];

console.log(myObject);

An example case. Sorry for changing the syntax but it looks weird, hope it helps.

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