Javascript Array grouping category - javascript

I have a problem with grouping an array of numeric value:
I have values in an array like this
var numb = [5,10,11,6,7,18,1,8,2,1,15,12,4,5,3,4,6,7,15,20];
that are then sorted into ascending numerical order
var sortedNumb = [1,1,2,3,4,4,5,5,6,6,7,7,8,10,11,12,15,15,18,20];
Now I want to create a group of numbers like
1-4 , 5-8 , 9-12 , 13-16 , 17-20
Is it possible to create groups dynamically, like that?

// important code
var numberToGroupOn = 4;
var numb = [5,10,11,6,7,18,1,8,2,1,15,12,4,5,3,4,6,7,15,20];
var srt = numb.slice(0).sort(function(a, b) { return a - b; });
var groupCount = Math.ceil(srt[srt.length-1] / numberToGroupOn);
var grps = {};
for(var i = 1; i <= groupCount; i++)
{
grps[((i*numberToGroupOn)-(numberToGroupOn-1)).toString() + '-' + (i*numberToGroupOn).toString()] =
srt.filter(function(a) {return (a <= i*numberToGroupOn) && (a >= (i*numberToGroupOn)-(numberToGroupOn-1))});
}
// unimportant code to see output in SO snippet
var output = '';
for(var key in grps)
{
output += key + ': ' + grps[key]+'<br>';
}
document.write(output);
This figures out the number of groups and then builds a dictionary of the groups using Array.prototype.filter.
It only works with positive numbers.

Assuming that 1-4, 5-8, 9-12, 13-16, 17-20 grouping means that you want 5 groups, the first one (1-4) containing all the numbers within the [1, 4] interval; the second one (5-8) containing all the numbers within the [5, 8] interval, and so on.
// numbers and intervals must be in ascending order
var numb = [5,10,11,6,7,18,1,8,2,1,15,12,4,5,3,4,6,7,15,20];
// 1-4 , 5-8 , 9-12 , 13-16 , 17-20
var intervals = [4, 8, 12, 16, 20];
numb.sort(function (a, b) {
return a - b;
});
var groups = [];
var j = 0;
for (var i = 0; i < intervals.length; i++) {
var group = [];
while (numb[j] <= intervals[i]) {
group.push(numb[j]);
j++;
}
groups.push(group);
}
console.log(groups);
The output:
[ [ 1, 1, 2, 3, 4, 4 ],
[ 5, 5, 6, 6, 7, 7, 8 ],
[ 10, 11, 12 ],
[ 15, 15 ],
[ 18, 20 ] ]
EDIT: After reading the comment about calculating the intervals based on the max number in the array.
var numb = [5,10,11,6,7,18,1,8,2,1,15,12,4,5,3,4,6,7,15,20];
numb.sort(function (a, b) {
return a - b;
});
var max = numb[numb.length - 1];
// Five groups based on the max value of the array
var increment = max / 5;
var groups = [];
var j = 0;
for (var i = increment; i <= max; i += increment) {
var group = [];
while (numb[j] <= i) {
group.push(numb[j]);
j++;
}
groups.push(group);
}
console.log(groups);
Output:
[ [ 1, 1, 2, 3, 4, 4 ],
[ 5, 5, 6, 6, 7, 7, 8 ],
[ 10, 11, 12 ],
[ 15, 15 ],
[ 18, 20 ] ]

Code : Assuming sortedNumb is not empty :)
var sortedNumb = [1,1,2,3,4,4,5,5,6,6,7,7,8,10,11,12,15,15,18,20];
var groups = [[sortedNumb[0]]];
var lastfirstValueInArray = sortedNumb[0];
var i = 1;
var j = 0;
while (i < sortedNumb.length)
{
if (sortedNumb[i] >= lastfirstValueInArray+4 || (sortedNumb[i]%4 == 1 && sortedNumb[i] != lastfirstValueInArray))
{
j++;
lastfirstValueInArray = 1+j*4;
groups[j] = [];
}
groups[j][groups[j].length] = sortedNumb[i];
i++;
}
console.log(groups);
Output :
[Array[6], Array[7], Array[3], Array[2], Array[2]]
Edit :
You seemed to want a range of group of 4, if you want N, just create a function taking it as parameter, and replace all 4 by N in code =)

Related

Array of numbers to batches of given sum

