0

I got a code block like this

const numbers = [1, 2, 3, 4, 5, 6];

const newNumbers = numbers.reduce((acc, num) => {
  acc.push(num > 3 ? num : null);
  return acc;
}, []);
console.log('new Numbers', newNumbers);
//returns new Numbers,[null, null, null, 4, 5, 6]

But I don't want null values to be pushed in array. I want to perform the action like this but without if:

const newNumbers = numbers.reduce((acc, num) => {
  if (num > 3) {
    acc.push(num);
  }  
  return acc;
}, []);
console.log(newNumbers);
//returns new Numbers,[4, 5, 6]
5
  • 2
    Why don't you want to use an if? You could use && but it's not very readable if you do that imo. Commented Sep 23, 2022 at 11:16
  • You can't: the ? : operator forms an expression, not a statement, and in JS (and most languages) all expressions have to evaluate to some value (unless you want to ruin everything with throw, I guess...) Commented Sep 23, 2022 at 11:20
  • @NickParsons I tried it but it still pushes unwanted items in new array :/ Commented Sep 23, 2022 at 11:20
  • Why are you using reduce instead of filter and map? Commented Sep 23, 2022 at 11:20
  • 2
    @Shifty You should use .filter() (as suggested in the below answer). The && solution doesn't sit inside of the .push(), it would be <condition> && arr.push(...), but it's not recommended to write code like that as you're writing an expression that isn't being used for it's result but rather just its side effects (more details). It still isn't clear why you want to remove the if though. While && works you should avoid it for side-effect code and instead favour an if statement Commented Sep 23, 2022 at 11:23

3 Answers 3

6

How Can I Use Ternary Conditional Operator Without Else?

No. The conditional operator always has to "return" a value. Imagine it was possible to leave out the alternative (e.g. arr.push(num > 3 ? num)), what value would be pushed to the array?

In this specific case there is a better tool for the job: Array#filter:

const newNumbers = numbers.filter(num => num > 3)
Sign up to request clarification or add additional context in comments.

2 Comments

Yeap filter() works fine but I wondered if its possible to make it with reduce() too
Of course it is, you even included one possible solution in your question. But only because you can do something doesn't mean you should.
0

Use && instead of ?,

const numbers = [1, 2, 3, 4, 5, 6];

const newNumbers = numbers.reduce((acc, num) => {
  num > 3 && acc.push(num)
  return acc;
}, []);
console.log(newNumbers);

//returns new Numbers,[4, 5, 6]

1 Comment

yes this one works thank you.
0

You can always do:

num > 3 ? acc.push(num) : {}

2 Comments

yes I tried but it adds empty objects too :)
@Shifty No. You must have tried acc.push(num > 3 ? num : {}), which is different from num > 3 ? acc.push(num) : {}

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.