Skip to content
Advertisements

Array keys()

JavaScript Array keys() Examples

The keys() method returns a new Array Iterator object that contains the keys for each index in the array.

array.keys()


JavaScript Array keys() Examples

Example 1


        let list = ["zero", "first", "second", "third"];
        let iterator = list.keys();

        for(let key of iterator) {
            console.log(key)
        }

output:

0
1
2
3


Example 2

        let cars = ["bmw", "audi", "mercedes", "toyota"];
        console.log(cars.keys())

output:

Array Iterator


Example 3

        let arr = ['a', 'b', 'c'];
        let iterator = arr.keys();
        let keyArray = [];

        for (let key of iterator) {
            keyArray.push(key);
        }
        console.log(keyArray);

output:

[0, 1, 2]


Browser Support

Chrome38
Edgeyes
Firefox28
Internet Explorerno
Opera25
Safari8
Android webviewyes
Chrome for Androidyes
Edge mobileyes
Firefox for Android28
Opera Androidyes
Advertisements

2 Comments »

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: