3

I'm trying to remove an object from Json Object it works..but it replace it with null..i dont know why, how can i remove the null value from the json..heres the 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 the object:

[{"name":"xxx","link":"www.ddd.com","id":0,"$$hashKey":"uiGrid-001Z"},null,null]

9
  • 1
    Show please matricule. Is it key or index? Commented Apr 20, 2017 at 11:14
  • 1
    Issuing delete will not delete anything, it will just undefine values. The length of the array will be unaffected Commented Apr 20, 2017 at 11:14
  • 2
    Try linkJsonObj.splice(linkJsonObj.indexOf(matricule), 1) Commented Apr 20, 2017 at 11:15
  • 1
    Unless matricule is the index. In that case use linkJsonObj.splice(matricule, 1) Commented Apr 20, 2017 at 11:17
  • 1
    @paperpaper You show an array of objects in your question. The null values are elements in the array, and an array does not have keys. Commented Apr 20, 2017 at 11:46

3 Answers 3

2

You can use filter(), x will be without null's.

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

Sign up to request clarification or add additional context in comments.

Comments

0

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

If you just want the nulls filtered 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 this loses the original reference to your object.

If you want to keep the reference, Use array.splice().

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

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

2 Comments

This is a comment, not an answer. Can you please at least support your statement with a simple example code?
Ok, will do, but the question is unclear. If paper-paper requires a new object or the same object reference
-1

delete will replaced 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));

2 Comments

Why is this downvoted, filter is the right way to filter out values from array.
@charlietfl Yes, you're right delete is not needed.

Your Answer

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