1

I'm trying to make an advanced searching for my app, it has a stores array of objects, every store is an object and every store has an array of objects that holds the items of that store, (every item is an object). (i'll leave the stores array down so u understand what i mean)

So basically, I want the user to filter the stores by the item names, but i got stuck and whatever i tried didn't seem to be working. here is the code:

stores array:

let stores = [
  {
    name:"",
    type:"",
    items:[
      {name:"tomato", quantity:"145", unit:"g"},  //this is what i want to filter with
      {name:"other items here", quantity:"45", unit:"kg"},
      {name:"example item", quantity:"74", unit:"l"},
    ]
  }
]

The filtering way I've tried:

let userInput = "tomato";


//this outputs the original array without any filtering

    let filteredStores = stores.filter(store=>{
      return store.items.filter(item=>{
        return item.name.includes(userInput)
      })
    })

Hope someone understands how i want to filter the stores, thanks

6
  • 1
    What is your expected output? Commented Apr 25, 2020 at 20:19
  • In that case with the userInput of "tomato", the output should be [{name:"tomato", quantity:"145", unit:"g"}] Commented Apr 25, 2020 at 20:21
  • 1
    And there are multiple objects into the store array? Commented Apr 25, 2020 at 20:26
  • @SajeebAhamed yes Commented Apr 25, 2020 at 20:27
  • 1
    What if the store look like this- let stores = [ { name:"", type:"", items:[ {name:"tomato", quantity:"145", unit:"g"}, {name:"other items here", quantity:"45", unit:"kg"}, {name:"example item", quantity:"74", unit:"l"}, ] }, { name:"", type:"", items:[ {name:"tomato", quantity:"145", unit:"g"}, ] } ]; Commented Apr 25, 2020 at 20:28

3 Answers 3

2

Array#filter will return an empty array when no matches are found. This is a truthy value, you can find this by doing !!array.filter(() => false). You need to call .length on the second filter to be able to determine whether it has found any matches or not, 0 is falsey and anything else is truthy.

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

2 Comments

you mean smth like this ? return store.items.filter(item=>{ return item.name.includes("d").length }) well that gave me the same input, can u help plz
@SalahEddineMakdour - that is right. if you wanted to be more explicit you could add .length > 0.
1

You can try this:

let stores = [
  {
    name:"",
    type:"",
    items:[
      {name:"tomato", quantity:"145", unit:"g"},  //this is what i want to filter with
      {name:"other items here", quantity:"45", unit:"kg"},
      {name:"example item", quantity:"74", unit:"l"},
    ]
  },
 {
    name:"",
    type:"",
    items:[
      {name:"tomatos", quantity:"14", unit:"kg"},
    ]
  }
];

let UserInput = "tomato";

const res = stores.filter(({items}) => items.find(item => item.name.includes(UserInput)));

console.log(res);
.as-console-wrapper{min-height: 100%!important; top: 0}

6 Comments

in ur code, im getting only the items as a result, i want to get the whole store that has those items, hope u understand
In your comment you said the output would be [{name:"tomato", quantity:"145", unit:"g"}]
Check my updated answer, I think this is what you are wanting.
oh that was a mistake, im sorry, the output would be the whole store that has the items that the user searches for , excuse me plz was a mistake
well now the stores only have the 1 item, i want to filter the stores with their items and return the filtered stores without any changes
|
1

let stores = [
  {
    name:"",
    type:"",
    items:[
      {name:"tomato", quantity:"145", unit:"g"},  //this is what i want to filter with
      {name:"other items here", quantity:"45", unit:"kg"},
      {name:"example item", quantity:"74", unit:"l"},
    ]
  }
]

let filterdStores = stores.filter(s=>s.items.some(i=>i.name==='tomato'));

console.log(JSON.stringify(filterdStores,null,2));

Use efficient Array.some

3 Comments

@SalahEddineMakdour Array.some will break the internal block when find a match, So it won't search whole of array when saw the match. It is more performant.
the problem with this that when the page loads, it won't show the stores until i type smth in search bar, so when filter string is empty , it gives an empty array which i don't want
@SalahEddineMakdour Your fetch function must be flexible and conditional!

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.