3

Using JS or jQuery how to delete the key-value pair which the value is of type “Null” & "". e.g. before:

Object {style: "fruit", origin: "Thailand", day: "18d", color: "", weight: null}

alter:

Object {style: "fruit", origin: "Thailand", day: "18d"}
brk
  • 48,835
  • 10
  • 56
  • 78
duskdeep
  • 45
  • 2
  • 4
  • 1
    Possible duplicate of [How to remove a property from a JavaScript object?](http://stackoverflow.com/questions/208105/how-to-remove-a-property-from-a-javascript-object) – Tibrogargan Sep 04 '16 at 07:13

5 Answers5

1

There are two parts to this:

  1. Loop through the object's properties

  2. Remove a property from an object

There are lots of ways to do the first, covered by this question's answers. Assuming you only care about "own" (non-inherited) properties, I'd probably use Object.keys to get an array of property names and then loop that.

The second is done with the delete operator.

So:

Object.keys(theObject).forEach(function(key) {
    var value = theObject[key];
    if (value === "" || value === null) {
        delete theObject[key];
    }
});

Live Example:

var theObject = {
  style: "fruit",
  origin: "Thailand",
  day: "18d",
  color: "",
  weight: null
};
console.log("Before:", JSON.stringify(theObject, null, 2));
Object.keys(theObject).forEach(function(key) {
  var value = theObject[key];
  if (value === "" || value === null) {
    delete theObject[key];
  }
});
console.log("After:", JSON.stringify(theObject, null, 2));
Community
  • 1
  • 1
T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
1

You can use for..in to go through the loop to find which key has null or "".

Then use delete to delete the key

var myObj = {
  style: "fruit",
  origin: "Thailand",
  day: "18d",
  color: "",
  weight: null
}

for(var keys in myObj){
 if(myObj[keys] ===null || myObj[keys] === ""){
  delete myObj[keys]
 }
}
console.log(myObj)

JSFIDDLE

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

An easy solution by iterating threw the keys of your object and pushing properties matching in a resulting Array :

var input = {style: "fruit", origin: "Thailand", day: "18d", color: "", weight: null};
var keys = Object.keys(input);
var result = {};
keys.forEach(key => {if (input[key] != null && input[key] != "") result[key] = input[key]});
console.log(result); // { style: 'fruit', origin: 'Thailand', day: '18d' }
kevin ternet
  • 4,514
  • 2
  • 19
  • 27
-1
var yourObj={style: "fruit", origin: "Thailand", day: "18d", color: "", weight: null}

for(var attr in yourObj){
  if(!yourObj[attr]){
    delete yourObj[attr]
  }
}
khem poudel
  • 587
  • 7
  • 13
-1

It can be done using this code :

var map = {style: "fruit", origin: "Thailand", day: "18d", color: "", weight: null};

for (var i in map){
    if(map[i]==null || map[i]==""){
       delete(map[i]);
 }
}
ArtOfCode
  • 5,702
  • 5
  • 37
  • 56
  • 1
    This answer has been flagged as low quality. If it answers the question, consider adding a bit of text to explain how it works. – lmo Sep 04 '16 at 13:34