2

I have an array:

var assArray = {};
assArray["VeryImportantData1"]="VeryImportant"; 
assArray["VeryImportantDataTest"]="VeryImportant"; 
assArray["Very"]="VeryImportant"; 
assArray["Vey"]="VeryImportant"; 
assArray["Test"]="VeryImportant"; 
assArray["Dumdum"]="VeryImportant"; 

How do I get the index of every element in this array in Javascript?

1
  • 2
    Object.keys(assArray)? Commented Jul 14, 2013 at 13:45

4 Answers 4

1

First of all, a bit of terminology: That is not an array, it is an object.

Now that that's out of the way, I can't tell for certain what you're asking for. Are you looking for this, perhaps?:

for( var i in assArray) {
    alert(i+": "+assArray[i]);
    // bad example, do something useful here
}
Sign up to request clarification or add additional context in comments.

2 Comments

You may also want to filter out properties from prototypes using assArray.hasOwnProperty(i)
@steff There shouldn't be any if the environment is sane.
1

You can use something like:

var keys = Object.keys(assArray);

for(var index = 0; index < keys.length; index++)
{
    var key = keys[index];

    // Do something with the index and key...
    alert(key + ' has an index of: ' + index);
}

Reference for Object.keys(obj)

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/keys

Object.keys returns an array whose elements are strings corresponding to the enumerable properties found directly upon object. The ordering of the properties is the same as that given by looping over the properties of the object manually.

2 Comments

If you can use Object.keys() you can probably also use forEach() :)
Yeah, thought about that...not sure why I chose index++ in the end!
0

Basically what you have here is a set of "key" "value" pairs. You don't have an array with 0...n indexes. To access the data structure you'll need to use your keys ("VeryImportantData1", "VeryImportantDataTest", etc.).

Simple examples of how to access the key/values:

alert(assArray['Dumdum']); //This will alert "VeryImportant"

console.log(assArray['Vey']); //This will log "VeryImportant"

Comments

0

You could use something like:

function getKeyListFor(obj, term, sortOpt) {
    var keys=[];
    for (var k in obj) {
        if (obj[k] === term) keys.push(k);
    }
    if (keys.length) {
        if (sortOpt) keys.sort();
        return keys;
    }
    else {
        return null;
    }
}

Note: The sort-argument would be optional. (Since keys of associative arrays (objects) are stored internally as hashes, they are returned in apparently random order else. Mind that Array.sort() without any sorting function as an argument always performs a string comparison in incrementing order. As any value-type may be converted to a meaningful string representation, this is also a sane default.)

// Usage example
var fruitColors = {
    'banana': 'yellow',
    'apple': 'red',
    'orange': 'orange',
    'strawberry': 'red'
}
var redList = getKeyListFor( fruitColors, 'red', true );
// returns ['apple', 'strawberry']
var blueList = getKeyListFor( fruitColors, 'blue' );
// returns null
if (blueList) {
    // delete all blue entries
    for (var i=0; i<blueList.length; i++) {
        var key = blueList[i];
        delete fruitColors[key];
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.