Alexey Ayzin Alexey Ayzin - 1 year ago 147
JSON Question

JSON Value Validation


var dataArr = [
{id: 123, str:"Alexey Ayzin", bg:"#FFFFFF"},
{id: 234, str:"Mira Mdivani"},
{id: 345, str:"Maria Iliakova"},
{id: 456, str:"Dennis Ayzin"}

As you can see, bg:"FFFFFF" exists in one of the subsets, but not the others. How can I change this:

for (var i = 0; i < datalength; i++) {
if(dataArr[i].bg === "undefined"){
data[i].bg = stringToColour(dataArr[i].str);

So that it checks each subset for the existence of a BG and if it doesn't exist, fills it with one. JSFiddle

Answer Source

You could use the in operator, if you have falsey values, which are valid.

if (!('bg' in dataArr[i])) {
    // set dataArr[i].bg
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download