5

Assume we have function that returns array let's say fn(parm1, parm2, parm3) and i want to conditional reverse the returned array.

I know you can do something like this:

condition ? fn(parm1, parm2, parm3).reverse() : fn(parm1, parm2, parm3)

But in my case I don't want to call it twice. Not the array or even the function that calls the array.

So my question, is there something obvious to do and I've missed like

array[condition  && .reverse()]

or the first way is the only way to call array methods.

Jalal
  • 3,308
  • 4
  • 35
  • 43

5 Answers5

7

You could use Array#slice as default method.

result = fn(parm1, parm2, parm3)[condition ? 'reverse' : 'slice']();

Instead of slice and to prevent to get a copy of the array, you could implement a function which turns just this from the object without mutating it.

Array.prototype.nop = function () { return this; };

result = fn(parm1, parm2, parm3)[condition ? 'reverse' : 'nop']();
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
  • Why create a new array? – guest271314 Nov 04 '17 at 20:40
  • 1
    @JimmyJanson If the requirement is to not write the code twice why create the array twice? – guest271314 Nov 04 '17 at 20:44
  • @guest271314 I don't see where it is created twice...what do you mean? – Michelangelo Nov 04 '17 at 20:48
  • @guest271314 How i create it twice? dont get it. – Jalal Nov 04 '17 at 20:49
  • @JimmyJanson [`Array.prototype.slice()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/slice) returns a new array – guest271314 Nov 04 '17 at 20:49
  • I use it without slice, like this: result = fn(parm1, parm2, parm3)[condition && 'reverse' ](); – Jalal Nov 04 '17 at 20:52
  • @JimmyJanson How does `result = fn(parm1, parm2, parm3)[condition && 'reverse']()` not throw an error if `condition` is `false`? – guest271314 Nov 04 '17 at 20:55
  • @JimmyJanson That would fail if condition is false – Michelangelo Nov 04 '17 at 20:56
  • @guest271314 Yeah, it is less efficient but I don't think it is noticeable with small datasets ;) – Michelangelo Nov 04 '17 at 20:57
  • @Michelangelo See the requirement described at original Question _"But in my case I don't want to call it twice. Not the array or even the function that calls the array. So my question, is there something obvious to do and I've missed like `array[condition && .reverse()]`"_ at comment _"i meant written twice of course"_ https://stackoverflow.com/questions/47115374/best-way-to-do-conditional-array-methods-in-javascript/47115417#comment81180627_47115374. If the requirement is to not do something twice, why do something twice? Though OP is free to accept or not to accept any Answer. – guest271314 Nov 04 '17 at 21:00
  • @Michelangelo Though OP is free to accept or not to accept whichever Answer they decide to. – guest271314 Nov 04 '17 at 21:02
  • @guest271314 you are completely right it is not the right answer because it creates the array twice – Jalal Nov 04 '17 at 21:03
  • @guest271314 Yeah, was not really clear to me what OP wanted. Thought he wanted to get rid of duplicate code or something like that. – Michelangelo Nov 04 '17 at 21:03
  • @NinaScolz `.sort()` does not create a new array `result = fn(parm1, parm2, parm3)[condition ? "reverse" : "sort"]()` – guest271314 Nov 04 '17 at 23:03
2
var arr = fn(parm1, parm2, parm3);
condition && arr.reverse();
guest271314
  • 1
  • 15
  • 104
  • 177
  • @NinaScholz `reverse` reverses the array in place, so the assignment isn't needed – 4castle Nov 04 '17 at 20:41
  • @NinaScholz _"you need an assignment with `reverse`"_ Not sure what you mean? The code at Answer should meet requirement – guest271314 Nov 04 '17 at 20:41
  • This is the better answer, because it's more efficient (and less tricky). – 4castle Nov 04 '17 at 20:46
  • How this is different from: var arr = fn(parm1, parm2, parm3); condition ? arr.reverse() : arr – Jalal Nov 04 '17 at 21:19
  • @JimmyJanson What is the requirement? The original Question states that the object is to not write code twice, yet the code at your comment writes code twice – guest271314 Nov 04 '17 at 21:20
  • won't work for me can't declare variable arr inside the array where i try to execute it. fn is element inside another array. – Jalal Nov 04 '17 at 21:22
  • @JimmyJanson _"won't work for me can't declare variable arr inside the array where i try to execute it. fn is element inside another array."_ How is that related to the requirement described at original Question? What do you mean by "inside the array"? – guest271314 Nov 04 '17 at 21:23
  • @ guest271314 like this [1, 2, 3, functionThatReturnsArray, 3] – Jalal Nov 04 '17 at 21:24
  • Can you post actual code at Question? See https://stackoverflow.com/help/how-to-ask, https://stackoverflow.com/help/mcve – guest271314 Nov 04 '17 at 21:25
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/158235/discussion-between-jimmy-janson-and-guest271314). – Jalal Nov 04 '17 at 21:25
1

I had an array I wanted to sort asc and desc. The sort boolean and ternary operators led me to this. Basically, if false then multiply by -1. It could probably even be simplified beyond this.

data.items
.sort((a, b) =>
    new Date(a.date) > new Date(b.date)
    ? -1 * (sort ? 1 : -1)
    : 1 * (sort ? 1 : -1)
)
mateoc15
  • 509
  • 5
  • 18
1

What about using sort? I am not sure if browsers and node.js have the same behaviour here though:

const arr = fn(parm1, parm2, parm3).sort(()=> INVERTED ? 1 : 0);
Julian Dm
  • 363
  • 2
  • 17
0

Just keep it easy to read and understand:

var arr = fn(parm1, parm2, parm3);
if (condition) {
    arr.reverse();
}

Tricky solutions won't be appreciated by people who will read your code in the future.

Zoli Szabó
  • 4,366
  • 1
  • 13
  • 19
  • 1
    How this is different form @guest271314 answer? – Jalal Nov 04 '17 at 21:09
  • As I wrote above, using the `if` keyword makes code easier to understand. This is, of course, a personal opinion, but I believe the majority agrees with it. It has been discussed earlier, read about it here: https://stackoverflow.com/q/34704750/3219919 – Zoli Szabó Nov 04 '17 at 21:54