How to write this script properly so I can match the value on the object.
var objGroup = [
{ "color": "YELLOW", "number": "11,7,44,22" },
{ "color": "BLUE", "number": "8,20,9" },
{ "color": "GREEN", "number": "12,34,55" }
];
objGroup.map(function (groupNum) {
if (groupNum.number== "11") {
alert(groupNum.color);
} else {
return null
}
});
This will return the object that has a number value that contains the supplied number.
http://jsfiddle.net/rVPu5/
Alternative using newer .filter function which won’t be as widely supported:
http://jsfiddle.net/rVPu5/2/
Finally – the jQuery version:
http://jsfiddle.net/rVPu5/3/