JavaScript function between two numbers in a list? - arrays
I would like to create a function which will retrieve the result from following list
var Fedsy = [
{min : 0, max : 2300 , must:0, percent:0 , excess:0},
{min : 2300, max : 11525 , must:0, percent:10 , excess:2300},
{min : 11525, max : 39750 , must:922.50, percent:15 , excess:11525},
{min : 39750, max : 93050 , must:5156.25, percent:25 , excess:39750},
{min : 93050, max : 191600 , must:18481.25, percent:28 , excess:93050},
{min : 191600, max : 413800 , must:46075.25, percent:33 , excess:191600},
{min : 413800, max : 415500 , must:119401.25, percent:35 , excess:413800},
{min : 415500, max : 1000000000 , must:119996.25, percent:39.6 , excess:415500}
];
Now I want to pull the row from above list if "inputNum" is between "min" & "max"
var result= function(inputNum){
///..???
}
Ex:if "inputNum"= 40000 the function should return
{min : 39750, max : 93050 , must:5156.25, percent:25 , excess:39750}
Since you tagged underscore.js, I understand that you want to solve the problem using that library.
As such, you can use the function _.find to find the first value that matches a condition. Think of it like a filter but just looking into the first one:
function findRange(inputNum) {
return _.find(Fedsy, function(f) {
return f.min <= inputNum && f.max >= inputNum;
});
}
The first item from Fedsy (f) to return true in the iterator function will be the value that underscore returns.
You may see it more in detail and with more reference functions at underscore.js site: http://underscorejs.org/
What I did:
I created the function you mentioned, it uses pure javascript without any libraries. It loops through each item in the Fedsy array, and looks to see if the value is in the range. If so it returns the row in question.
It also returns as soon as it finds the result not continuing to search the object.
Data
var Fedsy = [
{min : 0, max : 2300 , must:0, percent:0 , excess:0},
{min : 2300, max : 11525 , must:0, percent:10 , excess:2300},
{min : 11525, max : 39750 , must:922.50, percent:15 , excess:11525},
{min : 39750, max : 93050 , must:5156.25, percent:25 , excess:39750},
{min : 93050, max : 191600 , must:18481.25, percent:28 , excess:93050},
{min : 191600, max : 413800 , must:46075.25, percent:33 , excess:191600},
{min : 413800, max : 415500 , must:119401.25, percent:35 , excess:413800},
{min : 415500, max : 1000000000 , must:119996.25, percent:39.6 , excess:415500}
];
Function
function filterArray(inputNum){
var row;
// Foreach item in the array
for(rownum in Fedsy){
var row = Fedsy[rownum];
// If "inputNum" is greater than or equal to "row.min" & less than "row.max"
if(inputNum >= row.min && inputNum < row.max){
return row;
}
}
}
Usage
var result = filterArray(3); // returns first row
console.log(result.must); //0
If you just want the first matching row, then and application of underscore/lodash's _.find https://lodash.com/docs#find might be what you want:
var result = (function(inputNum){
return _.find(Fedsy, function(item){
return inputNum>item.min && inputNum<item.max;
});
})();
That will immediately run an anonymous function that accepts the inputNum and uses it in another anonymous function that _.find uses to check each item. The advantage of _.find is that it will stop looking as soon as it hits a result.
Ideally though, you'd write things in a more generalized way, so that you could specify your filter and what you're filtering separately, so maybe by doing something like this:
var minmaxFilterFunc = function(inputNum){
return function(item){
return inputNum>item.min && inputNum<item.max;
};
};
Now, using minmaxFilterFunc, you can create a function that will return true for a particular inputNum. Now you can use find directly, creating the proper filter function in one line. Easier to read, and also easier to reuse for filtering other collections on different numbers later.
var result = _.find(Fedsy, minMaxFilterFunc(inputNum));
Related
How can I use my plotted data in further calculations?
So I have this code that draws zigzag-alike line indicator("Custom zigzag", overlay=true) bullish(at) => open[at] < close[at] bearish(at) => open[at] > close[at] break_up() => if close != open at = 1 while close[at] == open[at] at += 1 if bearish(at) and bullish(0) true else false else false break_down() => if close != open at = 1 while close[at] == open[at] at += 1 if bearish(0) and bullish(at) true else false else false u = break_up() d = break_down() plot(u?open[0] : d?open[0] : na, color = color.fuchsia, linewidth = 1, style = plot.style_line, offset=0) I want to use the result in some further calculations, but I dont get it, how can I put the whole u?open[0] : d?open[0] : na in an array? Roughly pushing values to an array variable leads to recalculation on each bar, IMO. Or can I access somehow my own previous plot, since custom script call is not possible?
There are two ways to do it depending on do you want na values as it is or previous calculated values in place of na. You can save the whole thing in a variable and then use that to calculate anything like sma etc. First way //Keeping na value val=u?open[0] : d?open[0] : na s=ta.sma(val,10) plot(s) Second way //Replacing na values with previous values var val=open[0] val:=u?open[0] : d?open[0] : val[1] s=ta.sma(val,10) plot(s)
Sort Numbers after Letters in Angular
I want to sort Letters first and followed by numbers like below: [Austria , France , Germany , 101110 , 124563] This is what i have tried: obj.sort((a,b) => a.text > b.text ? 1 : -1) But it is sorting numbers first and then letters. Any help?
If you are looking for a solution like you want to sort string at the first portion of array and numbers at the last of array, just make sure that you are following this procedure. Never compare a string with a number. If both a and b are string or both are numbers just compare both with a > b and return 1 or -1 depending on the result of comparision. The last else block corresponds to the comparision between a number and a string. The return value determines the location of the numbers in the array. If either one of them is not a number, return -1 or 1 depending on the requirement. Since you want the numbers at the end of your array, return -1 if the value is not a number. If you send 1 instead, the numbers will take the first posion in he output array. const data = [101110 , 124563 , 'France' , 'Austria', 'Germany']; const output = data.sort((a,b) => { if ( (isNaN(a) && isNaN(b)) || (!isNaN(a) && !isNaN(b)) ) { // Both are strings // OR // Both are numbers return a > b ? 1 : -1; } else { // One of them is a number return isNaN(a) ? -1 : 1; } }); console.log(output); Much Simplified version const data = [101110, 'France', 'Austria', 124563, 'Germany']; const checkOfSamePattern = (a, b) => (isNaN(a) && isNaN(b)) || (!isNaN(a) && !isNaN(b)); const output = data.sort((a, b) => checkOfSamePattern(a, b) ? a > b ? 1 : -1 : isNaN(a) ? -1 : 1); console.log(output);
Replace values in an ArraySlice<Float>
I tried to filter an ArraySlice<Float>to replace the values below -5000 and higher 5000 to -5000 and 5000. I can apply one condition but how I can apply both condition in a same time? I want to add that if the value is higher than 5000, replace it with 5000, how I can do that? filteredData.append(contentsOf: data.map({ return $0 < -5000 ? -5000 : $0}))
You can just use an if-else statement to check both conditions let filteredData = floatData.map { value -> Float in if value > 5000 { return 5000 } else if value < -5000 { return -5000 } else { return value } } Or you can use the combination of min and max to limit your values to a specified range. let filteredData = floatData.map { value -> Float in max(min(value, 5000), -5000) }
According to the task conditions, let's do it: filteredData.append(contentsOf: data.map({ $0 < -5000 ? -5000 : $0 > 5000 ? 5000 : $0}))
JSONata sort / order by of an array
I want to order an array. The JSONata expression below has an incoming array as follows. [{"id":"Air-1a", "Controller":"ESP62", "Cntr-TaskNo":10, "Cntr-GPIO":13, "name":"Air", "valueName":"Humidity", "Sensor":"DHT22", (and many other key pairs)}, {next object}, ...] I then transform the array with the following JSONata expression: payload.( { "Controller" : $.Controller, "Cntr-TaskNo": $.CntrDef.TaskNo, "Cntr-GPIO" : $.CntrDef.GPIO, "name" : $.name, "valueName" : $.valueName, "Sensor" : $.Sensor, "id" : $.id } ) But now I want to - in the same JSONata expression, sort on firstly the Controller, and then the GPIO. To tried with the Controller only first. I tried: payload.( { $sort("Controller",function($l, $r){$l.Controller > $r.Controller}) : $.Controller , "Cntr-TaskNo": $.CntrDef.TaskNo, "Cntr-GPIO" : $.CntrDef.GPIO, "name" : $.name, "valueName" : $.valueName, "Sensor" : $.Sensor, "id" : $.id } ) As well as trying to add the sort function at the end with the ~> chaining command. I also tried the order-by operator. Could anyone point me in the right direction? //---------- The new flow with the changed 'ESP62' to '-' that does not work: [{"id":"874b0c77.f87418","type":"inject","z":"6f27a311.d135bc","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":200,"y":180,"wires":[["8c196590.c20638"]]},{"id":"8c196590.c20638","type":"change","z":"6f27a311.d135bc","name":"Dataset","rules":[{"t":"set","p":"payload","pt":"msg","to":"[{\"id\":\"Air-1a\",\"Controller\":\"ESP62\",\"CntrTaskNo\":10,\"CntrGPIO\":13,\"name\":\"Air\",\"valueName\":\"Humidity\",\"Sensor\":\"DHT22\",\"aaa\":\"111\",\"bbb\":\"222\",\"ccc\":\"333\"},{\"id\":\"Air-2a\",\"Controller\":\"ESP72\",\"CntrTaskNo\":11,\"CntrGPIO\":14,\"name\":\"Air\",\"valueName\":\"Humidity\",\"Sensor\":\"DHT22\",\"aaa\":\"444\",\"bbb\":\"555\",\"ccc\":\"666\"},{\"id\":\"Air-1a\",\"Controller\":\"ESP62\",\"CntrTaskNo\":2,\"CntrGPIO\":9,\"name\":\"Air\",\"valueName\":\"Humidity\",\"Sensor\":\"DHT22\",\"aaa\":\"777\",\"bbb\":\"888\",\"ccc\":\"999\"},{\"id\":\"Air-1a\",\"Controller\":\"-\",\"CntrTaskNo\":10,\"CntrGPIO\":12,\"name\":\"Air\",\"valueName\":\"Humidity\",\"Sensor\":\"DHT22\",\"aaa\":\"777\",\"bbb\":\"888\",\"ccc\":\"999\"}]","tot":"json"}],"action":"","property":"","from":"","to":"","reg":false,"x":360,"y":180,"wires":[["13981162.14e28f"]]},{"id":"c8a256a5.a170c8","type":"debug","z":"6f27a311.d135bc","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":690,"y":180,"wires":[]},{"id":"13981162.14e28f","type":"change","z":"6f27a311.d135bc","name":"Jsonata $sort","rules":[{"t":"set","p":"payload","pt":"msg","to":"($sort(payload,function($l , $r){$l.Controller > $r.Controller}) ; \t$sort(payload,function($l , $r){$l.CntrGPIO > $r.CntrGPIO}))","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":520,"y":180,"wires":[["c8a256a5.a170c8"]]}]
I suggest first sorting the dataset and afterward transform the already sorted array of objects. The transformation is trivial and you want to know how to sort, so I show below one possible solution. It uses an expression with two concatenated $sort functions. Edited after a better understanding of the requirement. I tested successfully a Node-RED flow using this expression in a change node: ($a := $sort(payload,function($l , $r){$l.Controller > $r.Controller}) ; $sort($a,function($l , $r){(($l.Controller = $r.Controller) and ($l.CntrGPIO > $r.CntrGPIO))})) Flow (contain dataset set hardcoded): [{"id":"a7814b7e.3adeb8","type":"tab","label":"Flow 4","disabled":false,"info":""},{"id":"8bf10833.c71748","type":"inject","z":"a7814b7e.3adeb8","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":140,"y":140,"wires":[["9e365564.edca08"]]},{"id":"9e365564.edca08","type":"change","z":"a7814b7e.3adeb8","name":"Dataset","rules":[{"t":"set","p":"payload","pt":"msg","to":"[{\"id\":\"Air-1a\",\"Controller\":\"ESP62\",\"CntrTaskNo\":10,\"CntrGPIO\":13,\"name\":\"Air\",\"valueName\":\"Humidity\",\"Sensor\":\"DHT22\",\"aaa\":\"111\",\"bbb\":\"222\",\"ccc\":\"333\"},{\"id\":\"Air-2a\",\"Controller\":\"ESP72\",\"CntrTaskNo\":11,\"CntrGPIO\":14,\"name\":\"Air\",\"valueName\":\"Humidity\",\"Sensor\":\"DHT22\",\"aaa\":\"444\",\"bbb\":\"555\",\"ccc\":\"666\"},{\"id\":\"Air-1a\",\"Controller\":\"ESP62\",\"CntrTaskNo\":2,\"CntrGPIO\":9,\"name\":\"Air\",\"valueName\":\"Humidity\",\"Sensor\":\"DHT22\",\"aaa\":\"777\",\"bbb\":\"888\",\"ccc\":\"999\"},{\"id\":\"Air-1a\",\"Controller\":\"-\",\"CntrTaskNo\":10,\"CntrGPIO\":12,\"name\":\"Air\",\"valueName\":\"Humidity\",\"Sensor\":\"DHT22\",\"aaa\":\"777\",\"bbb\":\"888\",\"ccc\":\"999\"}]","tot":"json"}],"action":"","property":"","from":"","to":"","reg":false,"x":300,"y":140,"wires":[["762f6421.074fec"]]},{"id":"f827bddb.c9acd","type":"debug","z":"a7814b7e.3adeb8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":630,"y":140,"wires":[]},{"id":"762f6421.074fec","type":"change","z":"a7814b7e.3adeb8","name":"Jsonata $sort","rules":[{"t":"set","p":"payload","pt":"msg","to":"($a := $sort(payload,function($l , $r){$l.Controller > $r.Controller}) ; $sort($a,function($l , $r){(($l.Controller = $r.Controller) and ($l.CntrGPIO > $r.CntrGPIO))}))","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":460,"y":140,"wires":[["f827bddb.c9acd"]]}] also tested in Jsonata exerciser: http://try.jsonata.org/S1IlT3y-E
You can sort the array using the following expression: payload^(Controller, CntrDef.GPIO) The order-by operator ^ will sort the array, first by increasing value of Controller, then by increasing value of CntrGPIO. You can then transform each object within that array payload^(Controller, CntrDef.GPIO).{ "Controller" : Controller, "Cntr-TaskNo": CntrDef.TaskNo, "Cntr-GPIO" : CntrDef.GPIO, "name" : name, "valueName" : valueName, "Sensor" : Sensor, "id" : id }
Merge random elements of array/split into chunks
How can I split array into chunks with some special algorithm? E.g. I need to shorten array to the size of 10 elements. If I have array of 11 elements, I want two next standing elements get merged. If I have array of 13 elements, I want three elements merged. And so on. Is there any solution? Sample #1 var test = ['1','2','3','4','5','6','7','8','9','10','11']; Need result = [['1'],['2'],['3'],['4'],['5|6'],['7'],['8'],['9'],['10'],['11']] Sample #2 var test = ['1','2','3','4','5','6','7','8','9','10','11','12','13']; Need result = [['1|2'],['3'],['4'],['5'],['6'],['7|8'],['9'],['10'],['11'],['12|13']] Thank you in advance.
The following code most probably does what you want. function condense(a){ var source = a.slice(), len = a.length, excessCount = (len - 10) % 10, step = excessCount - 1 ? Math.floor(10/(excessCount-1)) : 0, groupSize = Math.floor(len / 10), template = Array(10).fill() .map((_,i) => step ? i%step === 0 ? groupSize + 1 : i === 9 ? groupSize + 1 : groupSize : i === 4 ? groupSize + 1 : groupSize); return template.map(e => source.splice(0,e) .reduce((p,c) => p + "|" + c)); } var test1 = ['1','2','3','4','5','6','7','8','9','10','11'], test2 = ['1','2','3','4','5','6','7','8','9','10','11','12','13','14','15','16','17','18','19','20','21']; console.log(condense(test1)); console.log(condense(test2));
A - Find the difference and create thus many random numbers for merge and put in array B - loop through initial numbers array. B1 - if iterator number is in the merge number array (with indexOf), you merge it with the next one and increase iterator (to skip next one as it is merged and already in results array) B1 example: int mergers[] = [2, 7, 10] //in loop when i=2 if (mergers.indexOf(i)>-1) { //true String newVal = array[i]+"|"+array[i+1]; //will merge 2 and 3 to "2|3" i++; //adds 1, so i=3. next loop is with i=4 } C - put new value in results array
You can try this code jQuery(document).ready(function(){ var test = ['1','2','3','4','5','6','7','8','9','10','11','12','13','14','15','16']; var arrays = []; var checkLength = test.length; var getFirstSet = test.slice(0,10); var getOthers = test.slice(10,checkLength); $.each( getFirstSet, function( key,value ) { if(key in getOthers){ values = value +'|'+ getOthers[key]; arrays.push(values); }else{ arrays.push(value); } }); console.log(arrays); }); <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>