Javascript数组操作及索引

来源:互联网 发布:电饼铛品牌 知乎 编辑:程序博客网 时间:2024/05/22 15:26

1:清空数组最高效的做法

parentThis.PaperQuestionStrategiesList.length = 0;

 

2:push and pop

parentThis.PaperQuestionStrategiesList.push(questionStrategy);

 

3:字符串索引

parentThis.QuestionUnits[i] = parentThis.QuestionUnits[item.Id] = item;

从本质上来说,QuestionUnits[item.Id],实际上是为QuestionUnits对象创建了一个属性,该属性名为item.Id,如:

image

Id 为 ss,item值为”dd”。

那么,如何来遍历一个jq类的所有属性呢?,可以如下:

$.each(obj, function(key, element) { alert('key: ' + key + '\n' + 'value: ' + element); });

或者:

for(var key in obj) { alert('key: ' + key + '\n' + 'value: ' + obj[key]); }

原创粉丝点击