I've a JavaScript array and sum as input
array = [4,8,2,4,2,2,8,12,4,2, 2]
sum = 12 // all the time sum will be 12
I want 2d array, the numbers in batches should be sum equals or less than 12
The output array should look like
[
[4,8],
[2,4,2,2,2],
[8, 4],
[12],
[2]
]
4 + 8 = 12
2 + 4 + 2 + 2 + 2 = 12
...
2 is left at the end
Other examples
1) array = [6,5,3,3,3,2,2,2,2]
sum = 12
output: [ [6,3,3], [5,3,2,2], [2,2] ]
One the number is allotted to subset, it should not used to other subset
remaining numbers can be added to the last but sum should be less than 12, else add one more array and add remaining ones
The input array can have any integer from 1 - 12
How can I get the output I want?
Try this function. I commented the code as much as possible to clarify it.
const example1 = [4, 8, 2, 4, 2, 2, 8, 12, 4, 2, 2];
const example2 = [6, 5, 3, 3, 3, 2, 2, 2, 2];
const example3 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12];
const example4 = [5,12,3,4,4,1,1,1,5,8,12,6,9,11,6];
const example5 = [4, 2, 1, 2, 3, 3, 5, 7, 8, 9];
const lookAhead = function(array, searchFor) {
return array.some(val => {
return val <= searchFor;
});
}
function findPairs(inputArray) {
// First sort array in descending order
inputArray.sort((a, b) => b - a);
console.log("input", JSON.stringify(inputArray.slice(0)));
// set variables
const pairArray = [];
const max = 12;
inputArray.forEach(function(num, index) {
// when looping the array we will replace values with null once completed,
// Therefore if value is null no need to go futher
if (num == null)
return;
// initiate pair value with current number
const pair = [num];
// set it to null in input array
inputArray[index] = null;
// if number equals to max (ie. 12) no need to go futher
if (num == max) {
pairArray.push(pair);
return;
}
let total = num;
// Loop through array again to see matching numbers
for (let i = 0; i < inputArray.length; i++) {
// Don't go futher if it is a null value
if (inputArray[i] == null)
continue;
const add = total + inputArray[i];
/* if the total is less than max (12) then we check if we have an edge case
* For example in an array like [6, 5, 3, 3, 3], 6 + 5 is 11 but in next loops we
* will not find any "1" to get to 12. Therefore we escape this iteration and check
* next numbers. In this case the result would be 6 + 3 + 3
*/
if (add < max) {
const found = lookAhead(inputArray.slice(i), max - add);
if (found) {
pair.push(inputArray[i]);
total = add;
inputArray[i] = null;
}
} else if (add == max) {
// The addition is equals to max. Push the number and set it to null in input array
pair.push(inputArray[i]);
inputArray[i] = null;
total = 0;
break;
}
}
// Push pair array from this iteration to pairArray
pairArray.push(pair);
});
console.log("output", JSON.stringify(pairArray));
console.log("-------");
}
findPairs(example1);
findPairs(example2);
findPairs(example3);
findPairs(example4);
findPairs(example5);
A little complex to understand but here you go...
let originalArray = [7, 7, 7, 7, 7]
let sum = 12;
let twoDiArray = [];
let CalculateSum = (array, element) => {
array = [...array, element]
return array.reduce((x, y) => {
return x + y;
})
}
twoDiArray.push([]);
originalArray.forEach(element => {
for (let i = 0; i < twoDiArray.length; i++) {
if (CalculateSum(twoDiArray[i], element) <= 12) {
twoDiArray[i].push(element);
break;
} else {
if (twoDiArray.length - 1 === i) {
twoDiArray.push([element]);
break;
}
}
}
})
console.log(twoDiArray)
Here you... I will keep both answers open for future use of others...
let originalArray = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
let sum = 12;
let twoDiArray = [];
let CalculateSum = (array, element) => {
array = [...array, element]
return array.reduce((x, y) => {
return x + y;
})
}
twoDiArray.push([originalArray[0]]);
originalArray.splice(0, 1);
do {
originalArray.forEach((element, index) => {
for (let i = 0; i < twoDiArray.length; i++) {
let summ = CalculateSum(twoDiArray[i], element);
if (summ === 12) {
twoDiArray[i].push(element);
originalArray.splice(index, 1);
break;
} else {
if (index === originalArray.length - 1) {
if (CalculateSum(twoDiArray[twoDiArray.length - 1], originalArray[0]) <= 12) {
twoDiArray[twoDiArray.length - 1].push(originalArray[0]);
break;
} else {
twoDiArray.push([originalArray[0]]);
}
originalArray.splice(0, 1);
}
}
}
})
}
while (originalArray.length > 0);
console.log(twoDiArray)

