user882196
user882196

Reputation: 1721

How to check the object property in javascript

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..?

Upvotes: 0

Views: 186

Answers (3)

jabclab
jabclab

Reputation: 15042

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.

Upvotes: 0

Dominic Goulet
Dominic Goulet

Reputation: 8103

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";
}

Upvotes: 1

Shadow Wizard
Shadow Wizard

Reputation: 66389

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.

Upvotes: 0

Related Questions