6

Simple I have an object that looks like this which is returned directly from a stored procedure in my chrome browser. How can I remove the ones that say null in javascript/angular 2?

Object

Grinish Nepal
3,0633 gold badges35 silver badges51 bronze badges
asked Sep 22, 2017 at 14:59
1
  • Can you please, check my answer and share the format of the array you want to filter. Commented Sep 22, 2017 at 15:49

4 Answers 4

9

null seems to be the only falsy values, so you could do just

arr = arr.filter(Boolean);

If it's just an object with keys, you could do

var obj = {c1 : 's', c2 : 's', c3 : null, c4 : null};
Object.entries(obj).forEach( o => (o[1] === null ? delete obj[o[0]] : 0));
console.log(obj);

answered Sep 22, 2017 at 15:03
Sign up to request clarification or add additional context in comments.

3 Comments

Why did you use Boolean instead of el => el?
@HermanLauenstein I've never seen this either, but I suppose Boolean is there to cast whatever it gets into a boolean - truthy/falsy values. Since constructor is a function, you can pass it as an argument to filter.
Indeed, passing in Boolean will return the result of using the Boolean constructor on the values, and any falsy values, including null in this case, is filtered out.
6

The image that you attach in your question is for an object not an array

Read this question answers to know the difference between them.


To remove all null values from an array:

You can use Array#filter method

Ex:

const arr = [1, "a", null, "name", null, 100];
const newArr = arr.filter( e => e !== null);
console.log(newArr); // [1, "a", "name", 100]


To remove all properties with a nullvalue from an object:

You can use Array#filter , Array#reduce and Object#keys methods

Ex:

const obj = {a: 1, b: "name", c: null, d: 100, e: null};
const newObj = Object.keys(obj)
 .filter(e => obj[e] !== null)
 .reduce( (o, e) => {
 o[e] = obj[e]
 return o;
 }, {});
 
console.log(newObj); // {"a": 1,"b": "name", "d": 100 }


To remove all properties from an object inside array of objects:

You can combine all of the previous methods in addition to Array#map method.

Ex:

const arrOfObjects = [
 {a: null, b: "name", c: null, d: 100, e: "name"},
 {f: 1, g: null, h: 23, i: null, j: null},
 {k: null, l: "name", m: null, n: 100, o: "name"}
]
const newArrOfObjects = arrOfObjects
 .map(
 obj => 
 Object.keys(obj).filter(e => obj[e] !== null)
 .reduce((o, e) => {o[e] = obj[e]; return o;}, {})
 )
console.log(newArrOfObjects)

Grinish Nepal
3,0633 gold badges35 silver badges51 bronze badges
answered Sep 22, 2017 at 15:22

Comments

6

Use array filter:

var filtered = arr.filter(el => el !== null);

And even simpler (but this will filter other falsy values as well):

var filtered = arr.filter(el => el);
adriaan
1,1381 gold badge13 silver badges30 bronze badges
answered Sep 22, 2017 at 15:00

2 Comments

This doesn't seem to work I am using let filtered :any[] = dataObj.Errors[0].LineList.filter(el => el !== null); It isn't removing those values that are null. Its still the same exact list. Should I do a splice?
You should note that there is a difference between the 2 methods in some cases. what i want to say is that the 2 lines is not the same (in some other cases). so. you need to take care when using them.
3

First of all you don't have an array according to your provided image. It's an object and everyone thought you have an array due to your misguided title. So you can not use filter function right away. You can do something like this to filter the object.

var object = {
 C1: "123456",
 C2: "1234",
 C3: null,
 C4: null,
 C5: null,
 C6: "4567"
}
var MAP = {
 C1: "Product",
 C2: "Product Description",
 C3: "Date",
 C4: "",
 C5: "",
 C6: "Date"
}
for (var key in object) {
 if (object[key] === null) {
 delete object[key];
 } else {
 object[MAP[key]] = object[key];
 delete object[key];
 }
}
console.log(object);

UPDATE

I updated your answer to cater extra needs. Keep in mind that the MAP object should have a one to one mapping with key and value.

answered Sep 22, 2017 at 15:14

2 Comments

One more question. Lets say I wanted to give these C1, C2 etc a title of Product, Product Description, Date. How can I do that if I have a list of 5 objects exactly like this
This is awesome Thanks!

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.