官术网_书友最值得收藏!

Finding objects in an array and removing array items

The main task in this recipe is to find out whether an arbitrary object exists in an array. A way to remove objects from the array is also explored.

How to do it...

The following steps illustrate how you can perform object existence tests and object removal in an array:

  1. Create a sample array as follows:
    var colorsArray = new Array();
    colorsArray[0] = 'Blue';
    colorsArray[1] = 'Red';
    colorsArray[2] = 'White';
    
  2. Determine whether an object exists in an array by trying to find its position in the array:
    var position = colorsArray.indexOf('White');
    // postition is 2, the index of 'White' in the array.
    position = colorsArray.indexOf('Brown');
    // 'Brown' does not exist in the array,
    // position is -1.
    
  3. Remove one of the objects from the array:
    colorsArray.remove('Blue');
    position = colorsArray.indexOf('Blue');
    // 'Blue' does not exist anymore,
    // position is -1.
    

How it works...

Ext JS augments the native Array class with Array.indexOf(object) and Array.remove(object). While indexOf(object) works by examining each array element until it finds one that matches the supplied argument, remove(object) uses the native Array.splice(index, howmany, element1,....., elementX) function to remove the supplied argument from the array.

主站蜘蛛池模板: 宜春市| 永吉县| 梁平县| 朔州市| 读书| 蓬溪县| 焉耆| 渭源县| 陵水| 肥东县| 鹤壁市| 汾阳市| 张家口市| 西盟| 仲巴县| 崇左市| 乐平市| 桦川县| 澳门| 遂平县| 玛多县| 凌云县| 东至县| 涟源市| 阿鲁科尔沁旗| 罗源县| 平武县| 浑源县| 禹州市| 达拉特旗| 祁阳县| 刚察县| 罗山县| 广宁县| 兴义市| 龙泉市| 团风县| 通山县| 东乡县| 安义县| 七台河市|