0

I have an array of objects, there is one field in each object could be boolean or a string of array, for instance

myObjects=[{id: "fruit", selected: true}, {id: "veggie", selected: ["tomato", "cucumber", "potato"]}, {id: "diary", selected:[]}]

I would like to filter out the objects that have selected either not empty or true. The result would be:

result=[{id: "fruit", selected: true}, {id: "veggie", selected: ["tomato", "cucumber", "potato"]}]

Here is my code:

for (const object in myObjects) {
if (!object[selected] || (Array.isArray(object[selected] && object[selected].length ===0)) continue
...
}

But objects with selected is empty array won't get filter out. I got a typescript error

Property 'length' does not exist on type 'boolean | string[]'.
  Property 'length' does not exist on type 'false'.

Can anyone help?

5
  • You use a for...in loop on an array of objects? Should this not be for...of loop? Commented Aug 12, 2022 at 18:01
  • You could use a .filter() function. Something like myObjects.filter(obj => (typeof obj.selected === 'boolean' && obj.selected) || obj.selected.length > 0) to filter out any boolean false or empty arrays Commented Aug 12, 2022 at 18:03
  • 2
    There is a bad condition grouping, must be if (!object[selected] || Array.isArray(object[selected]) && object[selected].length === 0) continue Commented Aug 12, 2022 at 18:06
  • From what I tested, this should be a SyntaxError (in the if there's 3 opening parentheses and only 2 closing). So maybe there's something missing from the code posted here, compared to what you actually ran. It could be because instead of "continue;", you used a function or something else that is acceptable inside if (edit: I think it would only work with an operator actually). If you could clarify that, it'll probably be helpful. But yeah, what @n-- said matches with the behaviour you presented, it looks like the length part is inside the isArray function. Commented Aug 12, 2022 at 18:20
  • 1
    If you're going to use bracket notation, you need to use object["selected"] otherwise it's looking for a variable called selected. Commented Aug 12, 2022 at 20:32

2 Answers 2

3

You can use the filter method on the myObjects array

const res = myObjects.filter((object) => object.selected.length > 0 || object.selected === true )

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

2 Comments

The specific error he's seeing is because of typescript, so something like: myObjects.filter(obj => Array.isArray(obj.selected) ? obj.selected.length > 0 : obj.selected === true) is required
Agreed, was just pointing out that this answer doesn't solve the issue Property 'length' does not exist on type 'boolean | string[]'.
0

You could do it with loop for...of

add object to result array only if selected property is boolean and true:

(typeof object.selected === "boolean" && object.selected)

or selected property is an array and have elements:

(Array.isArray(object["selected"]) && object["selected"].length !== 0)

const myObjects = [{
    id: "fruit",
    selected: true,
  },
  {
    id: "veggie",
    selected: ["tomato", "cucumber", "potato"],
  },
  {
    id: "diary",
    selected: [],
  },
];
let result = [];
//                vv replace 'in' with 'of' since you loop over an array not an object
for (const object of myObjects) {
  if (
    (typeof object.selected === "boolean" && object.selected) ||
    (Array.isArray(object["selected"]) && object["selected"].length !== 0)
  )
    result.push(object);
}
console.log(result);

Comments

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.