data = { boxNoTo: 1, boxNoFrom: 1, size: 'M', }
array = [{ size: 'M', },{ size: 'M', },{ size: 'S,M,L,XS', boxNoTo: 1, boxNoFrom: 1, country: 'CA', name: 'Josh' }]
what I did is like this
const INDEX = array.findIndex((x) => {
if (x.size.split(',').length > 0) {
return (
x.size.split(',').filter((y) => y === data['size'])[0] ===
data['size'] &&
x.boxNoFrom === data['boxNoFrom'] &&
x.boxNoTo === data.boxNoTo
);
} else {
return (
x.size === data['size'] &&
x.boxNoFrom === data['boxNoFrom'] &&
x.boxNoTo === data.boxNoTo
);
}
});
What I'm trying to do is to get the index of
{
size: 'S,M,L,XS',
boxNoTo: 1,
boxNoFrom: 1,
country: 'CA',
name: 'Josh'
}
You could try something like this:
const data = { boxNoTo: 1, boxNoFrom: 1, size: 'M', };
const array = [{ size: 'M', },{ size: 'M', },{ size: 'S,M,L,XS', boxNoTo: 1, boxNoFrom: 1, country: 'CA', name: 'Josh' }];
const { size, ...rest } = data;
const index = array.findIndex(item =>
item.size.split(',').includes(size)
&& Object.keys(rest).every(key => item[key] === rest[key])
);
console.log(index);