1

I have the following javascript array:

var foo = [false,false,true,false,true];

What I want is to remove 'true' values and keep false like this:

[false,false,false]

I have tried this:

console.log(foo.map(function(i){if(i==false)return i;}));

but what i get is :

[ false, false, undefined, false, undefined ]

Any ideas how to accomplish this?

John Kananakis
  • 133
  • 1
  • 9
  • Try [Array.filter](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter) – Jonathan Kuhn Oct 20 '16 at 17:15
  • 1
    Surprisingly hard to find a dupe target for this (I'm **sure** there is one), probably because it's so basic. Most questions I can find are about arrays of objects and such. – T.J. Crowder Oct 20 '16 at 17:17
  • The only difference between this and the linked dupe target (which was not trivial to find) is the condition for filtering. – T.J. Crowder Oct 20 '16 at 17:23

4 Answers4

1
var foo = [false,false,true,false,true];
var filteredAry = foo.filter(function(e) { return e !== true })

console.log(filteredAry)
Thalaivar
  • 23,282
  • 5
  • 60
  • 71
1

You want filter, not map.

var foo = [false,false,true,false,true];
console.log(foo.filter(function(i){ return i !== true; }));

That does what your question asks: Specifically filters out true values. Obviously, you can adjust the filter condition as necessary.

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
0

Use filter property

var foo = [false, false, true, false, true];
var newArray = foo.filter(function(item) {
  return item === false;
});
console.log(newArray)

DEMO

brk
  • 48,835
  • 10
  • 56
  • 78
0

Here's a solution with ramdajs

var foo = [false,false,true,false,true, undefined, 0];
var isFalsy = R.filter(R.not);
isFalsy(foo);

//result: [false, false, false, undefined, 0]
Dan Beaulieu
  • 400
  • 1
  • 11