How to remove null from JSON object using AngularJS?

I am trying to remove an object from Json Object but it replaces it with null. I don't know why, how can I remove the null value from the json..heres function:

company.deleteExternalLinkFromGrid = function (row, matricule) {
        // console.log('Inside of deleteModal, code = ' + code);
        //$scope.sitting= {};
       console.log(matricule);
        //console.log(JSON.stringify(linkJsonObj));
        delete linkJsonObj[matricule];

        console.log(JSON.stringify(linkJsonObj));
    };

      

heres an object:

[{"Name": "xxx", "link": "www.ddd.com", "identifier": 0, "$$ hashKey": "uiGrid-001Z"}, NULL, NULL]

+3


source to share


3 answers


You can use filter()

, x

will be null.

function test()
{
    var x =[{"name":"xxx","link":"www.ddd.com","id":0,"$$hashKey":"uiGrid-001Z"},null,null].filter(isNotNull);
    alert(JSON.stringify(x));

}

function isNotNull(value) {
  return value != null;
}

      



fiddle

+2


source


There are several ways to remove an object from an array of objects in JavaScript. You don't need AngularJS for this, you can use VanillaJS.

If you just want to filter nulls you can use

var yourArray =[{"name":"xxx","link":"www.ddd.com","id":0,"$$hashKey":"uiGrid-001Z"},null,null];
     yourArray = yourArray.filter(function(elt){
     return elt != null;
    });

      

But that loses the original reference to your object.



If you want to keep the link, use array.splice ().

  yourArray.forEach(function(){ 
      yourArray.splice(yourArray.indexOf(null),1);
  });   

      

now you will have null less array in yourArray. This actually removes the object from the array without changing the reference,

0


source


delete

will replace the object with undefined

You can filter the array to remove them using Array#filter()

var array = [{
  "name": "xxx",
  "link": "www.ddd.com",
  "id": 0,
  "$$hashKey": "uiGid-001Z"
}, {
  "name": "xx",
  "link": "www.dddcom",
  "id": 1,
  "$$hashey": "uiGrid-0029"
}, {
  "name": "xxx",
  "link": "www.ddd.com",
  "id": 2
}];

delete array[1];

array = array.filter(a=>a);

console.log(JSON.stringify(array));
      

Run codeHide result


-1


source







All Articles