0

I have inserted objects with a new property(property name - "Type") to a JS array. After that, I need to filter objects with that property(which is "Type") to a new array.

var arr = [
{ID: 1, Name: "Name1"},
{ID: 2, Name: "Name2"}]

var newObject = {
ID: 3,
Name: 'Test',
Type: 'New'
};

arr.push(newObject);

I tried

var newVal = arr.filter(x => x.Type !== null);

it returns all the object from array

result should be

[{ ID: 3, Name: "Test", Type: "New" }]
uduwaras
  • 3
  • 3
  • 1
    there is no object with `Type === null` in your array. But `Type === undefined` for both objects, ie `null !== undefined` – derpirscher Sep 23 '21 at 12:29
  • Does this answer your question? [What is the difference between null and undefined in JavaScript?](https://stackoverflow.com/questions/5076944/what-is-the-difference-between-null-and-undefined-in-javascript) – derpirscher Sep 23 '21 at 12:33

3 Answers3

1

Don't check for null. Use undefined instead:

var newVal = arr.filter(x => x.Type !== undefined);

Check this definition of null and undefined:

Null: It is the intentional absence of the value. It is one of the primitive values of JavaScript. Undefined: It means the value does not exist in the compiler. It is the global object.

diiN__________
  • 7,393
  • 6
  • 42
  • 69
0

Because it is undefined instead of null

The code below will get any type EXCEPT falsy types like "Type" : 0 and "Type": ""

let arr = [
{ID: 1, Name: "Name1"},
{ID: 2, Name: "Name2"}]

var newObject = {
ID: 3,
Name: 'Test',
Type: 'New'
};

arr.push(newObject);

const newVal = arr.filter(x => { 
  console.log(x.Type) 
  return x.Type; // not undefined, not null, not empty string.
  });
  
console.log(newVal)
mplungjan
  • 169,008
  • 28
  • 173
  • 236
0

In newer versions of Javascript (ES 2021 I think), you can use nullish coalescing:

var arr = [
{ID: 1, Name: "Name1"},
{ID: 2, Name: "Name2"}]

var newObject = {
ID: 3,
Name: 'Test',
Type: 'New'
};

arr.push(newObject);

var newVal = arr.filter(x => x.Type ?? false);
console.log(newVal)

Nullish coalescing is good for when a variable is defined, but it is technically false. With a ?? b, either a or b will be returned. If a is undefined, null, then b will be returned. Otherwise, a will be returned. You can read more about it on MDN.

Rojo
  • 2,749
  • 1
  • 13
  • 34
  • *"" and 0 will return true with nullish coalescing.* That's wrong. The null coalescing operator `??` won't return `true` or `false`, it will return whatever the first or second operand in `aa ?? bb` is. If `aa` is `null` or `undefined` it will return `bb` else it will return `aa`. In this particular case, the `??` is probably not needed at all (depending on the semantics OP wants), because the result will be exactly the same as with `arr.filter(x => x.Type)` – derpirscher Sep 23 '21 at 18:58
  • @derpirscher right, I forgot that `||` and `??` return either a or b. I wish `??` returned true though; I feel like that would make things easier. I'll edit my question, thanks! – Rojo Sep 24 '21 at 12:40