关于我们

质量为本、客户为根、勇于拼搏、务实创新

< 返回新闻公共列表

JS 实现列表操作 #17

发布时间:2023-06-27 10:00:25

列表


人们经常使用列表,比如待办事项列表、购物车等,如果数据不太多的话,列表就显得尤为有用。


JS实现


function list() {  this.dataStore = []; //初始化数组  this.clear = clear; //清除列表  this.remove = remove; //移除列表中的元素  this.find = find; //寻找列表中的元素  this.length = length; //返回列表的长度 } function find(element) {  for (var i = 0, len = this.dataStore.length; i < len; i++) {  if (this.dataStore[i] === element) {  return i;  }  }  return -1; } function remove(element) {  for (var i = 0, len = this.dataStore.length; i < len; i++) {  if (this.dataStore[i] === element) {  this.dataStore.splice(i, 1);  }  }  return this.dataStore; } function length() {  return this.dataStore.length; } function clear() {  this.dataStore = []; }

/template/Home/leiyu/PC/Static