0

If I have an array built like so:

body: [
  [ 'A', 'A value' ],
  [ 'B', 'B value, other' ],
  [ 'C', 'C value' ]
]

How can wrap an if statement around the C so that the row is only added if the condition returns true? Something like this (pseudocode):

body: [
  [ 'A', 'A value' ],
  [ 'B', 'B value, other' ],
  if (x == true) {
    [ 'C', 'C value' ]
  }
]
t56k
  • 6,769
  • 9
  • 52
  • 115

4 Answers4

4

Just push it.

if (x) {
    yourReference.body.push([ 'C', 'C value' ]);
}
Daniel A. White
  • 187,200
  • 47
  • 362
  • 445
  • 2
    [push it real good](https://www.youtube.com/watch?v=vCadcBR95oU)! – Mottie Aug 17 '16 at 00:48
  • This is what I thought, but is there something more "inline"? The object I'm dealing with is big, and there are a lot of body elements. – t56k Aug 17 '16 at 00:55
  • 1
    @CD-RUM there is no *easy and robust* way to do that directly in the array literal. – zerkms Aug 17 '16 at 00:55
  • @CD-RUM nothing in pure javascript. if this was coming from a templating engine, then maybe – Daniel A. White Aug 17 '16 at 00:56
  • @CD-RUM: You could add a filter afterwards, but I’d advise against it: `[…, x ? ['C', 'C value'] : null, …].filter(Boolean)` (where you can check for `=== null` if falsy values are valid, and use `undefined` if `null` is valid, and use a new object if both are valid). – Ry- Aug 17 '16 at 00:57
  • After the array has been created, you can use `splice` to insert an element at a specific index - see http://stackoverflow.com/questions/586182/how-to-insert-an-item-into-an-array-at-a-specific-index – Mottie Aug 17 '16 at 00:58
2

If you want to avoid pushing the item after the array initilizer, you may prefer

body: [
  [ 'A', 'A value' ],
  [ 'B', 'B value, other' ],
  x == true && [ 'C', 'C value' ]
].filter(Boolean)

That is, let the third array item be [ 'C', 'C value' ] only if x == true is true. If it's false, let the third array item be false.

And then filter out all falsy values: null, undefined, false, +0, -0, NaN and "". So only use this approach if your array doesn't contain any of these.

Oriol
  • 274,082
  • 63
  • 437
  • 513
1

You could also make a builder:

function ArrayBuilder() {
    this.array = [];
}

ArrayBuilder.prototype.push = function (value) {
    this.array.push(value);
    return this;
};

ArrayBuilder.prototype.pushIf = function (condition, value) {
    if (condition) {
        this.array.push(value);
    }

    return this;
};

although this means your value is always evaluated, regardless of the condition.

body: new ArrayBuilder()
    .push([ 'A', 'A value' ])
    .push([ 'B', 'B value, other' ])
    .pushIf(x, [ 'C', 'C value' ])
    .array,
Ry-
  • 218,210
  • 55
  • 464
  • 476
0

This will loop through all the elements in the body and condition each.

const body = [

  [ 'A', 'A value' ],
  [ 'B', 'B value, other' ],
  [ 'C', 'C value' ]

];

const newArray = body.map(subArray => {

  if (condition) return subArray;  

});
Giovanni Lobitos
  • 852
  • 7
  • 19