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

178
Views
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 answers
Answer question

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 Report

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 Report

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