【发布时间】:2014-07-30 14:23:32
【问题描述】:
I have a set of radio buttons, when a radio button is selected the element is pushed into an object.
然后我需要获取对象中元素的值,然后将它们推送到数组中,如果它们尚未在数组中。
我的问题是,我使用此函数在每个值的数组中不断重复或更多,因为我没有正确检查它们是否存在。
如何检查该元素是否已存在于我的数组中,然后将其排除在添加之外?
_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);
}
});
【问题讨论】:
-
"被推入一个对象" 怎么样? “推入对象”是什么意思? :)
-
我的意思是对象是用键值对更新的。
-
您是否使用 id 作为键?如果是,那怎么可能有重复?
-
是
node一个元素或一个对象。 -
'node' 是一个对象,它们都是对象,我正在比较每个对象的 id。
标签: javascript arrays angularjs object