How do I make an array add up?

I want to add an array add up.
Let's say there's an array of [7, 1, 21, 70]
On a an array index of 0, it would just be 7. On an array index of 1, I want it to be 7 + 1 (8). Array index 2, 7 + 1 + 21 (29). Array index 3 7 + 1 + 21 + 70 (99).
This is my current code:
var pot = {
'id': 1,
'name': ['stalin', 'hitler', 'mao', 'kim jong-il'],
'amount': [50, 10, 150, 500],
'percentages': new Array()
}
var random = Math.random()*100;
var random = String(random).split(".")[0];
console.log(random);
function potTotal(amounts) {
var sum = 0;
for (var key in amounts) {
sum += pot['amount'][key];
}
return sum;
}
function potPercentage(total, amounts) {
for (var key in amounts) {
var percentage = amounts[key] / total * 100;
var percentage = String(percentage).split(".")[0];
var percentage = Number(percentage);
pot['percentages'].push(percentage);
}
}
potPercentage(potTotal(pot['amount']), pot['amount']);
function ranging(total, percentages) {
console.log(percentages);
for(var i = 0; percentages < i; i++) {
console.log(percentages[i]);
}
}
//[7, 1, 21, 70]
ranging(random, pot['percentages']);
for (var key in pot['percentages']) {
console.log(key);
console.log(pot['percentages'][key]);
}
The results of which return:
69
[ 7, 1, 21, 70 ]
0
7
1
1
2
21
3
70
reduce is the function defined to do this kind tasks.
const arr = [7, 1, 21, 70].reduce((acc, el, i) => [...acc, (acc[i-1] || 0) + el], []);
console.log(arr);
Basically you are looking for prefix sum method, try the following:
var arr = [7, 1, 21, 70];
for(var i = 1; i < arr.length; i++){
arr[i] += arr[i-1];
}
console.log(arr);
For Reference : Prefix sum
A simple forEach() will do for you:
var arr = [7, 1, 21, 70];
arr.forEach((item, index) => {
if(index - 1 > -1){
arr[index] += arr[index-1];
}
});
console.log(arr);
Just iterate with a for loop and add the previous item
let items = [1,2,3,4,5,6,7];
for (let i = 1; i < items.length; i++)
items[i] = items[i] + items[i-1];
You could use the reduce method on the array so that you don't bother managing index while doing the computation and also reduce will return you a brand new array, with the added benefit of not destroying your original array
var originalArray = [7, 1, 21, 70];
var addUp = (acc, curr, index) => {
if (index === 0) acc.push(curr);
else acc.push(acc[index-1] + curr);
return acc;
}
var computedArray = originalArray.reduce(addUp, []);
console.log(computedArray);
I like this as this is so easy to read and does not mutate the original array.
Mapping over an array gives you access to each item in that array one by one (just like a foreach loop) but it also creates a new array containing the return value from the callback.
function addConsecutives(nums) {
let sum = 0;
return nums.map(num => sum += num)
}
console.log(addConsecutives([7, 1, 21, 70]));

How to push elements of a 1d array into a 9x9 array in javascript? [duplicate]

