0

I have an object in JavaScript that starts by looking like this:

variable = {1: 'No', 2: 'No', 3: 'No'} 

and it dynamically changes based on user and updates to look like this, for example:

variabel = {1: 'Yes', 2: 'No', 3: 'Yes'} 

I now want to dynamically build an if statement for any keys that have a yes to be like this:

if (feature.properties.1.indexOf(variable.1)!=-1 && feature.properties.3.indexOf(variable.3)!=-1)  

Is there a way to do this? I tried looping over the object to dynamically build a string statement and then attempted to unstring it to be used in the if statement but had no luck. Any help is much appreciated.

4
  • 1
    I think you can't have x.1 but have to use x[1] instead Commented May 11, 2015 at 14:41
  • What is in feature.properties... And why are you using strings for booleans? Commented May 11, 2015 at 14:45
  • feature.properties is coming from a json object. And I am not sure about the boolean vs string, does it matter? Commented May 11, 2015 at 14:54
  • So are you trying to test if, for every own property of feature.properties, there is a same–named property of variable whose value is a substring of the feature.properties value? Commented May 11, 2015 at 15:00

2 Answers 2

2
var success = true;

for (var i in variable) {
   if (feature.properties[i].indexOf(variable[i]) === -1) {
       success = false;
       break;
   }
}

// evaluate success here
Sign up to request clarification or add additional context in comments.

Comments

1

You can take the properties present in your desired comparison target (called "reference" for simplicity) and check the corresponding properties in the feature. If a property exists in the reference but not the feature, this will consider the validation a failure.

var reference = {
  1: 'Yes',
  2: 'No',
  3: 'Yes'
};

var feature1 = {
  properties: {
    1: 'Yes',
    3: null
  }
};

var feature2 = {
  properties: {
    1: 'Yes',
    3: 'Yes'
  }
};

function compareReference(ref, feature) {
  var props = feature.properties;
  return Object.keys(ref).reduce(function(prev, key) {
    console.log('comparing', key, ref[key], props[key], prev);
    if (ref[key] === 'Yes') {
      // Only compare yes values
      return prev && props[key] === 'Yes'; 
    } else {
      return prev;
    }
  }, true);
}

document.getElementById('r1').textContent = '' + compareReference(reference, feature1);
document.getElementById('r2').textContent = '' + compareReference(reference, feature2);
<div id="r1"></div>
<div id="r2"></div>

4 Comments

Shouldn't the second object return true? every might be a better choice than reduce.
I was originally trying to build a string statement to just insert into the if statement (unstringed) but can't figure out how to make the string not a string.
@EvanL If you already have an object representing the desired options and an object with the actual options, you can replace the if condition with a function call. Building and evaluating a string at runtime is poor practice, since it's very difficult to debug and can cause other problems.
Okay, I will try that. Thanks. (I am a bit new to using javascript if that is not obvious)

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.