原文:Knockout获取数组元素索引的2种方法,在MVC中实现 在遍历数组.集合的时候,通常要获取元素的索引,本篇体验使用Knockout获取索引的2种方法. 假设有这样的一个模型: namespace UseIndex.Models { public class Student { public int Id { get; set; } public string Name { get; set; } } } 在HomeController中,先模拟一个Student的集合,在投影出Name
1.一般要同时遍历数组的索引和元素需要先确定数组的长度length(元素个数),然后使用range函数来生成数组的索引,最后使用该索引来访问数组的元素. 具体做法如下: l = [2,7,11,15] for i in range(len(l)): print i,l[i] 结果: 2.使用enumerate函数可以很方便的做到以上功能 l = [2,7,11,15] for index,value in enumerate(l): print index,value 结果: 0 2 1 7 2
//移除数组元素 Array.prototype.remove = function(val) { var index = this.indexOfArr(val); if (index > -1) { this.splice(index, 1); } }; //获取在数组的索引 Array.prototype.indexOfArr = function (val) { for (var i = 0; i < this.length; i++) { if (JSON.stringify(thi