Imagine I have an array:
A = Array(1, 2, 3, 4, 5, 6, 7, 8, 9);
And I want it to convert into 2-dimensional array (matrix of N x M), for instance like this:
A = Array(Array(1, 2, 3), Array(4, 5, 6), Array(7, 8, 9));
Note, that rows and columns of the matrix is changeable.
Something like this?
function listToMatrix(list, elementsPerSubArray) {
var matrix = [], i, k;
for (i = 0, k = -1; i < list.length; i++) {
if (i % elementsPerSubArray === 0) {
k++;
matrix[k] = [];
}
matrix[k].push(list[i]);
}
return matrix;
}
Usage:
var matrix = listToMatrix([1, 2, 3, 4, 4, 5, 6, 7, 8, 9], 3);
// result: [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
You can use the Array.prototype.reduce function to do this in one line.
ECMAScript 6 style:
myArr.reduce((rows, key, index) => (index % 3 == 0 ? rows.push([key])
: rows[rows.length-1].push(key)) && rows, []);
"Normal" JavaScript:
myArr.reduce(function (rows, key, index) {
return (index % 3 == 0 ? rows.push([key])
: rows[rows.length-1].push(key)) && rows;
}, []);
You can change the 3 to whatever you want the number of columns to be, or better yet, put it in a reusable function:
ECMAScript 6 style:
const toMatrix = (arr, width) =>
arr.reduce((rows, key, index) => (index % width == 0 ? rows.push([key])
: rows[rows.length-1].push(key)) && rows, []);
"Normal" JavaScript:
function toMatrix(arr, width) {
return arr.reduce(function (rows, key, index) {
return (index % width == 0 ? rows.push([key])
: rows[rows.length-1].push(key)) && rows;
}, []);
}
This code is generic no need to worry about size and array, works universally
function TwoDimensional(arr, size)
{
var res = [];
for(var i=0;i < arr.length;i = i+size)
res.push(arr.slice(i,i+size));
return res;
}
Defining empty array.
Iterate according to the size so we will get specified chunk.That's why I am incrementing i with size, because size can be 2,3,4,5,6......
Here, first I am slicing from i to (i+size) and then I am pushing it to empty array res.
Return the two-dimensional array.
The cleanest way I could come up with when stumbling across this myself was the following:
const arrayToMatrix = (array, columns) => Array(Math.ceil(array.length / columns)).fill('').reduce((acc, cur, index) => {
return [...acc, [...array].splice(index * columns, columns)]
}, [])
where usage would be something like
const things = [
'item 1', 'item 2',
'item 1', 'item 2',
'item 1', 'item 2'
]
const result = arrayToMatrix(things, 2)
where result ends up being
[
['item 1', 'item 2'],
['item 1', 'item 2'],
['item 1', 'item 2']
]
How about something like:
var matrixify = function(arr, rows, cols) {
var matrix = [];
if (rows * cols === arr.length) {
for(var i = 0; i < arr.length; i+= cols) {
matrix.push(arr.slice(i, cols + i));
}
}
return matrix;
};
var a = [0, 1, 2, 3, 4, 5, 6, 7];
matrixify(a, 2, 4);
http://jsfiddle.net/andrewwhitaker/ERAUs/
Simply use two for loops:
var rowNum = 3;
var colNum = 3;
var k = 0;
var dest = new Array(rowNum);
for (i=0; i<rowNum; ++i) {
var tmp = new Array(colNum);
for (j=0; j<colNum; ++j) {
tmp[j] = src[k];
k++;
}
dest[i] = tmp;
}
function matrixify( source, count )
{
var matrixified = [];
var tmp;
// iterate through the source array
for( var i = 0; i < source.length; i++ )
{
// use modulous to make sure you have the correct length.
if( i % count == 0 )
{
// if tmp exists, push it to the return array
if( tmp && tmp.length ) matrixified.push(tmp);
// reset the temporary array
tmp = [];
}
// add the current source value to the temp array.
tmp.push(source[i])
}
// return the result
return matrixified;
}
If you want to actually replace an array's internal values, I believe you can call the following:
source.splice(0, source.length, matrixify(source,3));
This a simple way to convert an array to a two-dimensional array.
function twoDarray(arr, totalPerArray) {
let i = 0;
let twoDimension = []; // Store the generated two D array
let tempArr = [...arr]; // Avoid modifying original array
while (i < arr.length) {
let subArray = []; // Store 2D subArray
for (var j = 0; j < totalPerArray; j++) {
if (tempArr.length) subArray.push(tempArr.shift());
}
twoDimension[twoDimension.length] = subArray;
i += totalPerArray;
}
return twoDimension;
}
const arr = [1, 2, 3, 4, 5, 6, 7, 8, 9];
twoDarray(arr, 3); // [ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ] ]
function changeDimension(arr, size) {
var arrLen = arr.length;
var newArr = [];
var count=0;
var tempArr = [];
for(var i=0; i<arrLen; i++) {
count++;
tempArr.push(arr[i]);
if (count == size || i == arrLen-1) {
newArr.push(tempArr);
tempArr = [];
count = 0;
}
}
return newArr;
}
changeDimension([0, 1, 2, 3, 4, 5], 4);
function matrixify(array, n, m) {
var result = [];
for (var i = 0; i < n; i++) {
result[i] = array.splice(0, m);
}
return result;
}
a = matrixify(a, 3, 3);
function chunkArrToMultiDimArr(arr, size) {
var newArray = [];
while(arr.length > 0)
{
newArray.push(arr.slice(0, size));
arr = arr.slice(size);
}
return newArray;
}
//example - call function
chunkArrToMultiDimArr(["a", "b", "c", "d"], 2);
you can use push and slice like this
var array = [1,2,3,4,5,6,7,8,9] ;
var newarray = [[],[]] ;
newarray[0].push(array) ;
console.log(newarray[0]) ;
output will be
[[1, 2, 3, 4, 5, 6, 7, 8, 9]]
if you want divide array into 3 array
var array = [1,2,3,4,5,6,7,8,9] ;
var newarray = [[],[]] ;
newarray[0].push(array.slice(0,2)) ;
newarray[1].push(array.slice(3,5)) ;
newarray[2].push(array.slice(6,8)) ;
instead of three lines you can use splice
while(array.length) newarray.push(array.splice(0,3));
const x: any[] = ['abc', 'def', '532', '4ad', 'qwe', 'hf', 'fjgfj'];
// number of columns
const COL = 3;
const matrix = array.reduce((matrix, item, index) => {
if (index % COL === 0) {
matrix.push([]);
}
matrix[matrix.length - 1].push(item);
return matrix;
}, [])
console.log(matrix);
Using the Array grouping proposal (currently stage 3), you can now also do something like the following:
function chunkArray(array, perChunk) {
return Object.values(array.group((_, i) => i / perChunk | 0));
}
See also the MDN documentation for Array.prototype.group().
Simplest way with ES6 using Array.from()
const matrixify = (arr, size) =>
Array.from({ length: Math.ceil(arr.length / size) }, (v, i) =>
arr.slice(i * size, i * size + size));
const list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12] ;
console.log(matrixify(list, 3));
Another stab at it,
Creating an empty matrix (Array of row arrays)
Iterating arr and assigning to matching rows
function arrayToMatrix(arr, wantedRows) {
// create a empty matrix (wantedRows Array of Arrays]
// with arr in scope
return new Array(wantedRows).fill(arr)
// replace with the next row from arr
.map(() => arr.splice(0, wantedRows))
}
// Initialize arr
arr = new Array(16).fill(0).map((val, i) => i)
// call!!
console.log(arrayToMatrix(arr, 4));
// Trying to make it nice
const arrToMat = (arr, wantedRows) => new Array(wantedRows).fill(arr)
.map(() => arr.splice(0, wantedRows))
(like in: this one)
(and: this one from other thread)
MatArray Class?
Extending an Array to add to a prototype, seems useful, it does need some features to complement the Array methods, maybe there is a case for a kind of MatArray Class? also for multidimensional mats and flattening them, maybe, maybe not..
1D Array convert 2D array via rows number:
function twoDimensional(array, row) {
let newArray = [];
let arraySize = Math.floor(array.length / row);
let extraArraySize = array.length % row;
while (array.length) {
if (!!extraArraySize) {
newArray.push(array.splice(0, arraySize + 1));
extraArraySize--;
} else {
newArray.push(array.splice(0, arraySize));
}
}
return newArray;
}
function twoDimensional(array, row) {
let newArray = [];
let arraySize = Math.floor(array.length / row);
let extraArraySize = array.length % row;
while (array.length) {
if (!!extraArraySize) {
newArray.push(array.splice(0, arraySize + 1));
extraArraySize--;
} else {
newArray.push(array.splice(0, arraySize));
}
}
return newArray;
}
console.log(twoDimensional([1,2,3,4,5,6,7,8,9,10,11,12,13,14], 3))
Short answer use:
const gridArray=(a,b)=>{const d=[];return a.forEach((e,f)=>{const
h=Math.floor(f/b);d[h]=d[h]||[],d[h][f%b]=a[f]}),d};
Where:
a: is the array
b: is the number of columns
An awesome repository here .
api : masfufa.js
sample : masfufa.html
According to that sample , the following snippet resolve the issue :
jsdk.getAPI('my');
var A=[1, 2, 3, 4, 5, 6, 7, 8, 9];
var MX=myAPI.getInstance('masfufa',{data:A,dim:'3x3'});
then :
MX.get[0][0] // -> 1 (first)
MX.get[2][2] // ->9 (last)

