跟踪数组中被删除的索引
keeping track of the removed indices of an array javascript
我有一个文本框和复选框的表单。当我将值填充到复选框中时,该值被推入数组。当我选中复选框时,该值将从该数组中删除。这是我的工作代码笔。
这是我的表单代码
<label class="item item-input">
<input type="text" placeholder="In a word, what is important to you?" ng-model="values.first" >
<ion-checkbox ng-click="toggleCheckBox(success.first,values.first)" ng-change="show2='true'" ng-model="success.first"
ng-disabled="!values.first" style="border: none;padding-left: 30px;" class="checkbox-royal"></ion-checkbox>
</label>
<label class="item item-input" ng-show="show2" ng-class="{'animated-custom slideInLeft':success.first}">
<input type="text" placeholder="What else is important to you?" ng-model="values.second" >
<ion-checkbox class="checkbox-royal" ng-model="success.second" ng-click="toggleCheckBox(success.second,values.second)" ng-change="show3='true'" ng-disabled="!values.second" style="border: none;padding-left: 30px;"></ion-checkbox>
</label>
<label class="item item-input" ng-show="show3" ng-class="{'animated-custom slideInLeft':success.second}">
<input type="text" placeholder="What else is important to you?" ng-model="values.third">
<ion-checkbox class="checkbox-royal" ng-model="success.third" ng-click="toggleCheckBox(success.third,values.third)" ng-change="show4='true'" ng-disabled="!values.third" style="border: none;padding-left: 30px;"></ion-checkbox>
</label>
<label class="item item-input" ng-show="show4" ng-class="{'animated-custom slideInLeft':success.third}">
<input type="text" placeholder="What else is important to you?" ng-model="values.four">
<ion-checkbox class="checkbox-royal" ng-model="success.four" ng-click="toggleCheckBox(success.four,values.four)" ng-change="show5='true'" ng-disabled="!values.four" style="border: none;padding-left: 30px;"></ion-checkbox>
</label>
这是控制器代码
.controller('myctrl',function($scope){
var values=[];
$scope.toggleCheckBox=function(isChecked, value){
if(isChecked){
values.push(value);
console.log(values);
}else{
var currentIndex = values.indexOf(value);
values.splice(currentIndex, 1);
console.log(values);
//console.log(values.indexOf(value));
}
}
})
问题是,当我从数组中删除值并再次选中复选框时,它将值推到最后。是否有可能保留该值在数组中的原始位置?
传递toggleCheckBox
中的项索引,并根据传递的索引和您想要的值设置数组项的值,如:
var x = Array();
var index = 2;
x[index] = 'A';
console.log(x);
但是请确保当您想使用数组来遍历它并拼接具有undefined
值的项时
为toggleCheckBox
添加第三个参数的位置-类似于:
...
<ion-checkbox class="checkbox-royal" ng-model="success.four" ng-click="toggleCheckBox(success.four,values.four,4)"
...
在你的控制器中:
.controller('myctrl',function($scope){
var values=[];
$scope.toggleCheckBox=function(isChecked, value, pos){
if(isChecked){
values[pos] = value;
}else{
values[pos] = null;
}
console.log(values);
}
})
理论上,您可以为数组中的特定索引添加值:
let arr = ['foo', 'bar'];
arr[3] = 'baz'; // 0 and 1 are taken
console.log(arr); //=> ['foo', 'bar', undefined, 'baz']
但是,不建议这样做。在JavaScript中,数组是编号索引,尝试解决这个行为可能很快就会出现问题。
你需要的是一个对象。如果您将输入命名为:
<input name="some_checkbox_1">
…并使用这些名称创建一个对象:
$scope.toggleCheckBox=function(isChecked, value){
values[this.name] = isChecked;
}
结果是这样的对象:
{ some_checkbox_1: true, some_checkbox_2: false } // this is the `values` variable
如果你想从这个对象中得到一个包含真值和/或假值的数组,使用object .values():
Object.values(values); //=> [true, false]
这样,您可以按顺序呈现复选框,并根据存储对象的true/false值将它们设置为checked
。希望对你有帮助。
this
指的是复选框的HTML元素
JavaScript对象属性顺序永不改变
因此,您可以创建一个对象来维护顺序,并遍历Object.keys()以根据相应的value
或false
填充数组values
:
angular
.module('ionicApp', ['ionic'])
.controller('myctrl', function ($scope) {
var values = [],
obj = {};
$scope.toggleCheckBox = function (isChecked, value, index) {
values = []; // Clean the values
obj[index] = (isChecked) ? value : false;
Object.keys(obj).forEach(function (k) {
obj[k] && values.push(obj[k]); // Fill the ordered values
});
console.clear();
console.log(values);
};
});
在视图中,我为所有复选框添加了index
,这是ion-checkbox
的编号:
<ion-checkbox
ng-click="toggleCheckBox(success.first,values.first, 1)"
ng-change="show2='true'"
ng-model="success.first"
ng-disabled="!values.first"
style="border: none;padding-left: 30px;"
class="checkbox-royal">
</ion-checkbox>
在Code Pen查看完整的解决方案
相关文章:
- 在特定索引处剥离/删除数组中的值
- 删除和重用数组索引
- 如何使用Jscript | JQuery在textArea中获取插入的|删除的字符串的索引和长度
- 如何删除cookie错误注意:PHP上的未定义索引
- 删除索引后的数组值
- 如何删除索引中加载的脚本文件中的重复项.html和业力
- 索引数据库:删除功能不起作用
- 使用另一个数组按索引删除数组中的对象
- 索引数据库删除数据库不会重置版本
- 在 jquery 中,如何通过索引[“键”] 或值删除数组元素
- 根据 jquery 中的索引删除 enitre 行
- 按索引删除数组元素
- 删除和添加嵌套对象中同一索引处的对象
- 如何使用 Java 脚本中的最后一个索引删除子字符串
- AngularJS:按索引删除数组项后索引不更新
- Javascript/Backbone-使用索引删除数组对象
- 按索引删除数组元素
- Jquery索引删除与var而不是数字
- jQuery根据索引删除复选框
- 通过_内部的索引删除当前对象.每个都有下划线