-2

No idea how to word this but say I'm trying to make an array of strings, and the existence of each string is dependant on a condition

What I'm doing right now is going

somelist = [
    true ? 'foo' : null,
    false ? 'bar' : null
]

Because I don't want to give any value to false conditions, but the only way I could figure out to do that was by giving them all null values.

I know that I could do an if statement for each and push it if so, but maybe there's a better way?

I just figured a kind of solution.

somelist = somelist.filter(value => value !== null)

but is there an even shorter way?

J Doe
  • 109
  • 2
  • 12
  • _via the constructor?_ You mean Array constructor? – hindmost Dec 20 '17 at 14:08
  • You gave a specific example of how your are is instaciated. Could you show us how you really do ? Because, this is probably where you could work to instanciate the array without the null items. – Grégory Bourgin Dec 20 '17 at 14:10
  • Please add a [minimal, complete, and verifiable example](https://stackoverflow.com/help/mcve/) to add some more informations. Right now, at least for me, it's not really clear what the actual problem is... – Andreas Dec 20 '17 at 14:10
  • 1
    You could use [push](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/push) methodlike this `true && somelist.push('foo');` . Here is an example https://jsfiddle.net/t8a1ck0y/ – Alexander Popov Dec 20 '17 at 14:15
  • 1
    `even shorter way` ... if you never have an empty string then `somelist = somelist.filter(value => value !== null)` can be `somelist = somelist.filter(Boolean)` - that's shorter – Jaromanda X Dec 20 '17 at 14:17
  • `somelist.filter(v => v)` – Ben Aston Dec 20 '17 at 14:27
  • Empty strings can be eliminated but 0's need to kept so `v => v || v==0` is what I went with. I'd say it's pretty clear what the problem is, you don't want an array with any nulls, `['value', 'other value', null, 'mvalue', null]` and there doesn't seem to be a way to return some result that tells the array not to even put that value in, I'm being very privileged but I just thought the feature might exist – J Doe Dec 21 '17 at 20:46

2 Answers2

0

If you control the data then you should use an if statement and push the data to the array conditionally. Does not make sense to loop through the array to filter out nulls that you added.

If you do not control the data there is this question on removing empty elements

If your dead set on using a constructor javascript let's you do fun things, like extend array

class NoNullArray extends Array{
     constructor(...elements){
         super(...elements.filter(element=>element !== null))
     }
}

var somelist = new NoNullArray(
true ? 'foo' : null,
false ? 'bar' : null
);
0

maybe a bit hacky, but you could do

//define condition-value pairs
let somelist = [
  true, 'foo',
  false, 'bar',
  //...
]
//filter the odd indices that are preceeded by a truthy value
.filter((v,i,a) => i&1 && a[i-1]);

that way, you can get rid of the ternary operations and writing null-values in the first place

Thomas
  • 11,958
  • 1
  • 14
  • 23
  • I think creating the array with the null values present and then just returning a filtered array that ignores null values is easier. However I do see the strength of your method if there are null values you would want to keep if they weren't put there by you – J Doe Dec 21 '17 at 20:42