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

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

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

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