Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

138
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda