Book Image

Ext JS 3.0 Cookbook

Book Image

Ext JS 3.0 Cookbook

Overview of this book

Using Ext JS you can easily build desktop-style interfaces in your web applications. Over 400,000 developers are working smarter with Ext JS and yet most of them fail to exercise all of the features that this powerful JavaScript library has to offer. Get to grips with all of the features that you would expect with this quick and easy-to-follow Ext JS Cookbook. This book provides clear instructions for getting the most out of Ext JS with and offers many exercises to build impressive rich internet applications. This cookbook shows techniques and "patterns" for building particular interface styles and features in Ext JS. Pick what you want and move ahead. It teaches you how to use all of the Ext JS widgets and components smartly, through practical examples and exercises. Native and custom layouts, forms, grids, listviews, treeviews, charts, tab panels, menus, toolbars, and many more components are covered in a multitude of examples.The book also looks at best practices on data storage, application architecture, code organization, presenting recipes for improving themóour cookbook provides expert information for people working with Ext JS.
Table of Contents (15 chapters)
Ext JS 3.0 Cookbook
Credits
About the Author
About the Reviewer
Preface

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. 1. Create a sample array as follows:

    var colorsArray = new Array();
    colorsArray[0] = 'Blue';
    colorsArray[1] = 'Red';
    colorsArray[2] = 'White';
    
  2. 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. 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.