I have a set of radio buttons, when a radio button is selected the element is pushed into an object.
我有一组单选按钮,当选择单选按钮时,元素被推入对象。
Then I need to take the value of the elements in the object and then push them into an array, if they are not already in the array.
然后我需要获取对象中元素的值,然后将它们推入一个数组,如果它们不在数组中。
My problem is that I keep getting duplicates or more in my array of each of the values with this function because I am not checking correctly if they exists.
我的问题是,我使用此函数在每个值的数组中不断获得重复或更多,因为我没有正确检查它们是否存在。
How can I check if the element already exists in my array and then exclude it from being added?
如何检查元素是否已存在于我的数组中,然后将其排除?
_this.selected = {};
_this.selectedArray = [];
//loop through the object
angular.forEach(_this.selected, function ( item ){
//if it is not the first time the array is getting data loop through the array
if(_this.selectedArray.length > 0) {
_this.selectedArray.forEach(function (node){
//check to see if the item already exists-- this is where it is failing
//and running the loop too many times
if(node.id !== item.id){
_this.selectedArray.push(item);
}
});
} else {
_this.selectedArray.push(share);
}
});
4 个解决方案
#1
1
You can use additional hash to check if you have already added item to array.
您可以使用其他哈希来检查是否已将项添加到数组。
_this.selected = {};
_this.selectedArray = [];
_this.selectedHash = {};
//loop through the object
angular.forEach(_this.selected, function ( item ){
if(_this.selectedHash[item.id]) { return; }
_this.selectedArray.push(item);
_this.selectedHash[item.id] = 1;
});
#2
0
Have you tried jQuery's inArray
? http://api.jquery.com/jquery.inarray
你试过jQuery的inArray吗? http://api.jquery.com/jquery.inarray
It works the same as the indexOf
method on String
.
它与String上的indexOf方法的工作方式相同。
You could try if $.inArray(node, selectedArray) == -1 // element is not in the array
您可以尝试$ .inArray(node,selectedArray)== -1 //元素不在数组中
#3
0
You can run this function on that array to reduce it to unique values rather than applying complex code to check whether the array had an element or not
您可以在该数组上运行此函数以将其减少为唯一值,而不是应用复杂代码来检查数组是否具有元素
var getOnlyUniqueValuesInArray = function(arrayObj) {
return arrayObj.reduce(function(a, b) {
if (a.indexOf(b) < 0) a.push(b);
return p;
}, []);
};
#4
0
Hi that should helps
嗨那应该有帮助
var app = angular.module('app', []);
app.controller('firstCtrl', function($scope){
var app = angular.module('app', []);
app.controller('firstCtrl', function($scope) {
_this.selected = {};
_this.selectedArray = [];
//loop through the object
angular.forEach(_this.selected, function(item) {
//if it is not the first time the array is getting data loop through the array
if (_this.selectedArray.length > 0) {
var canAdd = true;
_this.selectedArray.forEach(function(node) {
//do not add iny any of node.id will be equal to item.id
if (node.id == item.id) {
canAdd = false;
}
});
if(canAdd){
_this.selectedArray.push(item);
};
} else {
_this.selectedArray.push(share);
}
});
})
});
#1
1
You can use additional hash to check if you have already added item to array.
您可以使用其他哈希来检查是否已将项添加到数组。
_this.selected = {};
_this.selectedArray = [];
_this.selectedHash = {};
//loop through the object
angular.forEach(_this.selected, function ( item ){
if(_this.selectedHash[item.id]) { return; }
_this.selectedArray.push(item);
_this.selectedHash[item.id] = 1;
});
#2
0
Have you tried jQuery's inArray
? http://api.jquery.com/jquery.inarray
你试过jQuery的inArray吗? http://api.jquery.com/jquery.inarray
It works the same as the indexOf
method on String
.
它与String上的indexOf方法的工作方式相同。
You could try if $.inArray(node, selectedArray) == -1 // element is not in the array
您可以尝试$ .inArray(node,selectedArray)== -1 //元素不在数组中
#3
0
You can run this function on that array to reduce it to unique values rather than applying complex code to check whether the array had an element or not
您可以在该数组上运行此函数以将其减少为唯一值,而不是应用复杂代码来检查数组是否具有元素
var getOnlyUniqueValuesInArray = function(arrayObj) {
return arrayObj.reduce(function(a, b) {
if (a.indexOf(b) < 0) a.push(b);
return p;
}, []);
};
#4
0
Hi that should helps
嗨那应该有帮助
var app = angular.module('app', []);
app.controller('firstCtrl', function($scope){
var app = angular.module('app', []);
app.controller('firstCtrl', function($scope) {
_this.selected = {};
_this.selectedArray = [];
//loop through the object
angular.forEach(_this.selected, function(item) {
//if it is not the first time the array is getting data loop through the array
if (_this.selectedArray.length > 0) {
var canAdd = true;
_this.selectedArray.forEach(function(node) {
//do not add iny any of node.id will be equal to item.id
if (node.id == item.id) {
canAdd = false;
}
});
if(canAdd){
_this.selectedArray.push(item);
};
} else {
_this.selectedArray.push(share);
}
});
})
});