Alexey Ayzin Alexey Ayzin - 6 months ago 22
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


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

if (!('bg' in dataArr[i])) {
    // set dataArr[i].bg