Node.js lodash compact() remove falsey value from array

Description

Node.js lodash compact() remove falsey value from array


var _ = require('lodash');

// get an array with any non truthy values removed
var values = [0, null, 1, false, undefined, 2, 3, 4];

var truthy = _.compact(values);
console.log(truthy);//from   w ww  .j  a  va2 s.c om

var _ = {};

_.compact = function(arr) {
  return arr.filter(function(el) {
    return Boolean(el);
  });
};

console.log(lodash.compact([0, 1, false, 2, '', 3]));
console.log(_.compact([0, 1, false, 2, '', 3]));



PreviousNext

Related