Array truncation with splice method

I need to delete occurrences of an element if it occurs more than n times.
For example, there is this array:
[20,37,20,21]
And the output should be:
[20,37,21]
I thought one way of solving this could be with the splice method
First I sort the array it order to make it like this:
[20,20,37,21]
Then I check if the current element is not equal to the next and split the array into chunks, so it should look like:
[20, 20],[37],[21]
Later I can edit the chunk longer than 1 and join it all again.
This is what the code looks like in my head but didn't work in real life
var array = [20, 37, 20, 21];
var chunk = [];
for(i = 0; i < array.length; i++) {
if(array[i] !== array[i + 1]) {
var index = array.indexOf(array[i]);
chunk.push = array.splice(0, index) // cut from zero to last duplicate element
} else
var index2 = a.indexOf(a[i]);
chunk.push(a.splice(0, index));
}
with this code the output is
[[], [20, 20]]
I think It's something in the 'else' but can't figure it out what to fix.
As the logic you want to achieve is to delete n occurrences of element in an array, your code could be as follow:
var array = [1, 1, 3, 3, 7, 2, 2, 2, 2];
var n = 2;
var removeMultipleOccurences = function(array, n) {
var filteredArray = [];
var counts = {};
for(var i = 0; i < array.length; i++) {
var x = array[i];
counts[x] = counts[x] ? counts[x] + 1 : 1;
if (counts[x] <= n) filteredArray.push(array[i])
}
return filteredArray;
}
console.log(removeMultipleOccurences(array, n));
I came up with this one, based on array filter checking repeated values up to a limit, but I can see #Basim's function does the same.
function removeDuplicatesAbove(arr, max) {
if (max > arr.length) {max = arr.length;}
if (!max) {return arr;}
return arr.filter(function (v, i) {
var under = true, base = -1;
for (var n = 0; n < max; n++) {
base = arr.indexOf(v, base+1); if (base == -1) {break;}
}
if (base != -1 && base < i) {under = false;}
return under;
});
}
var exampleArray = [20, 37, 20, 20, 20, 37, 22, 37, 20, 21, 37];
console.log(removeDuplicatesAbove(exampleArray, 3)); // [20, 37, 20, 20, 37, 22, 37, 21]
Always when you use splice() you truncate the array. Truncate the array with the length of same values from the start with the help of lastIndexOf(). It always starts from 0.
[ 1, 1, 1, 2, 2, 2, 3, 4, 4, 5 ] // splice(0, 3)
[ 2, 2, 2, 3, 4, 4, 5 ] // splice(0, 3)
[ 3, 4, 4, 5 ] // splice(0, 1)
[ 4, 4, 5 ] // splice(0, 2)
[ 5 ] // splice(0, 1)
Do this as long as the array length is greater than 0.
var arr = [1, 1, 1, 2, 2, 2, 3, 4, 4, 5];
var res = [];
while (arr.length > 0) {
var n = arr[0];
var last = arr.lastIndexOf(n) + 1;
res.push(n);
arr.splice(0, last);
}
console.log(res);
You can use Array.prototype.reduce(), Array.prototype.filter() to check if n previous elements are the same as current element
let cull = (arr, n) => arr.reduce((res, curr) => [...res
, res.filter(v => v === curr).length === n
? !1 : curr].filter(Boolean), []);
let arrays = [[20,37,20,21], [1,1,3,3,7,2,2,2,2]];
let cullone = cull(arrays[0], 1);
let cullthree = cull(arrays[1], 3);
console.log(cullone // [20, 37, 21]
, cullthree // [1, 1, 3, 3, 7, 2, 2, 2]
);

