1

I am trying to do filter an array based on flag isSome: true. I want to filter when that flag is not present in that array

var value = [
      { "somevalues": {},
        "moreDetails": {
          "isSome": "true"
        }
      },
      { "somevalues": {},
        "moreDetails": {}
      },
      { "somevalues": {},
        "moreDetails": {}
      },
    ]
const valuewithisSome = value.filter(o => o.moreDetails &&  o.moreDetails.isSome);
const valuewithoutisSome = value.filter(o => o.moreDetails &&  !o.moreDetails.isSome);
console.log(valuewithisSome);
console.log(valuewithoutisSome);

valuewithisSome is working as expected and returning array with isSome: true.

valuewithoutisSome is not working as expected as i don't want to pass isSome false in morevalues array, is there a way to filter without even passing that flag?

2
  • 2
    How exactly is the "without" version not working? As written it should be fine. Commented Dec 24, 2018 at 14:05
  • 2
    "Run code snippet" shows that it actually works. This is probably not your real code. Commented Dec 24, 2018 at 14:09

2 Answers 2

1

The "without" code you already have should work:

const valuewithoutisSome = value.filter(o => o.moreDetails &&  !o.moreDetails.isSome);

The expression o.moreDetails.isSome will evaluate to undefined if the "isSome" property is missing or explicitly false-y, so !o.moreDetails.isSome will be true in that case.

Now while that will work, if you want to explicitly test for the property being false only when it's actually present, you can do this:

const valuewithoutisSome = value.filter(o => 
  o.moreDetails &&
  (!("isSome" in o.moreDetails) || !o.moreDetails.isSome)
);

That will only return false (and filter out the array entry) when the "isSome" property is present. If it's not, or if it's present and true, then the entry will be in the filtered result.

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

5 Comments

Thank you much, thats a great help, but I was getting null in the console for valuewithoutisSome. Is there a way we can use .some() in that filter()?
Well .filter() will never return null; are you sure your test code is really the same as what you posted? The code exactly as written in your question does not show null for valuewithoutisSome.
It was working in the codesnippet here, when I try to do the same thing in my visual studio code, I am seeing an empty array for valuewithoutisSome
Look carefully at your actual code, in particular for differences. There could be something that looks harmless but that is in fact a critical change.
Thanks,my mock data actually doesn't have moreDetails in other arrays except the first one. I think thats what is making a difference.
1

Your isSome property type is string. Remove double quetos from true.

{ "somevalues": {}, "moreDetails": { "isSome": true } },

2 Comments

This is a good suggestion, though the string "true" does work. However isSome: "false" would also be true (which is why it's a good suggestion) :)
I wanted it as boolean, I dont have double quotes for true. I somehow wrongly inserted in this mockdata, my apologies.

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.