I have a javascript array of Date objects, which I'd like to test for ascendingness. This is what I built, but I am kind of disappointed by it. Any suggestion?
function isAscending(timeLine) {
if (timeLine.length < 2)
return true;
for(var i=1; i < timeLine.length; i++) {
if(timeLine[i-1] > timeLine[i])
return false;
}
return true;
}
(I was hoping for something more expressive, built-in, some library math function etc.)
That's the simplest way to do it; don't be disappointed.
How about
function isAscending(timeLine){
var i=0, L= timeLine.length-1;
while(i<L){
if(timeLine[i]> timeLine[++i]) return false;
}
return true;
}
es6 way
let isAscending = timeLine =>
timeLine.every( (v, i, a) => a.length - 1 === i || v < a[i + 1] )
Related
I have an javascript array and I want to delete an element based on the value of the array, this is my array and this is what I have tried without success.
array = []
array.push (["Mozilla","Firefox",1.10])
index = array.indexOf(["Mozilla","Firefox",1.10])
array.splice(index, 1)
But it doesn't work, any idea¿?
You're trying to compare arrays, which are objects and have unique addresses. Your index variable is -1.
Try ['Mozilla','Firefox',1.10] === ['Mozilla','Firefox',1.10] in your console, you'll see that just because two arrays have the same values, it doesn't mean they are the same array.
What you need is a deep-equals style of comparison, that checks each value in the array, to see if two arrays have a likeness.
Take a look at lodash's isEqual function for an idea.
Here's a simple looping function:
function deepIndex(array, comparison) {
var i, j;
main:
for (i = 0; i < array.length; i++) {
if (Array.isArray(array[i])) {
for (j = 0; j < array[i].length; j++) {
if (array[i][j] !== comparison[j]) {
continue main;
}
}
return i;
}
}
}
var arr = [];
arr.push('string', ['Mozilla','Firefox',1.10], 'thing');
var index = deepIndex(arr, ['Mozilla','Firefox',1.10])
console.log(index, arr);
arr.splice(index, 1);
console.log(arr);
Take a look at this:
// Array Remove - By John Resig (MIT Licensed)
Array.prototype.remove = function(from, to) {
var rest = this.slice((to || from) + 1 || this.length);
this.length = from < 0 ? this.length + from : from;
return this.push.apply(this, rest);
};
This is function, made by the Creator of JQUery.
Basically you take the Index of one thing and than it is getting removed
Array.prototype.remove = function(from, to) {
var rest = this.slice((to || from) + 1 || this.length);
this.length = from < 0 ? this.length + from : from;
return this.push.apply(this, rest);
};
//Equals Function taken from:
//http://stackoverflow.com/questions/7837456/comparing-two-arrays-in-javascript
Array.prototype.equals = function (array) {
// if the other array is a falsy value, return
if (!array)
return false;
// compare lengths - can save a lot of time
if (this.length != array.length)
return false;
for (var i = 0, l=this.length; i < l; i++) {
// Check if we have nested arrays
if (this[i] instanceof Array && array[i] instanceof Array) {
// recurse into the nested arrays
if (!this[i].equals(array[i]))
return false;
}
else if (this[i] != array[i]) {
// Warning - two different object instances will never be equal: {x:20} != {x:20}
return false;
}
}
return true;
}
array = [];
array.push (["Mozilla","Firefox",1.10]);
array.push (["Microsoft","Spartan",1.0]);
array.push (["Safari","Safari",1.4]);
index = indexOfArr(array,["Mozilla","Firefox",1.10]);
array.remove(index, index);
document.getElementById("length").innerHTML = array.length;
for(var i = 0; i < array.length; i++){
document.getElementById("elems").innerHTML += "<br>"+array[i];
}
function indexOfArr(hay, needle){
for(var i = 0; i < hay.length; i++){
if (hay[i].equals(needle)){
return i;
}
}
return -1;
}
<span id = "length"></span><br>
<span id = "elems">Elements:</span>
You can use the fiter metodh, instead of indexOf.
Within the callback of that method, you can choose different approaches:
Use toString on the arrays and compare the two strings
Test for the length and the content, by iterating over the contained elements
... Continue ...
In any case using === will solve the problem, unless the object contained is exactly the same against which you are trying to match.
By the same, I mean the same. We are non speaking about having the same content, but to be the same instance.
Loop over your array and check the equality:
array = [];
array.push(["Mozilla", "Firefox", 1.10]);
for (var i = 0; i < array.length; i++) {
if (arraysEqual(array[i], ["Mozilla", "Firefox", 1.10])) {
array.splice(i, 1);
}
}
function arraysEqual(a, b) {
if (a === b) return true;
if (a === null || b === null) return false;
if (a.length != b.length) return false;
for (var i = 0; i < a.length; ++i) {
if (a[i] !== b[i]) return false;
}
return true;
}
JSFiddle: http://jsfiddle.net/ghorg12110/r67jts35/
Based on this question : How to check if two arrays are equal with JavaScript?
You can do something like this
array = []
array.push (["Mozilla","Firefox",1.10])
tempArray = array[0];
index = tempArray.indexOf("Mozilla","Firefox",1.10)
array.splice(index, 1)
You can build on this if you put for loop instead of hard coding.
What would be a shorter way to write :
if (array1[0] >= array2[0] && array1[1] >= array2[1] && ...) {
do something;
}
I tried creating a function but I was not able to make it work, I'm still quite new at this.
The most elegant way would be to use .every
The every() method tests whether all elements in the array pass the test implemented by the provided function.
if (array1.every(function(e,i){ return e>=array2[i];})) {
do something;
}
This will return true if all elements of a are greater than all elements of b. It will return as early as possible rather than having to compare all of the elements.
function compare(a, b) {
for (i = 0; i < a.length; i++) {
if (a[i] < b[i]) { return false;}
}
return true
}
var isGreater = true;
for (var i = 0; i < array1.length; i++)
{
if (array1[i] < array2[i])
{
isGreater = false;
break;
}
}
if (isGreater)
{
//do something
}
You loop your first array and replace the numbers by the looping variable (i)
I have a JavaScript array and a variable like so;
var a = [0, 1200, 3260, 9430, 13220],
b = 4500;
What would be the smartest way to select the largest value in the array that's still smaller than or equal to the variable?
In this example, I'd need to select 3260.
I could do something like this;
$.each(a, function(i){
if(a[i] <= b && a[i+1] > b){
var c = a[i];
return false;
}
});
But I'm thinking that might not work if the selected array value is the last one. Not to mention, to me it looks like a lot of code for something rather simple.
Is there a smarter/less verbose way of achieving what I'm after?
(and yes, I know I shouldn't have used a jQuery loop for that but I'm lazy when typing examples)
Another way you could to it is through a combination of array filtering and apply(), which I think is a very readable approach.
The call to filter() just returns an array of elements in a which don't satisfy the predicate function and then apply() calls Math.max with each element as an argument.
var a = [1, 2, 3, 4, 5];
var b = 4;
var result = Math.max.apply(Math, a.filter(function(x){return x <= b}));
Result will be equal to 4.
var max = Number.MIN_VALUE;
for (var i = 0; i < a.length; i++) {
if (a[i] <= b && a[i] > max) {
max = a[i];
}
}
I think the above approach is quite simple, readable, and not very verbose. An alternative would be to use reduce, like so:
var max = a.reduce(function (i, j) { return j <= b ? Math.max(i, j) : i }, Number.MIN_VALUE);
grep() of jQuery
var a = [0, 1200, 3260, 9430, 13220],
b = 4500;
var c= Math.max.apply( Math,$.grep(a,function(n){return n<=b}));
document.write(c)
WORKING DEMO
Is the a array always sorted? In this case, you could optimize your code along those lines (you might want to check the indexes, I haven't checked the code):
var beginning = 0;
var end = a.length;
while ((end-beginning)>1) {
var currentIndex = Math.floor((beginning+end)/2);;
if (a[currentIndex] < b) {
beginning = currentIndex;
} else if (a[currentIndex] > b){
end = currentIndex;
} else {
beginning=end=currentIndex;
}
}
var max = a[beginning];
var closest = null;
$.each(a, function() {
if ( closest == null || Math.abs(this - b) < Math.abs(closest - b) ) {
closest = this;
}
});
A jQuery variant if that for some reason would be desired.
Following should do:function getHigh(arr, max){
var c;
for(var i = 0, len=arr.length; i<len; i++){
if(a[i] <= b) c=a[i];
else return c;
}
return false;
}
I have two arrays of integers a=[1,3,5,7] and b=[2,4,6,8].
Now I need to check if a given var $v is in a and if it is, return the equivalent element from b. Example:
if $v in a (and $x is its position) return $b[$x].
How do I perform this?
the indexOf method will return the index of the array where the item was found, or -1 if it wasn't found.
var i = a.indexOf(v);
if (i != -1)
{
return b[i]
}
EDIT: This will add the method if your browser doesn't have it.
if (!Array.prototype.indexOf)
{
Array.prototype.indexOf = function(x)
{
var i;
for (i = 0; i < this.length; i++)
{
if (this[i] === x)
{
return i;
}
}
return -1;
}
}
Loop through the items in the array:
for (var i = 0; i < a.length; i++) {
if (a[i] == v) {
return b[i];
}
}
return -1; // not found
var i = a.indexOf(v);
if (i !== -1)
{
return b[i]
}
if(a.indexOf(v) > -1) {
var id = a.indexOf(v);
console.log(b[id]);
}
See for compatibility of Array.indexOf
I suppose this would work.
>> function test(k){ return b[a.indexOf(k)]}
>> test(1)
2
>> test(9)
undefined
In js, indexOf always returns an integer and by calling an array with an integer (like A[3] ), it always returns a value or undefined. explicitly check for the undefined value if you want to make sure application code is not broken.
You may want to give below function a try. PHP.js is really a great collection of functions and salute to all contributors to make it what it is today.
http://phpjs.org/functions/in_array:432
I have an ordered list of integers, and would like to search through them.
What is the fastest way to do this?
Is this the fastest it will work? Or can we optimise it because it is ordered?
Array.prototype.Contains = function(value) {
for (var index = 0; index < this.length; index++) {
if (value == this[index]) {
return true;
}
}
return false;
}
Thanks
Tried implementing a "binary search":
Array.prototype.binarySearch = function(v) {
/* ARRAY MUST BE SORTED */
if ( !this.length ) { return false; }
if ( this[0] === v ) { return true; }
var i, mid,
start = 0,
end = this.length,
c = false;
while ( c = (i = this[mid = start+((end-start)>>1)]) !== v ) {
i < v ? (start = mid) : (end = mid);
if (start >= end - 1) { break; }
}
return !c;
};
If the list is sorted, the fastest way is always a binary search. This is true in all languages, as long as you have an ordered list.
http://en.wikipedia.org/wiki/Binary_search_algorithm
It is often handy to return the index of the found item from a sorted list. The optional second parameter determines if a boolean or index is returned.
Array.prototype.biSearch= function(v, ret){
var L= this.length, i= -1, m;
while(L - i> 1){
if(this[m= L + i>> 1]< v) i= m;
else L= m;
}
if(ret) return this[L]== v? L: -1;
return this[L]== v;
}
Virtually the same code can be used for another common task-adding an item to a sorted array without having to re-sort the whole array.
If the second parameter is not sent, the item will only be inserted if it does not already exist in the array.
Array.prototype.addtoSort= function(v, dup){
var L= this.length, i= -1, m;
while(L - i> 1){
if(this[m= L + i>> 1]< v) i= m;
else L= m;
}
if(this[L]!= v || dup){
return this.splice(L,0,v);
}
return this.length;
}