Best way to basically make a `where` clause in Javascript?

I am trying to parse the JSON that is sent to me, and all this in the format

[{key:value},{key2:value2}, ... ] 

What would be the best way to get the key2 value in this? Is there a way to do this without doing a for loop?

+7
source share
9 answers

You can use the Select function from the Underscore.js library.

+9
source

Not really, but creating such a function is not difficult. However, it does include a for loop.

To complete, it will be a function:

 function selectWhere(data, propertyName) { for (var i = 0; i < data.length; i++) { if (data[i][propertyName] !== null) return data[i][propertyName]; } return null; } 

Using:

 var key2value = selectWhere(data, "key2"); 
+5
source

jQuery grep () is a good counterpart to the Where clause:

 var array = [{key:1},{key:2}, {key:3}, {key:4}, {key:5}]; var filtered = jQuery.grep(array, function( item, index ) { return ( item.key !== 4 && index > 1 ); }); 

Then your filtered array will contain two elements:

  [{key:3}, {key:5}] 
+3
source

You cannot do this with an array, but you can create an associative array with it that looks like an object. When you do this, you can use it as a hash.

 var arr = [{key:value},{key2:value2}, ... ], obj = {}; for (var i = 0, len = arr.length; i < len; i++) { $.extend(obj, arr[i]); } console.log(obj.key2); // value2 
+2
source

Regex - no for loop:

 var key2Val = jsonString.match(/\{key2:[^\}]+(?=\})/)[0].substring("{key2:".length); 
+1
source

Here is an example that is a prototype of an Array object. Note: this is shown, for example - find not a good name for this function, and it probably will not be needed for all arrays

Instead, consider only using a function definition and creating a function of type getObjVal , calling both getObjVal(arr,'propName') , as well as LaurenT's answer.

Considering


 var arr = [{key:'value'},{key2:'value2'}]; 

Definition


 // for-loop example Array.prototype.find = function (prop){ for(var i=this.length; i--; ) if (typeof this[i][prop] !== 'undefined') return this[i][prop]; return undefined; } // for-each loop example Array.prototype.find = function (prop){ for (var i in this) if ( this.hasOwnProperty(i) && typeof this[i][prop] !== "undefined" ) return this[i][prop]; return undefined; } 

Using


 console.log( arr.find('key2') ); // 'value2' console.log( arr.find('key3') ); // undefined 
+1
source

Heyas. You can use the lodash library .reduce () or .transform () functions to implement this function. Lodash is more modular than underscore (Underscore about 5kb, Lodash about 17kb), but usually easier because you only include the modules you need (see https://news.ycombinator.com/item?id = 9078590 for discussion). For this demonstration, I import the entire module (usually not a problem on the backend): I wrote these fragments for any script that handles both numeric and non-numeric arguments.

https://lodash.com/docs#reduce

https://lodash.com/docs#transform

Pull in lodash:

 var _ = require('lodash'); 

_. reduce () in where:

 var delim = ' WHERE ', where = _.isEmpty(options) ? '' : _.reduce(options, function(r, v, k) { var w = r + delim + k + '=' + (_.isNumber(v) ? v : ("'" + v + "'")); delim = ' AND '; return w; }, ''); 

_. transform () to where:

 var where = _.isEmpty(options) ? '' : ' WHERE ', delim = ''; _.transform(options, function(r, v, k) { where = where + delim + k + '=' + (_.isNumber(v) ? v : ("'" + v + "'")); delim = ' AND '; }); 

Hope this helps.

0
source

This work will answer upstairs. Here is one version of its liner using lodash (same as underlining in most cases):

 var result = _.filter(data, _.partialRight(_.has, 'key2')); 

In lodash, select only an alias for the filter. I pass it a data array filled with objects. I use _.has as a filter function, as it does exactly what we want: check if the property exists.

_.has expects two arguments:

 _.has(object, path) 

Since _.has expects two arguments, and I know that one of them is always constant (path argument). I use the _.partialRight function to add the key2 constant. _.partialRight returns a new function that expects one argument: the object to check. The new function checks if obj.key2 .

0
source

Try the following:

 var parsedJSON = JSON.parse(stringJSON); var value = parsedJSON['key2']; 
-one
source

All Articles