[英]How can I remove null from JSON object with AngularJS?
我正在嘗試從Json對象中刪除一個對象它工作..但它用null替換它..我不知道為什么,我怎樣才能從json中移除null值...函數:
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));
};
繼承人的對象:
[{ “名稱”: “XXX”, “鏈接”: “www.ddd.com”, “ID”:0, “$$ hashKey”: “uiGrid-001Z”},NULL,NULL]
你可以使用filter()
, x
將沒有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;
}
有多種方法可以從JavaScript中的對象數組中刪除對象。 你不需要AngularJS,你可以使用VanillaJS。
如果您只想過濾掉空值,可以使用
var yourArray =[{"name":"xxx","link":"www.ddd.com","id":0,"$$hashKey":"uiGrid-001Z"},null,null];
yourArray = yourArray.filter(function(elt){
return elt != null;
});
但這會丟失對您對象的原始引用。
如果要保留引用,請使用array.splice()。
yourArray.forEach(function(){
yourArray.splice(yourArray.indexOf(null),1);
});
現在你將在yourArray中使用null less數組。 這實際上是在不更改引用的情況下從數組中刪除對象,
delete
將使用undefined
替換該對象
您可以使用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));
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.