lodash searchIndex on reverse sorted list - javascript

I have a sorted array as follows:
var array = [ 10, 10, 10, 8, 7, 6, 6, 6, 6 ];
I want to find the insertion index of 11. Given that this array is sorted in descending order, the insertion index should be 0.
Is there a way to compute this using lodash, without having to reverse() and sort() again?

Use the iteratee argument of sortedIndex() to negate the item:
_.sortedIndex(array, 11, function(item) { return -item; });
// → 0

Related

JAVASCRIPT REGEX for digits suite

Is there a way to use regex to check if an array contains exactly one occurence of each number in a range ?
myArr = [1, 2, 3, 4, 5, 6, 7, 8, 9]
I have tried this :
let regex = /[1-9]{1}/;
But this only checks that the array contains at least one occurence in the range : )
The described validation is not a particularly good use case for regex.
One alternative way to find the answer you seek is to:
Create a Set with the array items. (A Set by default only retains unique values.)
Convert the Set back to array.
Compare the lengths of the original array and the new array. If they mismatch, the difference is the number of array items that exist in duplicate.
// return TRUE if myArr only has unique values
[...new Set(myArr)].length === myArr.length
You can just filter for duplicates and compare the original array with the filtered to see if it had any duplicates. Upside here is that you can use the filtered array if you need it
let myArr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 5]
let uniques = myArr.filter((v, i, a) => a.indexOf(v) === i)
let hasDupes = myArr.length != uniques.length
console.log("hasdupes?", hasDupes);
console.log(uniques)

sort() does not work

I use sort() to sort my table but I do not understand why it does not work. you have an idea ?
var tab = [5, 15, 17, 3, 8, 11, 28, 6, 55, 7];
tab = tab.sort();
for (var i = 0; i < tab.length; i++) {
$("p").append(" ", tab[i]);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<p></p>
https://jsfiddle.net/1423tLwd/
By default the sort method will sort the array that it has been called on alphabetically.
To get around this you need to pass sort a callback function that will sort the elements by their numerical value.
To achieve this you need to do the following:
function sortNumber(a, b) {
return a - b;
}
let tab = [5, 15, 17, 3, 8, 11, 28, 6, 55, 7];
let sortedTab = tab.sort(sortNumber);
console.log(sortedTab);
As Explained in MDN web docs:
The default sort order is according to string Unicode code points.
That is, you should give sort the function that is to compare elements of the array, otherwise, your array will be sorted according to string Unicode code points.
This should work (sort in ascending order):
function compareFunction(a, b) {
return a - b;
}
// sort tab array using compareFunction to compare elements
tab.sort(compareFunction);

How will I sort a list using javascript?

I want to sort a list on my site using JavaScript. I've search on w3c but it seems that I need to make a button for that. I want to sort a list automatically. Please help me.
Use .sort(<compare function>)
From MDN:
The sort() method sorts the elements of an array in place and returns the array. The sort is not necessarily stable. The default sort order is according to string Unicode code points.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort
Examples
For Ascending
var arr = [1, 6, 3, 8, 10, 3, 4, -1]; //unsorted array
arr.sort(function (a, b) {
return a - b;
});
result: -1,1,3,3,4,6,8,10
or
var arr = [1, 6, 3, 8, 10, 3, 4, -1]; //unsorted array
arr.sort(sortAscending);
function sortAscending(a, b) {
return a - b;
}
result: -1,1,3,3,4,6,8,10
For Descending
var arr = [1, 6, 3, 8, 10, 3, 4, -1]; //unsorted array
arr.sort(sortDescending);
function sortDescending(a, b) {
return b - a;
}
result: 10,8,6,4,3,3,1,-1
NOTE The compare function being passed may not suit your needs so you need to be more specific about what you're sorting for an answer that's more specific to your problem

Splice items from array considering indexes from another array

I have an array input and another array indexes. I want to remove item from array input whose index is provided in indexes array.
I have tried it using array.splice in for loop but as item is being removed in each iteration, indexes of other items are being changed.
JavaScript:
var array = [10, 11, 12, 13, 14, 15];
var indexes = [0, 1, 2, 3, 4, 5];
indexes.forEach(function(item) {
array.splice(item, 1);
});
console.log(array);
You can utilize Array.prototype.filter and do the following:
var array = [10, 11, 12, 13, 14, 15];
var indexes = [0, 1, 2, 3, 4, 5];
array = array.filter(function(x, i) {
return indexes.indexOf(i) === -1;
});
console.log(array);
Here you are using forEach loop which give you the item as first argument and the index on second one, so as per my understanding what you want to do can achieve by this, try this hope this solve your problem :)
indexes.forEach(function(item, index) {
array.splice(index, 1);
});
Sort the indexes array from high to low, then spice will only change the index of the numbers you have already removed

Finding the lowest number in an array using split('') and reverse();

I am trying to find the min value of an array, and am trying to do it by sorting the array, and then reversing the array, and then calling the very first index of the array.
Unfortunately with what I have been trying, I keep getting 9. (don't know why) Can anybody take a quick look at what I have been doing and bail me out here? (i'm using js)
var minny = [4, 3, 5, 2, 6, 3, 4, 5, 2, 3, 4, 6, 7, 8, 9, 9, 1, 11, 25];
var smallest = function (minny){
minny = minny.sort('');
var sorted = minny + " ";
sorted = minny.reverse('').join('');
return sorted[0];
}
console.log(smallest(minny))
By default the sort method sorts elements alphabetically(11 comes before 9) and therefore you need to add a compare function as a param.
var smallest = function (minny) {
minny = minny.sort(function(a, b) { return a - b; });
return minny[0];
}
console.log(smallest(minny))
JSFIDDLE.
Based on your code, you could just do
return minny.sort()[0];
So, your full code example becomes
var minny = [4, 3, 5, 2, 6, 3, 4, 5, 2, 3, 4, 6, 7, 8, 9, 9, 1, 11, 25];
var smallest = function (minny){
return minny.sort()[0];
}
console.log(smallest(minny))
You're calling minny.sort('') which is using the default natural sort, so 11 and 25 end up near the beginning because of the 1 and 2.
What you have to do is call sort with a function that compares numbers, such as:
minny.sort(function(a,b) { return b-a; });
This will sort minny the way you want it.
There is no need to even call reverse and join afterwards, just return the first item. "return sorted[0]" is fine but will fail if there are no items, so you might just want to call "return sorted.shift()" instead. This will return the first item too, but won't fail if the array is empty.
PS. your call to minny.reverse also has an empty string as a parameter. That's not needed, reverse takes no parameters.
sort() sorts alphabetically by string representation, so in your case it would result in 1, 11, 2, 2, 25, .... You have to provide a comparison function for correct integer sorting, although in your specific case it doesn't really make a difference.
var smallest = function (minny){
minny = minny.sort(function(a, b){return a-b});
return minny[0];
}
See jsfiddle
Using sort is fairly short code to write, and it will return the correct number if you use minny.sort(function(a,b){return a-b})[0].
If you have a large unordered array you are running the comparison many times and you are sorting the array, which is not usually what you want to do to an array.
It may be better to just iterate the members and compare each just once to the lowest fond so far.
var minny= [4, 3, 5, 2, 6, 3, 4, 5, 2, 3, 4, 6, 7, 8, 9, 9, 1, 11, 25];
var smallest= function(minny){
var min= Infinity;
minny.forEach(function(next){
if(next<min) min= next;
});
return min;
}
Or use Math.min, if this is code golf:
Math.min.apply(Array,minny);

Categories

Resources