Javascript对象删除特定键
问题描述:
我遇到了此对象数组的问题。我需要删除与removeVal相匹配的对象。我不会删除它。请参阅以红色突出显示的附加照片。我需要删除这一切。Javascript对象删除特定键
var removeVal = $(this).attr('href');
$.each(product_json, function(key, val){
if(key == removeVal) {
val.splice(key);
}
});
答
也许你的意思是像
$.each(product_json, function(key, val){
if(val == removeVal) {
product_json.splice(key, 1);
}
});
但是,由于splice
重新索引,删除一个项目时,下一个会不会被检查,但可能还需要删除。
正确的方法是使用filter
:
product_json = product_json.filter(function(val) {
return val != removeVal;
});
+0
我需要删除突出显示的红色。我不在乎它是否重新索引。它仍然不起作用。 – Rbex
答
我觉得product_json看起来像您展示图像。
然后,
不应该像下面的代码?
var removeVal = $(this).attr('href');
$.each(product_json, function(key, val){
// val is an array
for(var i in val) {
// val[i] is also an array
for(var j in val[i]) {
// val[i][j] is a value
if(val[i][j] == removeVal) {
val.splice(key);
}
}
}
});
更具体地讲,
我需要你解释product_json是什么
和$(这)样.attr( 'href' 属性)
,为什么那些该图像中的数组看起来像那样。
+0
我已经更新了这个问题。 – Rbex
确保removeVal和关键的数据类型应该相同 – Jigar7521
我们展示product_json数据 – Mahi
您可以用'filter'功能。 – uzaif