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