Reputation: 63
How can I check in a array of objects if one of the objects contain just a part of a string ?
products = [
{id: 'pdc', code: '123456', name: 'pompa'},
{id: 'kbr', code: '2365', name: 'kit kbr'},
{id: 'boiler', code: '23165', name: 'VPB'}
];
And let's say I want to find if the this.products contain an object with 'kbr' ? And, as you can see, that object has a longer name.. Something like:
const matches = this.products.filter(s => s.includes('kbr'));
Thank you!
Upvotes: 0
Views: 79
Reputation: 182
In the filter function, you didn't specified the field to be checked. So you've to write this if you want to check on name property:
const matches = this.products.filter(s => s.name.includes('kbr'));
Upvotes: 1
Reputation: 541
I would use Array.find() function, because it stops at the first matching object:
const products = [
{id: 'pdc', code: '123456', name: 'pompa'},
{id: 'kbr', code: '2365', name: 'kit kbr'},
{id: 'boiler', code: '23165', name: 'VPB'}
];
const testPattern = 'kbr';
const productsContains = (products, pattern) =>
products.find(
({id, name}) =>
id.indexOf(pattern) >= 0 || name.indexOf(pattern) >= 0
) ? true : false;
console.log(`productsContains '${testPattern}':`, productsContains(products, testPattern));
It isn't working in IE, but there is polyfill.
Upvotes: 0
Reputation: 3731
using some
is a better approach for this, it will stop as soon as it founds something.
for larger arrays it will be an improvement :)
products = [
{id: 'pdc', code: '123456', name: 'pompa'},
{id: 'kbr', code: '2365', name: 'kit kbr'},
{id: 'boiler', code: '23165', name: 'VPB'}
];
const result = products.some(product => Object.keys(product).some(key => product[key].includes('kbr')))
console.log(result)
Upvotes: 1
Reputation: 17190
You can generate the array of the Object.values() inside the filter method and check if some of the values
match() the string you are searching for:
const products = [
{id: 'pdc', code: '123456', name: 'pompa'},
{id: 'kbr', code: '2365', name: 'kit kbr'},
{id: 'boiler', code: '23165', name: 'VPB'}
];
let res = products.filter(o =>
{
return Object.values(o).some(x => ("" + x).match("kbr"));
});
console.log(res);
Upvotes: 0
Reputation: 17654
you can stringify the object values and see if it has the substring you're looking for :
const products = [{ id: 'pdc', code: '123456', name: 'pompa' }, { id: 'kbr', code: '2365', name: 'kit kbr' }, { id: 'boiler', code: '23165', name: 'VPB' }];
const result = products.filter(prod =>
Object.values(prod).join(' ').includes('kbr'));
console.log(result)
Upvotes: 0
Reputation: 17636
You were almost there. You simply needed to access the "name" property of your object "s". In this scenario I used destructuring to get quick access to "name" property.
const products=[{id:"pdc",code:"123456",name:"pompa"},{id:"kbr",code:"2365",name:"kit kbr"},{id:"boiler",code:"23165",name:"VPB"}];
const res = products.filter(({name})=>name.includes("kbr"));
console.log(res);
Upvotes: 1
Reputation: 138477
You could compose all values to a string and check if it includes that:
const matches = this.products.filter(s => Object.values(s).join("¥").includes('kbr'));
Upvotes: 0
Reputation: 386756
You need the values of the object and then iterate until a match is found.
var products = [{ id: 'pdc', code: '123456', name: 'pompa' }, { id: 'kbr', code: '2365', name: 'kit kbr' }, { id: 'boiler', code: '23165', name: 'VPB' }],
matches = this.products.filter(o =>
Object.values(o).some(v => v.toString().includes('kbr')));
console.log(matches);
Upvotes: 1