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

177
Visualizações
Filter json based on key and match it against one of the values from array

I have json file: products.json:

[
{
"id": "1",
"category": "Fruit from USA",
"name": "Banana",
},
{
"id": "2",
"category": "Fruit from Brazil",
"name": "Long Banana",
},
{
"id": "3",
"category": "Vegetable",
"name": "Carrot",
},
{
"id": "4",
"category": "Car from USA",
"name": "Ford",
},
{
"id": "5",
"category": "Car from Germany",
"name": "Audi",
},
{
"id": "6",
"category": "Train from Italy",
"name": "Pendolino",
},
]

Then I have an array

testMatch.match: ['Car', 'Fruit'].

I want to filter out products.json to return only objects that have category starts with any of elements of matchCategory in ES6

What i have so far is:

const productList = products.filter(filteredCategory => filteredCategory.category.startsWith(testMatch.match));

But it does not work if there is more than 1 element in testMatch.match and if there is none - it returns all products and not none.

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

You could iterate match as well with Array#some and exit on find.

const
    products = [{ id: "1", category: "Fruit from USA", name: "Banana" }, { id: "2", category: "Fruit from Brazil", name: "Long Banana" }, { id: "3", category: "Vegetable", name: "Carrot" }, { id: "4", category: "Car from USA", name: "Ford" }, { id: "5", category: "Car from Germany", name: "Audi" }, { id: "6", category: "Train from Italy", name: "Pendolino" }],
    match = ['Car', 'Fruit'],
    productList = products.filter(({ category }) =>
        match.some(m => category.startsWith(m))
    );

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

about 4 years ago · Juan Pablo Isaza Relatório

0

Use Array.some() to test if it starts with any of the strings in testMatch.match.

const products = [{
    "id": "1",
    "category": "Fruit from USA",
    "name": "Banana",
  },
  {
    "id": "2",
    "category": "Fruit from Brazil",
    "name": "Long Banana",
  },
  {
    "id": "3",
    "category": "Vegetable",
    "name": "Carrot",
  },
  {
    "id": "4",
    "category": "Car from USA",
    "name": "Ford",
  },
  {
    "id": "5",
    "category": "Car from Germany",
    "name": "Audi",
  },
  {
    "id": "6",
    "category": "Train from Italy",
    "name": "Pendolino",
  },
];

const testMatch = {
  match: ['Car', 'Fruit']
};

const productList = products.filter(filteredCategory =>
  testMatch.match.some(match => filteredCategory.category.startsWith(match)));

console.log(productList);

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