0

How do I store values into an array or hash to be recalled individually without adding individual identifier?

var myarray = [];
$(".express").each(function () {
  myarray.push($(this).text()); 
});

function flashEXPRESS() {
   $(".express").each(function () {
     if ($(this).text() == 'NEW') { $(this).text() = myarray[???]; }
     else { $(this).text() == 'NEW'}
   });
}
var flashEXPRESSid = 0;
flashEXPRESSid = setInterval("flashEXPRESS()",1000);
1
  • Unrelated note: You should pass functions to setInterval, not strings. flashEXPRESSid = setInterval(flashEXPRESS,1000); Commented Nov 30, 2011 at 15:17

2 Answers 2

2

The callback to each gives you the index as the first parameter. That's probably what you need.

$(".express").each(function (index) {
     if ($(this).text() == 'NEW') { $(this).text() = myarray[index]; }
     else { $(this).text() == 'NEW'}
});
Sign up to request clarification or add additional context in comments.

5 Comments

What if the number of elements changes?
Then you'll have to store this differently. I don't mean that as a snide response -- this approach assumes and works only so long as the .express elements and your myarray array are sync'd up
#1) Thanks for your help. #2)Any ideas how to do it differently (besides redefining the array when the number of elements changes)? I have another idea I would like to implement, but would need to be able to call the values individually.
I would somehow—maybe with html5 data attributes?—uiquely define each .express div, then, instead of an array, use an object, and store properties based on that info. Let me put an edit up for you.
@shaun5 - I added a second answer. It's untested, but it should get you close. The only potentially hard part will be coming up with a unique value for data-idforobject. Also note that reading data attributes with the jQuery data function was a fairly recent (not sure which version) addition, so make sure you're using the current version of jQuery.
0

From the comments, if you want to allow your divs to change, and be added, and not screw up your array indexes, something like this might work:

var myarray = {}; //object, not an array
$(".express").each(function () {
  var identifier = $(this).data("idforobject");
  myarray[identifier] = $(this).text(); 
});

function flashEXPRESS() {
   $(".express").each(function () {
     if ($(this).text() == 'NEW') { 
         $(this).text() = myarray[$(this).data("idforobject")]; 
     }
     else { $(this).text() == 'NEW'}
   });
}

Then on your divs:

<div class="express" data-idforobject="something unique">

Just rename myarray to something else, since it's not an array anymore

2 Comments

Thanks for this! It isn't exactly what I originally asked for because I will have to provide a unique identifier. I was imagining, that the browser would assign each element a unique id that could be referenced as a key and reused...
@shaun5 - it would be nice if the browser did that for you, but I think you'll have to manage those unique ids yourself :)

Your Answer

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