0

This is my issue, I try to get result of items base on my filter array.

const papaFilter=[
{type:'event', value: ['In Person']},
{type:'city', value: ['Boston', 'Miami', 'New York']}
]

const items = [
{city: 'Boston', type: 'In Person'},
{city: 'New Jersey', type: 'In Person'},
{city:'Boston', type:'Virtual'}
]

const filteredResults = items.filter(el => papaFilter.some(filterEl => el[filterEl.type] === filterEl.value));

I want the first object of my items because papafilter contain Boston and In Person.

I know in my condition filterEl.value is an array of multiple value so this condition doesn't work. Any Ideas?

2
  • because the papaFilter contain 'In Person' and 'Boston' so it match with my first object of items. Commented Jan 12, 2022 at 18:39
  • why do you have type in items, if you better take event for it? it would make the search easier. Commented Jan 12, 2022 at 19:03

2 Answers 2

1

You could filter the array checking all filter item.

This approach takes an object for replacing keys.

const
    replacements = { event: 'type' },
    filter = [{ type: 'event', value: ['In Person'] }, { type: 'city', value: ['Boston', 'Miami', 'New York'] }],
    items = [{ city: 'Boston', type: 'In Person' }, { city: 'New Jersey', type: 'In Person' }, { city: 'Boston', type: 'Virtual' }],
    result = items.filter(o =>
        filter.every(({ type, value }) => value.includes(o[(replacements[type] || type)]))
    );

console.log(result);

Sign up to request clarification or add additional context in comments.

3 Comments

You are a killer 😎👌! thank you
How this function can return a result if i have more key and value on my events? const filter = [ { type: 'type', value: ['In Person', 'Virtual'] }, { type: 'city', value: ['Boston', 'Miami', 'New York'] }, ] const events = [ { node: { city: 'Boston', type: 'In Person', name: 'Boston Party' } }, { node: { city: 'New Jersey', type: 'In Person', name: 'Hello Cookie' } }, { node: { city: 'Boston', type: 'Virtual', name: 'Sales Kick Off' } }, ]
function const result = events.filter(o => Object.entries(o.node).every(([k, v]) => filter.some(({ type, value }) => type === k && value.includes(v)),),)
0

I think you are looking for Array.includes. So, filterEl.value.includes(el[filterEl.type]) in the last block should give you the desired result.

You can find more about it here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/includes

2 Comments

this code const filteredResults = card.filter(el => Filter.some(filterEl => filterEl.value.includes(el[filterEl.type])), ) give me the 1 and 3 objects. I just want the first object of my items because papaFilter contain 'In Person' and 'Boston' so it match with my first object of items.
In that case, some on papaFilter won't help you, you need to iterate through all entries in the filter method of items and then return true only when all entries from papaFilter's iteration gives value true. Also, I think type:"event" should be type:"type".

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.