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

109
Views
Filter Object based on Keys and compare with array

I currently have an object containing key with multiple values.

I also have an array containing a simple key check i.e. ["random", "three"].

I want to return mainData but only with the object and the data from whats in the array i.e. ["random", "three"]

Current Code:

const mainData = {
    random: {
        name: "Random Entity",
        date: "05/04/2022",
        startTime: "19:00",
        finishTime: "00:00",
    },
    one: {
        name: "One Entity",
        date: "16/04/2022",
        startTime: "16:00",
        finishTime: "20:00",
    },
    three: {
        name: "Three Entity",
        date: "19/04/2022",
        startTime: "10:00",
        finishTime: "11:00",
    },
};

export default mainData;

Desired Output

const mainData = {
    random: {
        name: "Random Entity",
        date: "05/04/2022",
        startTime: "19:00",
        finishTime: "00:00",
    },
    three: {
        name: "Three Entity",
        date: "19/04/2022",
        startTime: "10:00",
        finishTime: "11:00",
    },
};

Attempt:

let filterKey = 'random';
const result = Object.entries(mainData).filter(([k, v]) => k== filterKey);

This works only as a single search, not an array search.

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

0

Map the array to an entries array ([[key,val],[key,val],...]) and pass it through Object.fromEntries()

const mainData = {"random":{"name":"Random Entity","date":"05/04/2022","startTime":"19:00","finishTime":"00:00"},"one":{"name":"One Entity","date":"16/04/2022","startTime":"16:00","finishTime":"20:00"},"three":{"name":"Three Entity","date":"19/04/2022","startTime":"10:00","finishTime":"11:00"}}

const filters = ["random", "nope", "three"]

const result = Object.fromEntries(
  filters
    .filter(
      key => key in mainData // only include keys that exist in mainData
    ) 
    .map(
      key => [ key, mainData[key] ]
    )
)

console.log(result)
.as-console-wrapper { max-height: 100% !important; }

about 4 years ago · Juan Pablo Isaza Report

0

If you know the keys you'd like to exclude you can use this approach:

const mainData = { random: { name: "Random Entity", date: "05/04/2022", startTime: "19:00", finishTime: "00:00", }, one: { name: "One Entity", date: "16/04/2022", startTime: "16:00", finishTime: "20:00", }, three: { name: "Three Entity", date: "19/04/2022", startTime: "10:00", finishTime: "11:00" } };

const {one, ...desiredData} = mainData;

console.log( desiredData );

Alternatively, if you have the filter keys, you can map() the keys to [key, value], from [key,mainData[key]], then create an object from these key-value pairs using Object.fromEntries() as in the demo below:

const mainData = { random: { name: "Random Entity", date: "05/04/2022", startTime: "19:00", finishTime: "00:00", }, one: { name: "One Entity", date: "16/04/2022", startTime: "16:00", finishTime: "20:00", }, three: { name: "Three Entity", date: "19/04/2022", startTime: "10:00", finishTime: "11:00" } };

const desiredData = Object.fromEntries( ["random", "three"].map(f => [f, mainData[f]]) );

console.log( desiredData );

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!