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

135
Views
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 answers
Answer question

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 Report

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 Report

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 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!