Related
I have two Javascript vectors, a and b, of the same length. I want to add b to a. The obvious way is:
for (i=0; i<a.length; ++i)
a[i] += b[i]
but this requires a loop. I can do:
a.forEach(function(item,index) {
a[index] += b[index]
})
but this is cumbersome - it requires an unneeded parameter "item". Is there a shorter option? Maybe using some external library?
There is no built-in JS function to do this without looping. Further any library that implements this is going to use looping to do it also.
So, write yourself a short utility function and then just call that function whenever you want to do this. There's going to be looping somewhere in order to implement this as it is not a native JS feature. If you want to "hide" the looping, then just put it in a utility function and just call the function when you need it.
// returns a new array that is the sum of the two vector arrays
function addVectors(a, b) {
return a.map(function(item, index) {
return item += b[index];
});
}
Or, if you want one array modified in place:
// add one vector to another, modifying the first one
function addToVector(a, b) {
a.forEach(function(item, index) {
a[index] += b[index];
});
return a;
}
Or, if the unused item argument bothers you for some reason:
// add one vector to another, modifying the first one
function addToVector(a, b) {
for (var i = 0; i < a.length; i++) {
a[i] += b[i];
}
return a;
}
Note, all of these functions assume a and b are the same length. You would have to specify what you want the behavior to be if they end up not being the same length and you want to check for that. You could throw an exception, just add the parts in common, etc...
For example:
// returns a new array that is the sum of the two vector arrays
function addVectors(a, b) {
if (a.length !== b.length) {
throw new Error("Vector arrays must be the same length to add them");
}
return a.map(function(item, index) {
return item += b[index];
});
}
Are you sure you want to do it without the loop? Ok well:
a.forEach(function(currentElement, Index){
b[Index] += currentElement;
});
Basically I want to build a function which sorts objects in an array by one of the object's properties/member variables. I am preeeety sure that the comparator function is where the error is hidden, but I am not 100% sure.
The output I should get after the sort function is called is 1,2,3. I get 1,3,2 which means that it is unchanged
This is the entire js code (with some comments):
var arr = [];
//object definition and creation
var main = document.getElementById("main");
var task = {
name: "",
priority: 0
};
//first
var one = Object.create(task);
one.priority = 1;
//secondd
var two = Object.create(task)
two.priority = 3;
//last
var three = Object.create(task);
three.priority = 2;
//append
arr.push(one);
arr.push(two);
arr.push(three);
//sort function
function sortT() {
arr.sort(compareFN);
}
//comperator function
function compareFN() {
return task.priority < task.priority;
}
function print() {
for (var i = 0; i < arr.length; i++) {
console.log(arr[i].priority);
}
}
//execution of the program
print();
sortT();
print();
EDIT: The solution is the following - As stated, the comparator function really was the problem, the correct way to write it is the following:
function compareFN(taskA, taskB) {
return taskA.priority < taskB.priority;
}
There are multiple problems with your comparator:
It refers to the global task object instead of the objects being compared.
It compares the object to itself.
It is supposed to perform a three-way comparison.
Try:
var compareFN = function(a, b) {
return a.priority - b.priority;
}
The compare function needs two arguments: the first and the second element it should compare.
So your compareFN should look like this:
function compareFN(taskA, taskB) {
return taskA.priority - taskB.priority;
}
Edit: As NPE said, it is supposed to perform a three-way comparison, so a simple a < b is not so a great idea here.
You need to change the signature of you compare function to include the two tasks.
For ascending order (normally what you want) you need to do b < a, a < b will do descending order
//comperator function
function compareFN(a, b) {
return b.priority < a.priority;
}
The comparator function returns a negative value, zero or a positive value. Those three comprise what is called here the three-way comparison.
So:
'''
function cmp((a, b) => {
// ascending
return a - b
}
'''
For descending return b - a
I'm try to order objects numerically in an array in ascending order, by using a nested object property, the property being 'score'.
The Array;
[ {name:'dan', score:220},
{name:'lucy', score:876},
{name:'mike', score:211} ]
I found the following thread, but have yet managed to get it working.
How to sort a JavaScript array of objects by nested object property?
Console outputs undefined.
function order_top_scores(prop, arr) {
arr.sort(function (a, b) {
if (a[prop] < b[prop]) {
return -1;
} else if (a[prop] > b[prop]) {
return 1;
} else {
return 0;
}
});
};
function get_results(){
$.get(wp_theme_dir+'/dottodot.php',
function(data){
var returnedData = $.parseJSON(data);
var ordered_scores = order_top_scores(returnedData)
console.log(returnedData);
});
}
My Array differs slightly, could it be the second property thats disrupting the sort?
Or maybe the way i'm handling the data from the ajax request.
thanks in advance,
Cam
how are you?
I've just tested this, and there's a couple of things you might want to modify.
First of all, you're using the "sort" solution from the person who asked, not the actual solution, so you first need to rewrite your order_top_scores, like this:
var order_top_scores = function (prop, arr,reverse) {
if(!reverse) reverse = 0;
prop = prop.split('.');
var len = prop.length;
arr.sort(function (a, b) {
var i = 0;
while( i < len ) { a = a[prop[i]]; b = b[prop[i]]; i++; }
if (a < b) {
return -1;
} else if (a > b) {
return 1;
} else {
return 0;
}
});
if(reverse){arr.reverse()};
return arr;
};
Analyzing this function, I've added a third "reverse" parameter, which expects either true or false (because the original solution orders it from lowest to highest, and in this case you want the opposite)
Now that you have this function, there's 2 things you want to keep in mind:
First
In this line:
var ordered_scores = order_top_scores(returnedData);
You are not sending the first mandatory parameter, which actually tells the function which property you want the object sorted by: In this case, "score".
So, you have to call the function like this:
var ordered_scores = order_top_scores('score',returnedData);
And if you want it to be sorted from high to low, like this:
var ordered_scores = order_top_scores('score',returnedData,true);
Second
Also, keep in mind you are outputting the "returnedData" value rather than the ordered_scores value so if this line:
console.log(returnedData);
Is outputting undefined, it means your JSON data is not correct. To be sure the sort worked, you should also output the ordered_scores like this:
console.log(ordered_scores);
Let me know if anything unclear.
Cheers!
I am not sure this is the correct code:
var returnedData = $.parseJSON(data);
var ordered_scores = order_top_scores(returnedData)
The following change in order_top_scores and method call may work correctly if returnedData is array as you have mentioned in your question:
function get_results(){
$.get(wp_theme_dir+'/dottodot.php',
function(data){
var returnedData = $.parseJSON(data);
var ordered_scores = order_top_scores("score", returnedData);
console.log(returnedData);
});
}
function order_top_scores(prop, arr) {
arr.sort(function (a, b) {
if (a[prop] < b[prop]) {
return -1;
} else if (a[prop] > b[prop]) {
return 1;
} else {
return 0;
}
});
}
You can check the output here in console
I have a function that computes product of numbers in an array. The function should work like this
function prod (array){
//compute and return product
}
var arr = [1,2,3,0,4,5,0,6,7,8,0,9];
the function call:
prod(arr); //should return 6
prod(arr); //should return 20
prod(arr); //should return 336 (6*7*8)
prod(arr); //should return 9
prod(arr); //should return 0
prod(arr); //should return 0
prod(arr); //should return 0
In scheme, this is done with continuations, by storing previous state of the function (state of the function is captured just before its exit point) see this
So, in short, I want the javascript function return different values at different times with same parameter passed everytime.
JavaScript is a well designed language, so I hope there must be something which can emulate this. If there happens to be nothing in JS to do it, I do not mind to conclude with failure and move on. So, feel free to say its impossible.
Thanks.
JavaScript is not capable of supporting continuations: it lacks tail-calls.
Generally I would write this to use a "queue" of sorts, although CPS is also do-able (just have a finite stack :-) Note that other state can also be captured in the closure, making it an "explicit continuation" of sorts ... in a very gross sense.
Example using a closure and a queue:
function prodFactory (array){
// dupe array first if needed, is mutated below.
// function parameters are always locally scoped.
array.unshift(undefined) // so array.shift can be at start
// also, perhaps more closured state
var otherState
// just return the real function, yippee!
return function prod () {
array.shift()
// do stuff ... e.g. loop array.shift() and multiply
// set otherState ... eat an apple or a cookie
return stuff
}
}
var prod = prodFactory([1,2,3,0,4,5,0,6,7,8,0,9])
// array at "do stuff", at least until "do stuff" does more stuff
prod() // [1,2,3,0,4,5,0,6,7,8,0,9]
prod() // [2,3,0,4,5,0,6,7,8,0,9]
prod() // [3,0,4,5,0,6,7,8,0,9]
Happy coding.
"Finished implementation". Although this particular problem can avoid array mutation and just use an index: the same concepts apply. (Well, slightly different. With just an index the closed over variable would be altered, whereas with this approach an object is mutated.)
function prodFactory (array) {
array = array.slice(0)
return function prod () {
var p = 1
for (var n = array.shift(); n; n = array.shift()) {
p *= n
}
return p
}
}
var prod = prodFactory([1,2,3,0,4,5,0,6,7,8,0,9])
prod() // 6
prod() // 20
prod() // 336
You can give the function a property that will be remembered between calls:
function prod (array){
if (typeof prod.index === "undefined" || prod.currentArray != array) {
prod.currentArray = array;
prod.index = 0;
}
if (prod.index >= array.length)
return 0;
//compute and return product
var p = 1,
c;
while (prod.index < array.length) {
c = array[prod.index++];
if (c === 0)
return p;
p *= c;
}
return p;
}
I'm just guessing from your description of what should be returned that on an individual call to the function it should take the product of all of the numbers up to but not including the next zero or the end of the array. Calls after the end of the array should return 0? I may have the algorithm wrong for that, but you get the idea for what I'm suggesting to remember the function state between calls.
I've added a property to remember the current array being processed. As long as you keep passing the same array in to the function it will continue with the next elements, but if you pass a different array it will reset...
you can try something like
var index = 0;
function prod (array){
if(index < array.length){
var prod=1;
for(int i=index;i<array.length;i++){
if(array[i] != 0){
prod = prod * array[i];
}
else{
index = i+1;
return prod;
}
}
}
return 0;
}
this will update the global variable index everytime the function is called.
What you're looking for here are generators. As of 1.7, JavaScript supports them.
The title should explain my question.
I have an array:
a = [[1,2],[1,3],[1,4]];
How can I check if the array [1,2] is inside the array a?
That depends on the situation.
given
var a = [1,2], b = [1,3], c = [a,b];
We can check easily if a resides in c, if we have c to test on.
for(var i=0,d;d=c[i];i++) {
if(d === a) {
//a is inside c
}
}
or even simpler for browser that supports it (ie7 doesn't)
if(c.indexOf(a) != -1) {
//a is inside c
}
But if we only have a, and a is not a local variable and we wish to know if it exists inside any array, then we can't, since a is a reference to an object and we can't possibly know if a reference to it exists elsewhere outside our current scope.
if you have a reference, the you can use the == operator. else you have to write your own method to test values. something like this:
function someMethod(testArr, wanted){
for (i=0; i<testArr.length; i++){
if(array_diff(testArr[i], wanted).length==0 && array_diff(wanted, $subArr).length==0){
return true;
}
}
return false;
}
function array_diff(a1, a2)
{
var a=[], diff=[];
for(var i=0;i<a1.length;i++)
a[a1[i]]=true;
for(var i=0;i<a2.length;i++)
if(a[a2[i]]) delete a[a2[i]];
else a[a2[i]]=true;
for(var k in a)
diff.push(k);
return diff;
}
If your array contains numbers or texts only, you can join each array into string, then compare if a string is inside the other.
var a = [[1,2],[1,3],[1,4]];
var b = [1,2]
var aStr = '#' + a.join('#') + '#'
var bStr = '#' + b.join() + '#'
if (aStr.indexOf(bStr) > -1){
alert ('b is inside a')
}else{
alert ('b is not inside a')
}
You can try this if your array elements are non-nested arrays.
return JSON.stringify([[1,2],[1,3],[1,4]]).indexOf(JSON.stringify([1,2])) > 0
This checks if the JSON representation of [1,2] is contained in the JSON representation of [[1,2],[1,3],[1,4]]
But in this case it gives a false positive
return JSON.stringify([[[1,2]],[1,3],[1,4]]).indexOf(JSON.stringify([1,2])) > 0
returns true.
You can also loop through the array object and for each of it's item you can use jQuery.isArray() to determine if the object is an array.