I have four arrays like this
var Broker = ['A', 'B', 'C'];
var Currency = ['C', 'D', 'E'];
var Time = ['F', 'G', 'H', 'I'];
var Mode = ['J', 'K', 'L'];
so all theses arrays are shown in multiple select separtely.so when user selects the multiple field from each multi select dropdown i like to list all the paring possiblities with selected items
exampleif user choose A and B from Broker, C from currency, F,G from time and J from mode the paring possiblity should be stored in another separate array like this
var paired = [{borker:A,currency:C,time:F, mode: J},{borker:A,currency:C,time:G, mode: J},{borker:A,currency:C,time:F, mode: J}, {borker:B,currency:C,time:F, mode: J},{borker:B,currency:C,time:G, mode: K},{borker:B,currency:C,time:F, mode: L}];
I may missed the items in paried array but i need atleast one unique item from all the selected arrays.Its kind of set.So can you guys how can i obtain this kind of result.
This code creates an array called permutations which holds an object for every possible permutation of choices. The nested for-loops are the trick to permutations.
var Broker = ['A', 'B', 'C'];
var Currency = ['C', 'D', 'E'];
var Time = ['F', 'G', 'H', 'I'];
var Mode = ['J', 'K', 'L'];
var permutations = [];
for(var i = 0; i < Broker.length; i++) {
for(var j = 0; j < Currency.length; j++) {
for(var k = 0; k < Time.length; k++) {
for(var l = 0; l < Mode.length; l++) {
permutations.push({
borker:Broker[i],
currency:Currency[j],
time:Time[k],
mode:Mode[l]
});
}
}
}
}
Related
Im writing algorithms for a scrabble scoring program in javascript Node JS, and im trying to use create an object that has a function as a value. That function calls and uses another function, but for whatever reason it says my function is not defined.
I have tried searching google for a solution with no luck.
//this is the original score key
const oldScoreKey = {
1: ['A', 'E', 'I', 'O', 'U', 'L', 'N', 'R', 'S', 'T'],
2: ['D', 'G'],
3: ['B', 'C', 'M', 'P'],
4: ['F', 'H', 'V', 'W', 'Y'],
5: ['K'],
8: ['J', 'X'],
10: ['Q', 'Z']
};
//below is a transform function that reverses the keys and values of oldScoreKey
function transform(obj) {
const newScoreKey = {};
for (var key in oldScoreKey) {
for (let i = 0; i < oldScoreKey[key].length; i++) {
newScoreKey[oldScoreKey[key][i].toLowerCase()] = +key;
}
}
return newScoreKey;
}
let newScoreKey = transform(oldScoreKey);
//below is the new object that has the scoreFunction key that is causing the problem.
let scrabbleScoring = {
name: 'Scrabble',
description: 'The traditional scoring algorithm.',
scoreFunction: function(word) {
var letter, i, sum = 0;
for (i = 0; i < word.length; i++) {
letter = word[i];
sum += newScoreKey[letter];
}
}
};
console.log(scrabbleScoring.scoreFunction('word'))
The expected result is for whatever word is entered into scoreFunction,
to be scored according to the point system layed out by newScoreKey.
But i get an error stating that newScoreKey is undefined when I try to call scoreFunction.
First of all feel free to edit title if it isn't precise enough.
JS is completly not my territory.
Im trying to write this custom JS callback and, aside of the index-grabing line, it does what i require.
However since im already inside a double for-loop i have no idea how to push correct
( by correct I mean: the indexes under which each occurence of value present in 'active holder' var resided before pushing to 'partial_data' var)
indexes into indexes var. As it looks now, it will only return index of first occurence.
var full_data = ['a', 'b', 'c', 'd', 'a', 'g', 'g', 'h']
var partial_data = []
var active_holder = ['a', 'g']
var indexes = []
for (j = 0; j < full_data.length; j++) {
for (z = 0; z < active_holder.length; z++) {
if (active_holder[z].includes(full_data[j])) {
indexes.push(full_data.indexOf(full_data[j]));
partial_data.push(full_data[j]);
}
}
}
console.log(partial_data) // * ['a', 'a', 'g', 'g'] //
console.log(indexes) // * [0, 0, 5, 5] // WRONG, should be 0,4,5,6 or something along
Any suggestions please ?
You can use reduce and includes
const fullData = ['a', 'b', 'c', 'd', 'a', 'g', 'g', 'h']
const active = ['a', 'g']
let {partial, index} = fullData.reduce((op,inp,index)=>{
if( active.includes(inp) ){
op.partial.push(inp)
op.index.push(index)
}
return op
},{partial:[],index:[]})
console.log(partial)
console.log(index)
You could use reduce method and return one object with both indexes and partial data.
var data = ['a', 'b', 'c', 'd', 'a', 'g', 'g', 'h']
var active = ['a', 'g']
const {
index,
partial
} = data.reduce((r, e, i) => {
if (active.includes(e)) {
r.index.push(i)
r.partial.push(e)
}
return r
}, {
index: [],
partial: []
})
console.log(index)
console.log(partial)
You are again calling indexOf while push(). You should just push() j.
indexOf: returns the index for first occurence of value in array
var full_data = ['a', 'b', 'c', 'd', 'a', 'g', 'g', 'h']
var partial_data = []
var active_holder = ['a', 'g']
var indexes = []
for (j = 0; j < full_data.length; j++) {
for (z = 0; z < active_holder.length; z++) {
if (active_holder[z].includes(full_data[j])) {
indexes.push(j);
partial_data.push(full_data[j]);
}
}
}
console.log(partial_data) // * ['a', 'a', 'g', 'g'] //
console.log(indexes) // * [0, 0, 5, 5] // WRONG, should be 0,4,5,6 or something along
I have an array: var array = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i']
I want the array to be a length of 3, but I don't want to remove the consecutive elements. I want to remove elements evenly from an array.
Since array.length is 9, I want to remove 6 elements from it and I always want to keep the first element. so in this case, var array would be like ['a', 'e', 'i']
I was doing like this so far:
var extraLength = array.length - 3;
var eachVal = Math.round(array.length/extraLength);
for (var j = 0; j < extraLength; j++){
for(var i = array.length -1; i >= 0; i-- ) {
if (i == eachVal*j && i != 0) {
array.splice(i, 1)
}
}
}
This gives an error saying array.splice is not a function.
Also, as I write this, I realized that this only works when array.length/extraLength is an even number, so in the example above, it does not work.
How can I achieve this?
This gives an error saying array.splice is not a function.
There is no error, at least with the code you presented in the question. See the snippet below for the result of your code:
var array = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i'];
var extraLength = array.length - 3;
var eachVal = Math.round(array.length/extraLength);
for (var j = 0; j < extraLength; j++){
for(var i = array.length -1; i >= 0; i-- ) {
if (i == eachVal*j && i != 0) {
array.splice(i, 1)
}
}
}
console.log(array);
I want the array to be a length of 3, but I don't want to remove the
consecutive elements. I want to remove elements evenly from an array.
Since array.length is 9, I want to remove 6 elements from it and I
always want to keep the first element. so in this case, var array
would be like ['a', 'e', 'i']
But, this only one use-case. How do you want to handle odd/even sized arrays? I assume, your basic requirement is to remove elements with some delta based on the length of the array and the desired resulting size. And you would be fine by truncating the trailing elements.
To do that would be easy by dividing the length of the array by your desired size and flooring it to avoid fractions. Adding one to that will allow you to have a delta step factor which is suitable for odd sized arrays.
Once done that, you just iterate the array with a step size of your calculated delta factor.
Example:
var array = [
['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i'],
['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'],
['a', 'b', 'c', 'd', 'e']
],
factor, result, desiredSize = 3;
for (i=0; i < array.length; i++) {
// calculate the delta factor
factor = Math.floor(array[i].length / desiredSize) + 1;
factor = (desiredSize % 2 === 0) ? --factor : factor;
result = [];
// iterate with the step size of the calculated facor
for (j = 0; j < array[i].length; j = j + factor) {
result.push(array[i][j]);
}
console.log(result);
}
In the above example, I am using a nested array to show multiple arrays of differing lengths to keep the code size compact. Check the console to see the result for each of the arrays.
Edit:
Changed the code to take into account if the desiredSize is an even number. Have to just rollback the increment on the floored delta factor. This will make it more evenly distributed. Thank you #Andrey Popov for the comment.
I can't figure out whats wrong here, Im trying to loop through the entire array but its stopping at 'c'.
function foo() {
var temp = ['e', 'm', 'o', 'c', 'l', 'e', 'W'];
for (var i = 0; i < temp.length; i++) {
console.log(temp.pop());
}
}
foo() // => welc
It does it only the half time, because when you use the pop(), it automatically reduces the length. The for loop checks the length when every loop is done. Instead use:
function foo() {
var temp = ['e', 'm', 'o', 'c', 'l', 'e', 'W'];
for (;temp.length > 0;) {
console.log(temp.pop());
}
}
You won't be needing the counters at all. So we can even switch this to while loop.
function foo() {
var temp = ['e', 'm', 'o', 'c', 'l', 'e', 'W'];
while (temp.length > 0) {
console.log(temp.pop());
}
}
The pop() method removes the last element of the array, and returns that element and with that changes the length of that array.
And in your loop. you are increasing the i and decreasing the array length. so it will go from:
0(i) < 7 (array length)
to
3(i) < 4 (array length) (here is your c)
and then exit the loop because the i will be 4 and the array length will be 3.
so in your case you can use in the console.log the temp[i], to iterate all the array.
function foo() {
var temp = ['e', 'm', 'o', 'c', 'l', 'e', 'W'];
for (var i = 0; i < temp.length; i++) {
console.log(temp[i]);
}
}
When you pop temp in the for loop you are changing the length of temp
pop() reduces the size of the array. Your code is running like this (L is array length, I is the index).
L I
W 6 0
e 5 1
l 4 2
c 3 3
So the loop stops when the index is no longer less than the array length (i < temp.length).
Perhaps a change to:
for (var i = temp.length; temp.length > 0; i--) {
console.log(temp.pop());
}
I am trying to generate n arrays with a for loop and push an extra element from another array of n using for loop to each of these arrays.
var userlist = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l'];
var selectlist = ['c', 'f', 'k'];
get_field_options = userlist.filter(function (el) {
return selectlist.indexOf(el) < 0;
});
var selectlen = selectlist.length;
var op_arr = new Array();
for (var i = 0; i < selectlen; i++) {
op_arr[i] = new Array();
op_arr[i] = get_field_options;
op_arr[i].push(selectlist[i]);
console.log(op_arr[i]);
}
here is my working fiddle.
but its adding items to same array each time. what I am doing wrong?
this line op_arr[i] = get_field_options; makes your arrays reference to the same object.
You need to clone get_field_options to get a new array.
One simple way to clone is to use JSON.stringify like this.
op_arr[i] = JSON.parse(JSON.stringify(get_field_options));
Yet another way, use map and concat functions
var op_arr = selectlist.map(function(el){
return get_field_options.concat(el);
});