[英]Remove specific value from multidimensional array
I have a multidimensional array with an unknown amount of nesting. 我有一个未知数量的嵌套多维数组。 I just need to remove <span class='highlight'>
and </span>
from it. 我只需要从中删除<span class='highlight'>
和</span>
。
How do I do that? 我怎么做?
My idea was, to create a JSON string and replace the specific value with nothing, but somehow it doesn't work. 我的想法是,创建一个JSON字符串,然后不使用任何内容替换特定值,但是以某种方式不起作用。 Note: the specific value can exists more than once. 注意:特定值可以存在多次。
Here is my code for it: 这是我的代码:
function removeHighlightFromData(aData){
var jsonData = JSON.stringify(aData)
jsonData = jsonData.replace("<span class='highlight'>", "");
jsonData = jsonData.replace('<span class="highlight">', "");
jsonData = jsonData.replace("</span>", "");
return jQuery.parseJSON(jsonData);
} }
Any idea what is wrong, or any other approaches? 知道有什么问题吗,还是有其他方法?
Do you know if your json has multiple occurrences of string you want to replace ? 你知道你的json是否有多个要替换的字符串吗? It might be that the first occurrence only is getting replaced because you are not using global switch. 可能是因为您没有使用全局开关,所以只替换了第一个匹配项。 Try : 尝试:
jsonData = jsonData.replace(\<span class=\'highlight\'>/g, "");
& 和
jsonData = jsonData.replace(\/g, "");
to replace all occurrences. 替换所有事件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.