0

I have an array like below

  var colorArray = ["#a", "#b", "#c", "#d", "#e"];

From this I will generate a map like this

  function initilizeColorMap(){
    for(var i = 0 ;i <colorArray.length ;i++){
    colorTrackingMap[i] = {value: colorArray [i],state:"unused"};
   }
 }

Hopw i can iterate through the map when i need a color (next color from the map ) by checking the state in javascript..?

1
  • I'm not sure what you're trying to accomplish. You're adding colorArray[0] (the same first color) to each element of colorTrackingMap. Commented Dec 20, 2011 at 13:37

3 Answers 3

1

You can have a method that will return the next color. Check out this jsfiddle : http://jsfiddle.net/QYWDb/

var colorArray = ["#a", "#b", "#c", "#d", "#e"];
var colorTrackingMap = [];
var currentIndex = -1;

for(var i = 0 ;i <colorArray.length ;i++){
  colorTrackingMap[i] = {value: colorArray [i],state:"unused"};
}

function getNextColor() {

    if (currentIndex > colorTrackingMap.length)
        currentIndex = 0;
    else
        currentIndex++;

    while ( colorTrackingMap[currentIndex] !== undefined  && 
            colorTrackingMap[currentIndex].state !== "unused" ) {
        currentIndex++;
    }

    if ( colorTrackingMap[currentIndex] )
        return colorTrackingMap[currentIndex].value;
    else
        return "No color available";
}
Sign up to request clarification or add additional context in comments.

Comments

0

If you need color according to given index you don't have to iterate, use such code:

var currentIndex = 0;
function Next() {
    var tracking = colorTrackingMap[currentIndex];
    alert("color: " + tracking.value + ", state: " + tracking.state);
    currentIndex++;
    if (currentIndex >= colorTrackingMap.length)
        currentIndex = 0;
}

Live test case.

If you mean searching the array for item with specific value, just use ordinary loop:

function Find() {
    var color = document.getElementById("color").value;
    var state = "";
    for (var i = 0; i < colorTrackingMap.length; i++) {
        if (colorTrackingMap[i].value.toLowerCase() === color) {
            state = colorTrackingMap[i].state;
            break;
        }
    }

    if (state.length == 0) {
        alert("color isn't mapped");
    } else {
        alert("state: " + state);
    }
}

You can also pass the color as function argument, this is just for sake of example.

Updated test case.

Comments

0

You could use something like this:

var ColourMap = function (arr) {
    var _map = [],
        out = [],
        i,
        len;
    // Set up the colour map straight away
    for (i = 0, len = arr.length; i < len; i++) {
        _map.push({
            value: arr[i],
            state: "unused"
        });
    }

    return {
        get_next: function () {
            var i,
                len,
                next;
            for (i = 0, len = _map.length; i < len; i++) {
                if (_map[i].state === "unused") {
                    next = _map[i];
                    break;
                }
            }
            return next;
        }
    }
};

And then use something like:

var m = new ColourMap(["#a", "#b", "#c", "#d", "#e"]);
m.get_next(); // get the next available element

Here's a working example.

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.