0

I have a url array parameters that has number values. I've do research but I didn't find nothing.

So here is my array url: units[1]=5&grade[1]=1.25&units[2]=3&grade[2]=1.50&units[3]=2&grade[3]=2.50

What I need is to get the lowest value of units and grade

In short, it will display: 1.25 as lowest grade and 2 as lowest unit

Is it possible to do it with that kind of string? Thank you!

mplungjan
  • 169,008
  • 28
  • 173
  • 236

2 Answers2

1

I used the parseParams from here via jquery.parseparams.js

/* helper functions */

(function ($) { var re = /([^&=]+)=?([^&]*)/g; var decode = function (str) { return decodeURIComponent(str.replace(/\+/g, ' ')); }; $.parseParams = function (query) { function createElement(params, key, value) { key = key + ''; if (key.indexOf('.') !== -1) { var list = key.split('.'); var new_key = key.split(/\.(.+)?/)[1]; if (!params[list[0]]) params[list[0]] = {}; if (new_key !== '') { createElement(params[list[0]], new_key, value); } else console.warn('parseParams :: empty property in key "' + key + '"'); } else if (key.indexOf('[') !== -1) { var list = key.split('['); key = list[0]; var list = list[1].split(']'); var index = list[0]; if (index == '') { if (!params) params = {}; if (!params[key] || !$.isArray(params[key])) params[key] = []; params[key].push(value); } else { if (!params) params = {}; if (!params[key] || !$.isArray(params[key])) params[key] = []; params[key][parseInt(index)] = value; } } else { if (!params) params = {}; params[key] = value; } } query = query + ''; if (query === '') query = window.location + ''; var params = {}, e; if (query) { if (query.indexOf('#') !== -1) { query = query.substr(0, query.indexOf('#')); } if (query.indexOf('?') !== -1) { query = query.substr(query.indexOf('?') + 1, query.length); } else return {}; if (query == '') return {}; while (e = re.exec(query)) { var key = decode(e[1]); var value = decode(e[2]); createElement(params, key, value); } } return params; }; })(jQuery);

Array.prototype.min = function() {
  return Math.min.apply(null, this);
};
function getArr(obj) {
    return Object.keys(obj).map(function (key) { return obj[key]; })
}

/* Actual code */

// change the string to for example location.href to get your URL
var url = "http://example.com/?units[1]=5&grade[1]=1.25&units[2]=3&grade[2]=1.50&units[3]=2&grade[3]=2.50";
var arrs = $.parseParams(url); // now the arrays are stored as arrs={ unit:[], grade:[] }

var unitArr = getArr(arrs.units),gradeArr=getArr(arrs.grade); // extact the values only

console.log(unitArr.min(),gradeArr.min()); // grab the min of each
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
mplungjan
  • 169,008
  • 28
  • 173
  • 236
-1

Steps

Parse URL arguments to JavaScript object

First step would be to parse this parameter to an object easy to manipulate. Other questions asked how to do this, so you will only have to search a bit to find which is best for you. The one I found first uses JQuery BBQ's deparam function, which is the reverse for JQuery's param function. Parsing your URL parameter this way is easy:

var params = $.deparam(urlParam);

Extract minimums from each array

Another answer (same method used here) showed how to add a function to the array prototype which would return the minimal element of the array.

Array.prototype.min = function() {
  return Math.min.apply(null, this);
};

If you go with it, getting the lowest element of your array is simple:

var lowestUnits = params.units.min();

Alternately, if you do not wish to add it to your prototype, you could call it explicitely:

var lowestUnits = Math.min.apply(null, params.units);

Full code example

Concatenating the samples from other answers, here is would be how to go1:

Adding a min function to Array's prototype

Array.prototype.min = function() {
  return Math.min.apply(null, this);
};

// ...

var params = $.deparam('units[1]=5&grade[1]=1.25&units[2]=3&grade[2]=1.50&units[3]=2&grade[3]=2.50');
var lowestGrade = params.grade.min();
var lowestUnits = params.units.min();

Leaving prototype as is

var params = $.deparam('units[1]=5&grade[1]=1.25&units[2]=3&grade[2]=1.50&units[3]=2&grade[3]=2.50');
var lowestGrade = Math.min.apply(null, params.grade);
var lowestUnits = Math.min.apply(null, params.units);

1: Once again, JQuery BBQ's deparam function is not the only available solution.

Community
  • 1
  • 1
Chop
  • 4,267
  • 5
  • 26
  • 58