从下面的代码我尝试从数组中删除数据。不幸的是它没有从我的数组中删除数据,ary
,看看你是否可以帮我解决这个问题
var ary = [{"Force":"Force Converter"},{"Power":"Power Converter"}];
function removeFav(n,v){
var index = ary.indexOf(n);
alert(index);
if (index > -1) {
ary.splice(index, 1);
}
alert("New updated Array:"+ary);
}
要求删除
removeFav("Power","Power Converter"); // this calls the above method
答案 0 :(得分:1)
注意:对indexOf的浏览器支持是有限的,IE7-8不支持。
以下是演示Demo,您可以在下面找到该代码。
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
<script src="//cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script>
localStorage.clear();
var ary = [];
ary.push({ "Force": "Force Converter" })
ary.push({ "Power": "Power Converter" });
function contains(arr, k) {
var doesContain = false;
for (var i = 0, length = arr.length; i < length; i++) {
var item = arr[i];
if (item.hasOwnProperty(k)) {
doesContain = true;
break;
}
}
return doesContain;
}
if (localStorage.getItem("testObject") != null) {
alert("localstorage has item testobject");
//Parse the retrieved value into an array
ary = JSON.parse(localStorage.getItem('testObject'));
}
function setFavorite(k, v) {
var obj = {};
obj[k] = v;
var hasItem = contains(ary, k);
if (!hasItem) {
ary.push(obj);
// Put the object into storage
localStorage.setItem('testObject', JSON.stringify(ary));
}
// Retrieve the object from storage
var retrievedObject = localStorage.getItem('testObject');
// Parse the retrieved value into an array
var retrievedArray = JSON.parse(retrievedObject);
// clear the ouput first
$("#fab-id").html("");
//iterate over the array which contains key-value pairs
for (var i = 0; i < retrievedArray.length; i++) {
var item = retrievedArray[i];
var key = Object.keys(item)[0];
var value = item[key];
//----------BELOW CODE DOES NOT WORK PROPERLY----
$("#fab-id").append('<a>' + value + '</a><br/>');
}
}
</script>
</head>
<body>
<button id="btn">Click me!</button>
<button id="btn2">Add another item!</button>
<div id="fab-id"></div>
<script>
//adding a simple click event handler
document.getElementById("btn").addEventListener("click", function () {
//setFavorite("Power", "Power Converter");
//setFavorite("Power", "Power Converter");
setFavorite("propA", "X");
});
document.getElementById("btn2").addEventListener("click", function () {
//setFavorite("Power", "Power Converter");
//setFavorite("Power", "Power Converter");
setFavorite("xyz", "blabal");
});
</script>
</body>
</html>
答案 1 :(得分:1)
您的包含功能非常完美。 这应该适合您,只需将名称直接用作属性
function setFavorite(name, value){
alert(contains(ary, value));
var obj = {};
obj.name = value;
alert(obj.name);
ary.push(obj);
alert(contains(ary, obj));
答案 2 :(得分:0)
您的代码实际上是在调用ary.indexOf("Power")
。但是ary
中没有索引为Power
的元素,因此调用总是失败的原因。实际上,Array.indexOf()
不适用于您的场景,因为它只适用于简单类型,而不适用于复杂对象。
您需要做的是检查该数组中具有指定键的对象,如果找到该对象,则删除该对象。为此,您使用Object.hasOwnProperty()
方法:
function removeFav(key) {
for (var i = 0; i < ary.length; i ++) {
// if the current array item has a property
// that matches the specified one, remove that item
if (ary[i].hasOwnProperty(key)){
ary.splice(i, 1);
return;
}
}
}