Javascript 从json中删除项再次单击
我曾在这里询问过如何更准确地从json中删除项目: 而且效果很好。。现在我遇到了同样的问题,但是代码有点不同。总之,我有一个清单。当我点击一个项目时,它会将其属性添加到json中。我希望,如果我再次单击此项,它将从json中删除其属性。因此,我单击Item1并添加,再次单击Item1并删除 这就是我试过的Javascript 从json中删除项再次单击,javascript,jquery,json,angularjs,Javascript,Jquery,Json,Angularjs,我曾在这里询问过如何更准确地从json中删除项目: 而且效果很好。。现在我遇到了同样的问题,但是代码有点不同。总之,我有一个清单。当我点击一个项目时,它会将其属性添加到json中。我希望,如果我再次单击此项,它将从json中删除其属性。因此,我单击Item1并添加,再次单击Item1并删除 这就是我试过的 myApp.controller("mycontroller", ["$scope", "$http", function($scope, $http){ $scope
myApp.controller("mycontroller", ["$scope", "$http",
function($scope, $http){
$scope.getItems = {
"data": [
{
"label": "first",
"objects": [
{
"name": "firstObj",
"attributes": [
{
"attrname": "Item1",
"attrValue": "",
"attrType":"text"
},
{
"attrname": "Item2",
"attrValue": "",
"attrType":"text"
}
]
}
],
"key": "bolla"
}
]
};
$scope.filterSelected = $scope.getItems.data[0].objects;
$scope.myNewArray = {
objects: [
]
}
$scope.createjson = function(attribute, items) {
var obj = {};
obj.name = attribute;
obj.attributes = [];
obj.attributes.push(items);
return obj;
}
$scope.checkIfAttributeExists = function(attribute) {
for(var i=0; i<$scope.myNewArray.objects.length; i++) {
if($scope.myNewArray.objects[i]["name"] == attribute) {
return i;
}
}
return -1;
}
$scope.pushItems = function pushItems(attribute, items) {
var index = $scope.checkIfAttributeExists(attribute);
if(index == -1) {
var obj = $scope.createjson(attribute, items);
$scope.myNewArray.objects.push(obj);
} else if(index !== -1) {
$scope.myNewArray.objects.splice(index, 1);
}else {
$scope.myNewArray.objects[index].attributes.push(items);
}
}
$scope.showNewJson = function() {
return $scope.myNewArray;
}
}]);
正如您所看到的,属性“Item1”有两次,因为我在它上面单击了两次。但是如果我点击两次,项目1就会消失!因此,正确的json应该是:
{"objects":[{"name":"firstObj","attributes":[{"attrname":"Item2","attrValue":"","attrType":"text"}]}]}
您传递了错误的参数。您可以大大简化代码,如下所示: HTML 通过使用对象本身,您不需要查看对象的属性来检查它是否存在,而是检查实际对象的索引
对代码的一点建议:函数定义是$scope.checkIfAttributeExists=function(attribute){…,而实际调用是“var index=$scope.checkIfAttributeExists(items);”这太令人困惑了!我更新了Fiddle的链接。不要将属性值传递到函数中,而是处理整个对象。您的代码远比需要的复杂。还要了解什么是
json
。您不是在创建或使用json,而是在处理javascript对象和数组。json
只是一个str使用类似javascript对象和数组的数据传输格式,我将向服务器发送该数组。但我必须发送我需要的。因此,为每个选定项发送一个属性。不是更多。了解吗?尝试查看我对questif的小编辑,如果我传递var index=$scope.checkIfAttributeExists(属性);如果我再次单击,它将删除该项目,为true。。但即使我单击第二个项目,它也会删除该项目。如果单击不同的项目(Item1、Item2、Item3…),它们将添加到json中。但只能是其中之一。因此,如果您再次单击Item1,您将得到如下列表:(Item2、Item3…)没有第一个。我想要单击该项目的单个属性。有帮助吗?现在了解我的问题吗?是的!!!!!它正在工作!非常完美!您帮助我改进了代码。谢谢!charlieftl我需要帮助:(
{"objects":[{"name":"firstObj","attributes":[{"attrname":"Item2","attrValue":"","attrType":"text"}]}]}
var index = $scope.checkIfAttributeExists(attribute);
<ul ng-repeat="item in att.attributes">
<li>
<a ng-click="pushItems(item)">{{item.attrname}}</a>
</li>
</ul>
$scope.pushItems = function pushItems(item) { // note only single argument now
// index the whole item object
var itemIndex = $scope.myNewArray.objects.indexOf(item);
if (itemIndex == -1) {
// if index doesn't exist ...add to array
$scope.myNewArray.objects.push(item);
} else {
// otherwise remove from array
$scope.myNewArray.objects.splice(itemIndex, 1);
}
}