1

I'm kinda stuck with an issue. This is the response for my AJAX call.

var test = [
  {
    "analytics.feature": "false"
  },
  {
    "demo": "true"
  },
  {
    "analytics.demo": "false"
  }
]

Now I want to check if analytics.demo is enabled or disabled.

Can I use _.find?

_.find(test, {analytics.demo}); //returns error

I just want to do this

if(analytics.demo) {
//Do something
}

How do I do that? Can someone help?

Instead of iterating objects and comparing, can I use underscorejs/jQuery to get the exact value for a key?

Pranav C Balan
  • 113,687
  • 23
  • 165
  • 188
TechnoCorner
  • 4,879
  • 10
  • 43
  • 81
  • What's wrong with plain javascript? `test[2]["analytics.demo"]` will return "false"... – nicael Jun 05 '16 at 10:09
  • The order keeps changing. Is there anyway I can search the "key" and get the value. Instead of blindly doing test[2] @nicael – TechnoCorner Jun 05 '16 at 10:11

4 Answers4

4
const options = Object.assign(...test)
console.log(options) // { "analytics.feature": "false", demo: "true", "analytics.demo": "false" }
if (options['analytics.demo'] === 'true') {
  // ...
}

(Note that this uses ES6. For ES5 use var options = Object.assign.apply(object, test) instead, and you also need to polyfill Object.assign.)

This uses Object.assign() with the spread operator to merge the objects into one, then get the "analytics.demo" option from the new single object. It has to be accessed using bracket notation because the key has a dot in. It is compared to the string true because "false" (string) is true but false (boolean) is false.

There is more information on SO about flattening arrays and converting strings to booleans.

PS is there any way you can get the Ajax call to use proper booleans instead of strings? It would be easier to work with.

Community
  • 1
  • 1
gcampbell
  • 1,252
  • 15
  • 13
3

You can check if any of the items in the array matches a certain condition like so:

var isDemoEnabled = test.some(function (item) {
    return item['analytics.demo'] === 'true';
});

if (isDemoEnabled) {
    // Do something 
}

If the values were boolean, you could remove === 'true'

Rudi
  • 2,987
  • 1
  • 12
  • 18
2

Assuming you're using underscore, check out the documentation regarding find: http://underscorejs.org/#find

Find runs a predicate function for every element in an array and returns a new array that contains every value where the predicate function returned true.

In this case we want to find if the element has a field analytics.demo To do this we can use the underscore function _.has: http://underscorejs.org/#has

all in all:

var result = _.find(test, function(elem) {
    return _.has(elem, "analytics.demo");
  }
);
simba
  • 37
  • 1
2

You could iterate over and check if the key is given and return then the result.

function getKeyValue(array, key) {
    var value;
    array.some(function (a) {
        if (key in a) {
            value = a[key];
            return true;
        }
    });
    return value;
}

var test = [{ "analytics.feature": "false" }, { "demo": "true" }, { "analytics.demo": "false" }]

console.log(getKeyValue(test, 'analytics.demo')); // 'false' a string with this value
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392