Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

186
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda