最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

jquery - Find item by key in Javascript - Stack Overflow

programmeradmin1浏览0评论

Quick rookie question please.

Suppose I have a javsacript object like so:

var meh=[["cars",27], ["bikes",85], ["skates",4]];

To go through each data object here, I can do this:

$.each(meh, function(index,value){        
    console.log(value) //returns ["cars",27] etc..
 });

And considering I know the place of, say, cars, I can do this to access it:

console.log(meh[0][0]) //shows "Cars"

and of course, if I want the value of cars, I need to do this:

console.log(meh[0][1]) //Shows 27

Now, I have the string - Keys, like cars, bikes or skates

But I cant figure out this: How do I access their respective values?

meh["cars"] is returning undefined, since, as I understand, it cant find a description outside each object.

I can do meh[0]["cars"] but it defeats the point as the position of cars might change.

How do I access a value of something with their key please?

thanks.

Quick rookie question please.

Suppose I have a javsacript object like so:

var meh=[["cars",27], ["bikes",85], ["skates",4]];

To go through each data object here, I can do this:

$.each(meh, function(index,value){        
    console.log(value) //returns ["cars",27] etc..
 });

And considering I know the place of, say, cars, I can do this to access it:

console.log(meh[0][0]) //shows "Cars"

and of course, if I want the value of cars, I need to do this:

console.log(meh[0][1]) //Shows 27

Now, I have the string - Keys, like cars, bikes or skates

But I cant figure out this: How do I access their respective values?

meh["cars"] is returning undefined, since, as I understand, it cant find a description outside each object.

I can do meh[0]["cars"] but it defeats the point as the position of cars might change.

How do I access a value of something with their key please?

thanks.

Share Improve this question asked May 17, 2011 at 11:35 LocustHordeLocustHorde 6,39916 gold badges66 silver badges98 bronze badges
Add a comment  | 

5 Answers 5

Reset to default 8

You should change that to objects

var meh={"cars" :27 , "bikes" :85, "skates" :4};

Now you can simply access it via keys

alert(meh['cars']); //27

If you have access to the code and can change the object, change it to something like this:

meh = {
  'cars': 27,
  'bikes': 85,
  'skates': 4
};

and you can access them with keys like

meh["cars"] //will give you 27

If you cannot change the code, then the only way I see is using jQuery.each and comparing each key with your known key and assigning it to a temp variable.

Use an object instead:

var meh = {
    "cars":   27,
    "bikes":  85,
    "skates": 4
};

You can iterate over it using $.each():

$.each(meh, function (key, value) {
    // key == "cars" and value == 27, etc.
});

Accessing values works like this:

meh.cars

which is equivalent to this:

meh["cars"]

Obviously, the second notation can be used with variables.

You have your data structure in an array, so you are always going to have to access by the array syntax e.g. [0][1]. The arrays in JavaScript are not associative. You could write a helper function which iterates around the array looking for the key you specify and returning the value back. Or you could change your data structure to be Objects, which do support key lookup.

If you can't alter the object, you can create a map to make managing the indices simple. E.g.

map = {
  'cars': 0,
  'bikes': 1,
  'skates': 2
};

Then, you can do:

alert(meh[map['cars']][1]);
发布评论

评论列表(0)

  1. 暂无评论