- 0
Javascript array containsObj extension
-
Recently Browsing 0 members
- No registered users viewing this page.
-
Similar Content
-
Microsoft explains how to download and install the new "10 times faster" TypeScript
By hellowalkman,
- microsoft
- typescript
- (and 7 more)
- 0 replies
- 0 views
-
JavaScript devs beware: this very popular NPM package has been compromised by attackers
By David Uzondu,
- javascript
- axios
- (and 2 more)
- 0 replies
- 0 views
-
Microsoft is bringing some interesting features to Teams and Edge soon
By Usama Jawad96,
- microsoft
- microsoft 365
- (and 8 more)
- 0 replies
- 0 views
-
AI's impact on programming language popularity revealed
By zikalify,
- ieee
- programming languages
- (and 7 more)
- 3 replies
- 0 views
-
Over 300 npm packages compromised by self-replicating worm
By David Uzondu,
- javascript
- npm
- (and 3 more)
- 8 replies
- 0 views
-
Question
sathenzar
Hey guys, was writing a test project to refresh myself in a few areas and I came across the need for two extensions in javascript I wrote. They might not be the most efficient methods in the world but if you need them feel free to use them!
/* Returns the first object in the array that matches the designated values (json or single value accepted). * If the val parameter is not valid json it can only contain one value. Otherwise it will need to be json * that contains a 'matches' element that is an array of values to test against the property. * Can be used like so: * var arr = new Array(); * arr.push(new google.maps.LatLng(100.0, 200.9)); * arr.push(new google.maps.LatLng(300.0, 210.3)); * var searchElem = arr.containsObj("lat", "{\"matches\":[\"300.0\", \"100.0\"]}"); * In this example searchElem would return the first object it found so it would return the second element in the array. */ if (!Array.prototype.containsObj) { Array.prototype.containsObj = function(prop, val) { if (this == null) return null; var t = Object(this); if (t.length === 0) return null; try { var jsonStr = JSON.parse(val); for (var i = 0; i < t.length; i++) { if (t[i] != null && t[i] != undefined) { for (var i2 = 0; i2 < jsonStr["matches"].length; i2++) { var nVal = jsonStr["matches"][i2]; //console.log("t[" + i + "][" + prop + "] = " + t[i][prop] + " nVal = " + nVal); if (t[i][prop] == nVal) return t[i]; } } } } catch(e) { for (var i = 0; i < t.length; i++) { if (t[i] != null && t[i] != undefined) { if (t[i][prop] == val) return t[i]; } } } return null; } /* Returns the first object in the array that matches the designated property/value pairs in the json 'matches' section. * So you could call it like so: * var arr = new Array(); * arr.push(new google.maps.LatLng(100.0, 200.9)); * arr.push(new google.maps.LatLng(300.0, 210.3)); * var searchElem = arr.containsObjVals("{\"matches\":[{\"propName\":\"lat\", \"propVal\":\"100.0\"}, {\"propName\":\"lng\", \"propVal\":\"200.9\"}]}"); * assuming the LatLng object contains the properties named 'lat' and 'lng' (without the quotes of course). */ Array.prototype.containsObjVals = function (params) { //console.log("params: " + params); if (this == null) return null; var t = Object(this); if (t.length === 0) return null; try { var jsonStr = JSON.parse(params); for (var i = 0; i < t.length; i++) { //console.log("now looping through i: " + i); if (t[i] != null && t[i] != undefined) { //console.log("t[i] is not null"); for (var i2 = 0; i2 < jsonStr["matches"].length; i2++) { var prop = jsonStr["matches"][i2].propName; var val = jsonStr["matches"][i2].propVal; //console.log("matches: " + i2 + " prop: " + prop + " " + val); if (t[i][prop] == val) return t[i]; } } } } catch (e) { //console.log("Error while parsing json. Error info: " + e); return null; } return null; }Link to comment
https://www.neowin.net/forum/topic/1156182-javascript-array-containsobj-extension/Share on other sites
0 answers to this question
Recommended Posts