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

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.

主站蜘蛛池模板: 富锦市| 惠来县| 洪泽县| 两当县| 滁州市| 万源市| 临夏县| 凤冈县| 双江| 陕西省| 朝阳区| 家居| 遂川县| 平果县| 衡南县| 桂东县| 宜章县| 丽水市| 延寿县| 克什克腾旗| 昆明市| 平南县| 涿鹿县| 道孚县| 苏尼特右旗| 合水县| 贵阳市| 隆化县| 莱州市| 萨嘎县| 乌兰浩特市| 濮阳县| 白银市| 乌拉特中旗| 贵定县| 永吉县| 武宣县| 双流县| 富阳市| 邢台市| 淮北市|