如何递归删除包含空数组的嵌套对象?(How do you recursively remove nested objects that contain an empty array?)

编程入门 行业动态 更新时间:2024-10-15 16:22:42
如何递归删除包含空数组的嵌套对象?(How do you recursively remove nested objects that contain an empty array?)

我最初收到{"B":{"1":"100","3":{"AA":256}},"A":100}的AJAX响应{"B":{"1":"100","3":{"AA":256}},"A":100}并转换为javascript对象:

var jsonOBJ = {}; jsonOBJ = jQuery.parseJSON(data);

未来的响应可以是初始响应的子集或超集。 如果服务器上的表值未更改,则停滞数据将替换为空数组。 例:

{"B":{"1":"90","2":200,"3":[]}}

{"B":[],"A":20}

每次收到AJAX响应时,对象都会更新为:

jQuery.extend(true, jsonOBJ, jQuery.parseJSON(data));

但是我需要javascript对象来保持未更改的部分,所以我需要最终得到一个与上面的示例响应相同的对象:

jsonOBJ = jQuery.parseJSON('{"B":{"1":"90","2":200,"3":{"AA":256}},"A":20}');

我首选的选项是从转换​​后的响应中删除空对象。 是否有一个现有的函数或对jQuery扩展函数的修改可以做到这一点?

I initially receive an AJAX response of {"B":{"1":"100","3":{"AA":256}},"A":100} and converted to a javascript object:

var jsonOBJ = {}; jsonOBJ = jQuery.parseJSON(data);

Future responses can be subsets or supersets of the initial response. If the value of a table is unchanged at the server, the stagnant data is replaced with an empty array. Example:

{"B":{"1":"90","2":200,"3":[]}}

{"B":[],"A":20}

Everytime an AJAX response is received, the object is updated with:

jQuery.extend(true, jsonOBJ, jQuery.parseJSON(data));

But I need the javascript object to keep the unchanged portions, so I need to end up with an object that would be equivalent to the following with the example responses above:

jsonOBJ = jQuery.parseJSON('{"B":{"1":"90","2":200,"3":{"AA":256}},"A":20}');

My preferred option would be to remove the empty objects from the converted response. Is there an existing function or a modification to the jQuery extend function that would do this?

最满意答案

您可以使用此代码删除响应中使用空数组的元素。

它循环通过顶层,寻找任何空数组并删除它们。 它找到的任何对象,它都会递归到同时删除它们中的空数组:

// make sure the ES5 Array.isArray() method exists if(!Array.isArray) { Array.isArray = function (arg) { return Object.prototype.toString.call(arg) == '[object Array]'; }; } function removeEmptyArrays(data) { for (var key in data) { var item = data[key]; // see if this item is an array if (Array.isArray(item)) { // see if the array is empty if (item.length == 0) { // remove this item from the parent object delete data[key]; } // if this item is an object, then recurse into it // to remove empty arrays in it too } else if (typeof item == "object") { removeEmptyArrays(item); } } } var jsonOBJ = {}; jsonOBJ = jQuery.parseJSON(data); removeEmptyArrays(jsonOBJ);

你可以在这里看到它的工作: http : //jsfiddle.net/jfriend00/U6qMH/

You can remove the elements in your response with empty arrays with this code.

It cycles through the top level, looking for any empty arrays and removing them. Any objects it finds, it recurses into to also remove empty arrays in them:

// make sure the ES5 Array.isArray() method exists if(!Array.isArray) { Array.isArray = function (arg) { return Object.prototype.toString.call(arg) == '[object Array]'; }; } function removeEmptyArrays(data) { for (var key in data) { var item = data[key]; // see if this item is an array if (Array.isArray(item)) { // see if the array is empty if (item.length == 0) { // remove this item from the parent object delete data[key]; } // if this item is an object, then recurse into it // to remove empty arrays in it too } else if (typeof item == "object") { removeEmptyArrays(item); } } } var jsonOBJ = {}; jsonOBJ = jQuery.parseJSON(data); removeEmptyArrays(jsonOBJ);

You can see it work here: http://jsfiddle.net/jfriend00/U6qMH/

更多推荐

本文发布于:2023-08-02 04:29:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1368781.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:递归   嵌套   数组   对象   recursively

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!