简体   繁体   中英

How to select a current object's value in array with js or jQuery

I have an array of objects like so...

[Object, Object, Object]

Basically, there is a div with a click function and I want this click function to display that div's (which also has an object) specific property.

The two ways I know of, is using a for loop and defining an i variable and calling that or calling a specific object.

  1. For loop method to select current object in array:

     for (var i = 0; i < PODS.podsData.length; i++) { console.log (PODS.podsData[i].posMinimized); PODS.podsData[i].posMinimized = 1; } 
  2. console.log(PODS.podsData[5].posMinimized)

So is there another way of declaring the specific PODS.podsData? Basically instead of PODS.podsData[i] or PODS.podsData[5] is there another value I can put between the [] that will log the current object's value I need, like PODS.podsData[XXX] or something?

NO you have to provide the name of the item to be able to get the item.

PODS.podsData[5] // will work

PODS.podsData['Please get me number 5'] // Wont work

So to get the 5th item in the array you must provide the number 5 unless you have a object like this

   var podsData = {
        coolName: {
          posMinimized: 1
        }
    }

Then you would use

podsData.coolName.posMinimized or podsData['coolName].posMinimized

to get the result

You could try that: {maybe...}

$('.pod').each(function (i) {
    $(this).click(function () {
        var posMinimized = PODS.podsData[i].posMinimized;
        //do some stuff with current clicked element
    });
});

But i still don't understand your issue...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM