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

181
Visualizações
JS Filter Object that includes array

I tried to filter a JS object that includes an array. I want to check if id, matchcode and description. That was I tried was that:

    let search = articles.filter(function(item){
      return (item.matchcode.includes(articleno) || item.description.includes(articleno) || item.id.includes(articleno))
    })

That is the object:

let articles = '[ {
        "id": "35",
        "matchcode": "KM Anteile  S",
        "description": "KM Anteile  S ",
        "unit": "STK",
        "salesPrice": 0.7,
        "stock": "HL-F",
        "stockAmount": -3282
    },
    {
        "id": "41",
        "matchcode": "Arbeitszeit",
        "description": "Arbeitszeit ",
        "unit": "STD",
        "salesPrice": 76.8,
        "stock": "HL-F",
        "stockAmount": 0.75
    }, {
        "id": "502019",
        "matchcode": "Gummimuffe 100 mm",
        "description": "Gummimuffe 100 mm ",
        "unit": "STK",
        "salesPrice": 9.8,
        "stock": "HL-F",
        "stockAmount": 15
    }';

When I tried it, I get the error, that filter is not an function.

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

0

let articles = [ {
            "id": "35",
            "matchcode": "KM Anteile  S",
            "description": "KM Anteile  S ",
            "unit": "STK",
            "salesPrice": 0.7,
            "stock": "HL-F",
            "stockAmount": -3282
        },
        {
            "id": "41",
            "matchcode": "Arbeitszeit",
            "description": "Arbeitszeit ",
            "unit": "STD",
            "salesPrice": 76.8,
            "stock": "HL-F",
            "stockAmount": 0.75
        }, {
            "id": "502019",
            "matchcode": "Gummimuffe 100 mm",
            "description": "Gummimuffe 100 mm ",
            "unit": "STK",
            "salesPrice": 9.8,
            "stock": "HL-F",
            "stockAmount": 15
        }];
    let articleno = "Gummimuffe"
    let search = articles.filter(function(item){
          return (item.matchcode.includes(articleno) || item.description.includes(articleno) || item.id.includes(articleno))
        })
        console.log(search)

This is a sample example, which you can apply to your code as well.

let arr = JSON.parse('[{"value":"This is object insided array"}]');
console.log(arr)

about 4 years ago · Juan Pablo Isaza Relatório

0

You need to close your [ with ] and you have a string, not an array of objects. You can also use JSON.parse(). This would do the job:

let articles = [{
        "id": "35",
        "matchcode": "KM Anteile  S",
        "description": "KM Anteile  S ",
        "unit": "STK",
        "salesPrice": 0.7,
        "stock": "HL-F",
        "stockAmount": -3282
    },
    {
        "id": "41",
        "matchcode": "Arbeitszeit",
        "description": "Arbeitszeit ",
        "unit": "STD",
        "salesPrice": 76.8,
        "stock": "HL-F",
        "stockAmount": 0.75
    }, {
        "id": "502019",
        "matchcode": "Gummimuffe 100 mm",
        "description": "Gummimuffe 100 mm ",
        "unit": "STK",
        "salesPrice": 9.8,
        "stock": "HL-F",
        "stockAmount": 15
    }
];

Additional thing is that .filter() returns a new array while keeping original one as it is.

about 4 years ago · Juan Pablo Isaza Relatório

0

Because articles is a string you need to parse it by using JSON.parse(),


Note: as @Dai mentioned in the comment in this question includes() is case sensitive, so you might want to use another approach in order to have a friendly search (case insensitive)

I choosed to use new RegExp() and instead of includes() I used match()

const articles = `[ {
"id": "35",
"matchcode": "KM Anteile  S",
"description": "KM Anteile  S ",
"unit": "STK",
"salesPrice": 0.7,
"stock": "HL-F",
"stockAmount": -3282
}, {
  "id": "41",
  "matchcode": "Arbeitszeit",
  "description": "Arbeitszeit ",
  "unit": "STD",
  "salesPrice": 76.8,
  "stock": "HL-F",
  "stockAmount": 0.75
}, {
  "id": "502019",
  "matchcode": "Gummimuffe 100 mm",
  "description": "Gummimuffe 100 mm ",
  "unit": "STK",
  "salesPrice": 9.8,
  "stock": "HL-F",
  "stockAmount": 15
}]
`;
const articlenenoFilter = "arbeitsze"

const articleneno = new RegExp(articlenenoFilter, "i")

const search = JSON.parse(articles).filter((item) => (item.matchcode.match(articleneno) || item.description.match(articleneno) || item.id.match(articleneno)))

console.log(search)

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