ganesh kaspate
ganesh kaspate

Reputation: 2685

On filter not getting the data using filter function using javasctipt

I have an array of object which looks like

const data = [{excluded: [{id: "100"}, {id: "200"}], id: "1"}, {excluded: [{id: "300"}, {id: "400"}], id: "2"}, {excluded: [{id: "300"}, {id: "200"}], id: "400"}]

Now, Here I am trying to get those values which does not have the given excluded with the following logic.

const getvalues = (
  (data, inputId) => {
    return data.filter(bsg => {
      let excludedvalues = (bsg?.excluded || []).map(
        value => value.id
      )
      return !_.includes(excludedvalues, inputId)
    })
  }
)

I was expecting , if the given inputId is present in the excluded object then that object should get filterd.

here _ is a lodsh

So, is there any thing which i am doing wrong ?

thanks.

Upvotes: 0

Views: 35

Answers (2)

Apostolos
Apostolos

Reputation: 10463

I'm not sure I quite understood your question, but do you need this?

const data = [{excluded: [{id: "100"}, {id: "200"}], id: "1"}, {excluded: [{id: "300"}, {id: "400"}], id: "2"}, {excluded: [{id: "300"}, {id: "200"}], id: "400"}]

const getValues = (data, inputId) => data.filter(dt => dt.excluded.every(obj => obj.id !== inputId));
getValues(data, "300");

filter all the data and find only the ones that don't include this value in the excluded array?

The previous code snippet will result in the first element only.

Upvotes: 1

Angel
Angel

Reputation: 100

const data = [{excluded: [{id: "100"}, {id: "200"}], id: "1"}, 
              {excluded: [{id: "300"}, {id: "400"}], id: "2"}, 
              {excluded: [{id: "300"}, {id: "200"}], id: "400"}
             ]


const getvalues = (
  (data, inputId) => {
    return data.filter(bsg => {
      let excludedvalues = (bsg?.excluded || []).map(
        value => value.id
      )
      return !excludedvalues.includes(inputId.toString())
    })
  }
)
                            
console.log(getvalues(data, 400)); 

Not sure i totally undertsood.

Here with 400 at second argument we filter every array element that has a 400 as and id in an object in the excluded array.

Upvotes: 0

Related Questions