Find the lowest among the next highest elements in an array in JavaScript

The question seems to be a bit weird. Never mind.
This is an array
[2, 7, 5, 10]
If I want to get the next greater number after 2, here's my code
var MyArray = [2, 7, 5, 10];
var RandomNumber = 2;
var MinGreaterThanPos;
for (var i =0; i < MyArray.length; i++) {
if (MyArray[i] <= RandomNumber)
continue;
if (typeof(MinGreaterThanPos) == 'undefined' || MyArray[i] < MinGreaterThanPos)
{
MinGreaterThanPos = i;
}
}
alert(MyArray[MinGreaterThanPos]);
It will return 7.
What if I want to get the lowest among the greater numbers after 2?
That means, 7, 5, 10 are greater than 2. But I want to get 5, since the difference between 5 and 2 is lesser than any of the rest comparing with 2.
How will I do that?
Updated:
Coming to this point so far, what if there are objects inside an array?
For example:
var MyArray = [{user: 1, position:2}, {user:2, position: 6}, {user:3, position: 4}];
I want to do the same thing only with position. If I choose position 2, then the next position I am hoping to get back is 4 and not 6.
Another way to solve your problem is the following. Initially, we extend the Array adding a min method, in order we get the minimum element of an array. This is taken from here. Then we filter our array, in order we exlcude the enries that are less or equal to the number we hava as a threshold. Last we find the min number.
Array.min = function( array ){
return Math.min.apply( Math, array );
};
var numbers = [2, 7, 5, 10];
var number = 5;
var numbers = numbers.filter( function( n ){
return n > number;
});
console.log( Array.min( numbers ) );
first you sort the array then you get next of last item equal to RandomNumber if there is duplicates
var MyArray = [2,2,2, 10, 7, 5,5,7,5];//to test duplicates
var RandomNumber = 2;
var srt = MyArray.sort(function(a,b){return a-b});
var MinGreaterThanPos = srt[srt.lastIndexOf(RandomNumber)+1];
alert(MinGreaterThanPos);
This returns the minimal of array elements greater than el:
function minNext (a, el) {
var min = Infinity;
for (let x of a) {
if (x > el && x - el < min - el)
min = x;
}
return min;
}
//
let a = [1,9,2,8,3,-2,7,4,-3,6,5,5,5];
for (let x of a)
console.log(x, minNext(a, x))
less efficient, but more idiomatic:
let minNext = (a, el) => Math.min.apply(0, a.filter(x => x > el));
you can use this
var ar = [2,7,5,10];
Math.min.apply(undefined, ar.filter(function(x,y){return y > 0}));
//for any explanation, tell it in comment
You might do like this in a single pass. It takes into account the duplicates as well;
var arr = [2, 7, 5, 2, 10],
result = arr.reduce((p,c) => c < p[0] ? (p[0] = c,p)
: c < p[1] ? (p[0] !== c && (p[1] = c),p)
: p, [Infinity,Infinity])[1];
console.log(result);
As per objects as array items you simply the modify the code to show as follows;
var arr = [{user: 1, pos:2}, {user:2, pos: 6}, {user:3, pos: 4}, {user:4, pos: 12}, {user:5, pos: 9}],
result = arr.reduce((p,c) => c.pos < p[0].pos ? (p[0] = c,p)
: c.pos < p[1].pos ? (p[0].pos !== c.pos && (p[1] = c),p)
: p, [{pos:Infinity},{pos:Infinity}])[1];
console.log(result);
You can first sort and then loop through the array and until you find the next larger value. This way you will always have the second lowest value even if you have multiple.
var MyArray = [2,7,5,10];
var RandomNumber = 2;
var MinGreaterThanPos;
sortedMyArray = MyArray.sort(function(a, b){return a-b});
for(var i in sortedMyArray) {
if (sortedMyArray[i] > RandomNumber) {
MinGreaterThanPos = i;
break;
}
}
alert(sortedMyArray[MinGreaterThanPos]);
You can do the same for position as:
var MyArray = [{user: 1, position:2}, {user:2, position: 6}, {user:3, position: 4}];
var RandomNumber = 2;
var MinGreaterThanPos;
sortedMyArray = MyArray.sort(function(a, b){return a.position-b.position});
for(var i in sortedMyArray) {
if (sortedMyArray[i].position > RandomNumber) {
MinGreaterThanPos = i;
break;
}
};
alert(sortedMyArray[MinGreaterThanPos]);
And if your don't want to use RandomNumber
var MyArray = [{user: 1, position:2}, {user:2, position: 6}, {user:3, position: 4}];
var MinGreaterThanPos;
sortedMyArray = MyArray.sort(function(a, b){return a.position-b.position});
for(var i in sortedMyArray) {
if (sortedMyArray[i].position > sortedMyArray[0].position) {
MinGreaterThanPos = i;
break;
}
};
alert(sortedMyArray[MinGreaterThanPos]);
You could use Array#reduce.
function getItem(array, search) {
return array.reduce(function (r, a) {
return a.position > search && (!r || r.position > a.position) ? a : r;
}, undefined);
}
var array1 = [{ user: 1, position: 2 }, { user: 2, position: 6 }, { user: 3, position: 4 }],
array2 = [{ user: 1, position: 2 }, { user: 2, position: 6 }, { user: 3, position: 4 }, { user: 4, position: 5 }];
console.log(getItem(array1, 2));
console.log(getItem(array2, 2));

Categories