How to get values from one array based on another array? - javascript

So, I am trying to figure out a situation where I would populate an array (b[]) with the index numbers from another array (a[]) whose elements meet a certain criteria (array b would be index numbers based on array a which is an array of images, and would populate b when width is greater than height).
So, I came up with a hypothetical function in hopes of getting an array output where I would get a listing from a[] that align with values from b[]. Needless to say, neither attempt came up with anything of value.
var a = ['ab', 'bc', 'cd', 'de', 'ef', 'fg', 'gh', 'hi'];
var b = [2, 4, 5];
var d = function(a, b) {
var func1 = [];
var func2 = [];
for(i = 0; i > b.length; i++) {
func1.push(a[b[i]]);
}
console.log('func1 = ' + func1); // 'func1 = []'
for(i=0; i > a.length; i++) {
if(b.indexOf(a.indexOf(a[i])) > -1) {
func2.push(a[i])
}
}
console.log('func2 = ' + func2); // 'func2 = []'
}
d(a,b) // Ideally outputs ['cd', 'ef', 'fg']
Is this a matter of function scope, or am I missing the point of .push?

The comparisons in your for loops are backwards. They should be like this:
for(i = 0; i < b.length; i++) {
func1.push(a[b[i]]);
}
console.log('func1 = ' + func1); // 'func1 = []'
for(i=0; i < a.length; i++) {
if(b.index(a.indexOf(a[i])) > 1) {
func2.push(a[i])
}
}
Also, b.index is not a function. I assume you meant indexOf:
var a = ['ab', 'bc', 'cd', 'de', 'ef', 'fg', 'gh', 'hi'];
var b = [2, 4, 5];
var d = function(a, b) {
var func1 = [];
var func2 = [];
for(i = 0; i < b.length; i++) {
func1.push(a[b[i]]);
}
console.log('func1 = ' + func1); // 'func1 = []'
for(i=0; i < a.length; i++) {
if(b.indexOf(a.indexOf(a[i])) > 1) {
func2.push(a[i])
}
}
console.log('func2 = ' + func2); // 'func2 = []'
}
d(a,b) // Ideally outputs ['cd', 'ef', 'fg']
This outputs:
func1 = cd,ef,fg
func2 = fg

use for(i=0; i < a.length; i++) instead! Proper syntax is the key to success! Thanks #Superdrac

Related

Javascript ... rest operator behavior using a new array value?

I have the following code below, the value that is being passed in is 4, 1, 9, 14, 6 ,8 and the
value which is assigned to newHeight is 1, 4, 6, 8, 9, 14. Insertion Sort sorts the array in ascending order.
var heightChecker = function(heights) {
var sorted = [...heights];
var newHeight = insertionSort(heights);
var count = 0;
for(var i = 0; i < newHeight.length; i++) {
if(newHeight[i] !== sorted[i]) {
count++;
}
}
return count;
}
insertionSort sorts the array, and when i use this line of code
var sorted = [...height];
Then it returns the answer I was looking for which is 3. However when i change the code to be
var heightChecker = function(heights) {
var newHeight = insertionSort(heights);
var count = 0;
for(var i = 0; i < heights.length; i++) {
if(newHeight[i] !== heights[i]) {
count++;
}
}
return count;
}
It returns the answer as 0.
I am not understanding why it isn't the same answer, and after debugging and google searching, I still cannot find why.
Here is the insertion sort code
function insertionSort(inputArr) {
let n = inputArr.length;
for (let i = 1; i < n; i++) {
// Choosing the first element in our unsorted subarray
let current = inputArr[i];
// The last element of our sorted subarray
let j = i-1;
while ((j > -1) && (current < inputArr[j])) {
inputArr[j+1] = inputArr[j];
j--;
}
inputArr[j+1] = current;
console.log(inputArr);
}
return inputArr;
}
a = [...b] creates a copy of b, a = b only assigns a different name to your value (i.e. a second reference pointing to the same value).
let a = [1,2,3];
b = a;
c = [...a];
a[1] = 41; // modifies value of a AND b
b[1] = 42; // modifies value of a AND b
c[1] = 43; // only modifies value of c
console.log(a); // 1, 42, 3
console.log(b); // 1, 42, 3
console.log(c); // 1, 43, 3
or, with a function call:
function addNumber(array, number) {
array.push(number);
}
let a = [1,2,3];
b = a;
c = [...a];
addNumber(b, 4); // now a = [1,2,3,4]; and b = [1,2,3,4] (=a); c = [1,2,3]
addNumber(c, 5); // still a = [1,2,3,4]; but c = [1,2,3,5]

adding values that meet a certain condition javascript

I have an array of strings array_strings and I want to get the maximum length of x consecutive values from the list and return a string. I have tried with this code but, its not working.
var myArr = ['we', 'make', 'the', 'best', 'dishes', 'in', 'cooking', 'class'];
x = 2;
function myFunc(array_strings, x) {
// your code
let val = '',
y = 0;
array_strings.sort((a, b) => {
return a.length < b.length
});
if (array_strings.length > 0) {
while (x) {
val += array_strings[y];
y++;
x--;
}
}
return val;
}
console.log(myFunc(myArr, x))
// expected output 'cookingclass'
on sorting, I cannot satisfy the consecutive order. Where am I going wrong?
To keep it simple, first get all possible values for x consecutive items.
function longestConsecutives(arr, x) {
var consecutives = [];
for (var i=0; i<arr.length-x; i++)
consecutives.push(arr.slice(i, x).join(''));
and then search the one with maximum length amongst them:
var longest = consecutives[0];
for (var i=1; i<consecutives.length; i++)
if (consecutives[i].length > longest.length)
longest = consecutives[i];
return longest;
}
There is also a more efficient approach to find the result, have a look at this for inspiration.
I don't think there's any reason to sort for this. You can just move a sliding window of x length over the array and keep track of the longest sequence you've seen:
let array_strings = ['we', 'make', 'the', 'best', 'dishes', 'in', 'cooking', 'class']
function findMax(array_strings, x){
let max_index = 0, max_length = 0
for (let i= 0; i < array_strings.length - x + 1; i++){
let length = array_strings.slice(i, i+x).reduce((l, c) => l + c.length, 0)
if (length > max_length) {
max_index = i,
max_length = length
}
}
return array_strings.slice(max_index, max_index + x)
}
console.log(findMax(array_strings,2))
console.log(findMax(array_strings,3))
console.log(findMax(array_strings,4))
Rolling sum O(n) solution:
function findLongestConsec(arr, l) {
var curSum = array_strings.slice(0, l).map(ele => ele.length).reduce((a, b) => a+b);
var curMax = curSum;
var curMaxIndex = 0;
for (var i = 1; i < arr.length - l + 1; i++) {
curSum = curMax - arr[i-1].length + arr[i+l-1].length;
if (curSum > curMax) {
curMax = curSum;
curMaxIndex = i;
}
}
return arr.slice(curMaxIndex, curMaxIndex + l).join('');
}
The previous solutions all involve string concatenation. I don't think it's worth doing it every time. We just have to keep track of the starting index of the longest subarray.
Try this:
arr = ['we', 'make', 'the', 'best', 'dishes', 'in', 'cooking', 'class']
x = 2
var sorting = function (array_strings, items) {
// your code
let val = '', y = 0;
const sort = array_strings.sort((a, b) => a.length < b.length);
console.log({sort});
if (arr.length > 0){
while (items){
val += array_strings[y];
y++;
items--;
}
}
return val;
}
var s = sorting(arr,x);
console.log({s});

Javascript: Given an array, return a number of arrays with different ordered combinations of elements

I need code that takes an array, counts the number of elements in it and returns a set of arrays, each displaying a different combination of elements. However, the starting element should be the same for each array. Better to explain with a few examples:
var OriginalArray = ['a','b','c']
should return
results: [['a','b','c'], ['a','c','b']]
or for example:
var originalArray = ['a','b','c','d']
should return
[['a','b','c','d'], ['a','b','d', 'c'], ['acbd', 'acdb', 'adbc', 'adcb']]
Again note how the starting element, in this case 'a' should always be the starting element.
You can use Heap's algorithm for permutations and modify it a bit to add to result only if first element is equal to first element of original array.
var arr = ['a', 'b', 'c', 'd']
function generate(data) {
var r = [];
var first = data[0];
function swap(x, y) {
var tmp = data[x];
data[x] = data[y];
data[y] = tmp;
}
function permute(n) {
if (n == 1 && data[0] == first) r.push([].concat(data));
else {
for (var i = 0; i < n; i++) {
permute(n - 1);
swap(n % 2 ? 0 : i, n - 1);
}
}
}
permute(data.length);
return r;
}
console.log(generate(arr))
You have to do a .slice(1) to feed the rest of the array to a permutations function. Then you can use .map() to stick the first item to the front of each array in the result of permutations function.
If you will do this job on large sets and frequently then the performance of the permutations function is important. The following uses a dynamical programming approach and to my knowledge it's the fastest.
function perm(a){
var r = [[a[0]]],
t = [],
s = [];
if (a.length <= 1) return a;
for (var i = 1, la = a.length; i < la; i++){
for (var j = 0, lr = r.length; j < lr; j++){
r[j].push(a[i]);
t.push(r[j]);
for(var k = 1, lrj = r[j].length; k < lrj; k++){
for (var l = 0; l < lrj; l++) s[l] = r[j][(k+l)%lrj];
t[t.length] = s;
s = [];
}
}
r = t;
t = [];
}
return r;
}
var arr = ['a','b','c','d'],
result = perm(arr.slice(1)).map(e => [arr[0]].concat(e));
console.log(JSON.stringify(result));

Comparing and Filtering two arrays

I've been trying to implement a function where given with two arrays,
array1's elements is used as conditions to filter out elements in array2.
For instance:
array1= [apple, grapes, oranges]
array2= [potato, pears, grapes, berries, apples, oranges]
After feeding into a function, array2 should have elements as such:
filter_twoArrays(array1,array2)
array2= [grapes, apples, oranges]
I've tried the following code, using for loops and array.splice(), but the problem I am seeing is that when I use the splice method, it seems that it changes the lengths of array2 in the for loop:
function filter_twoArrays(filter,result){
for(i=0; i< filter.length; i++){
for(j=0; j< result.length; j++){
if(filter[i] !== result[j]){
result.splice(j,1)
}
}
}
Any inputs will be greatly appreciated on how to refine the filter function
cheers!
You can use filter as follow
var array1 = ['apples', 'grapes', 'oranges', 'banana'],
array2 = ['potato', 'pears', 'grapes', 'berries', 'apples', 'oranges'];
var intersection = array1.filter(function(e) {
return array2.indexOf(e) > -1;
});
console.log(intersection);
You can also add this method on Array prototype and call it directly on array
Array.prototype.intersection = function(arr) {
return this.filter(function(e) {
return arr.indexOf(e) > -1;
});
};
var array1 = ['apples', 'grapes', 'oranges', 'banana'],
array2 = ['potato', 'pears', 'grapes', 'berries', 'apples', 'oranges'];
var intersection = array1.intersection(array2);
console.log(intersection);
You can use some, like this:
let newArray = array2.filter(
(array22) => !array1.some((array11) => array11.id === array22._id));
Hi this is a porting of the function array_intersect php. Should be good for you
http://phpjs.org/functions/array_intersect/
function array_intersect(arr1) {
// discuss at: http://phpjs.org/functions/array_intersect/
// original by: Brett Zamir (http://brett-zamir.me)
// note: These only output associative arrays (would need to be
// note: all numeric and counting from zero to be numeric)
// example 1: $array1 = {'a' : 'green', 0:'red', 1: 'blue'};
// example 1: $array2 = {'b' : 'green', 0:'yellow', 1:'red'};
// example 1: $array3 = ['green', 'red'];
// example 1: $result = array_intersect($array1, $array2, $array3);
// returns 1: {0: 'red', a: 'green'}
var retArr = {},
argl = arguments.length,
arglm1 = argl - 1,
k1 = '',
arr = {},
i = 0,
k = '';
arr1keys: for (k1 in arr1) {
arrs: for (i = 1; i < argl; i++) {
arr = arguments[i];
for (k in arr) {
if (arr[k] === arr1[k1]) {
if (i === arglm1) {
retArr[k1] = arr1[k1];
}
// If the innermost loop always leads at least once to an equal value, continue the loop until done
continue arrs;
}
}
// If it reaches here, it wasn't found in at least one array, so try next value
continue arr1keys;
}
}
return retArr;
}
You can use
const arr1 = [1, 2, 3];
const arr2 = [2, 3];
arr1.filter(e => arr2.indexOf(e) > -1 ? false : true); // [1]
Came here some week back to find a solution to a problem like this but its a pity I couldn't get what I wanted, but now I figured it out in a more simple way. using the arrow function, .filter() method and .includes() method.
Declare an arrow function that takes in two arguments:
const filterTwoArrays = (string1, string2) => string1.filter(item => string2.includes(item));
console.log(filterTwoArrays(array1, array2)).
Here is one simple way based on your code
function array_filter(filter, result) {
var filterLen = filter.length;
var resultLen = result.length;
for (i = 0; i < resultLen; i++) {
for (j = 0; j < filterLen; j++) {
if (!contains(filter, result[i]))
result.splice(i, 1);
}
}
}
//Return boolean depending if array 'a' contains item 'obj'
function contains(array, value) {
for (var i = 0; i < array.length; i++) {
if (array[i] == value) {
return true;
}
}
return false;
}
Since you have tagged javascript here is the solution.
function f1(x, y) {
var t = y.slice(0);
var r = [];
for (var i = 0; i < x.length; i++) {
for (var j = 0; j < y.length; j++) {
if (x[i] === y[j]) {
[].push.apply(r, t.splice(j, 1));
}
}
}
console.log(r)
y.length = 0;
[].push.apply(y, r);
}
Mark the items which are to be filtered out via delete result[index] manipulate them as needed.
JavaScript
window.onload = runs;
function runs() {
var array1 = ["apples", "grapes", "oranges"];
var array2 = ["potato", "pears", "grapes", "berries", "apples", "oranges"];
var result = filter_twoArrays(array1, array2);
function filter_twoArrays(filter, result) {
var i = 0,
j = 0;
for (i = 0; i < result.length; i++) {
var FLAG = 0;
for (j = 0; j < filter.length; j++) {
if (filter[j] == result[i]) {
FLAG = 1;
}
}
if (FLAG == 0) delete result[i];
}
return result;
}
var body = document.getElementsByTagName("body")[0];
var i = 0;
for (i = 0; i < result.length; i++) {
if (result[i] !== undefined)
body.innerHTML = body.innerHTML + result[i] + " ";
}
}
const func = array1.filter(item => array2.includes(item));

Output each combination of an array of numbers with javascript

I have several numbers in an array
var numArr = [1, 3, 5, 9];
I want to cycle through that array and multiply every unique 3 number combination as follows:
1 * 3 * 5 =
1 * 3 * 9 =
1 * 5 * 9 =
3 * 5 * 9 =
Then return an array of all the calculations
var ansArr = [15,27,45,135];
Anyone have an elegant solution? Thanks in advance.
A general-purpose algorithm for generating combinations is as follows:
function combinations(numArr, choose, callback) {
var n = numArr.length;
var c = [];
var inner = function(start, choose_) {
if (choose_ == 0) {
callback(c);
} else {
for (var i = start; i <= n - choose_; ++i) {
c.push(numArr[i]);
inner(i + 1, choose_ - 1);
c.pop();
}
}
}
inner(0, choose);
}
In your case, you might call it like so:
function product(arr) {
p = 1;
for (var i in arr) {
p *= arr[i];
}
return p;
}
var ansArr = [];
combinations(
[1, 3, 5, 7, 9, 11], 3,
function output(arr) {
ansArr.push(product(arr));
});
document.write(ansArr);
...which, for the given input, yields this:
15,21,27,33,35,45,55,63,77,99,105,135,165,189,231,297,315,385,495,693
I think this should work:
var a = [1, 3, 5, 9];
var l = a.length;
var r = [];
for (var i = 0; i < l; ++i) {
for (var j = i + 1; j < l; ++j) {
for (var k = j + 1; k < l; ++k) {
r.push(a[i] * a[j] * a[k]);
}
}
}
Edit
Just for my own edification, I figured out a generic solution that uses loops instead of recursion. It's obvious downside is that it's longer thus slower to load or to read. On the other hand (at least on Firefox on my machine) it runs about twice as fast as the recursive version. However, I'd only recommend it if you're finding combinations for large sets, or finding combinations many times on the same page. Anyway, in case anybody's interested, here's what I came up with.
function combos(superset, size) {
var result = [];
if (superset.length < size) {return result;}
var done = false;
var current_combo, distance_back, new_last_index;
var indexes = [];
var indexes_last = size - 1;
var superset_last = superset.length - 1;
// initialize indexes to start with leftmost combo
for (var i = 0; i < size; ++i) {
indexes[i] = i;
}
while (!done) {
current_combo = [];
for (i = 0; i < size; ++i) {
current_combo.push(superset[indexes[i]]);
}
result.push(current_combo);
if (indexes[indexes_last] == superset_last) {
done = true;
for (i = indexes_last - 1; i > -1 ; --i) {
distance_back = indexes_last - i;
new_last_index = indexes[indexes_last - distance_back] + distance_back + 1;
if (new_last_index <= superset_last) {
indexes[indexes_last] = new_last_index;
done = false;
break;
}
}
if (!done) {
++indexes[indexes_last - distance_back];
--distance_back;
for (; distance_back; --distance_back) {
indexes[indexes_last - distance_back] = indexes[indexes_last - distance_back - 1] + 1;
}
}
}
else {++indexes[indexes_last]}
}
return result;
}
function products(sets) {
var result = [];
var len = sets.length;
var product;
for (var i = 0; i < len; ++i) {
product = 1;
inner_len = sets[i].length;
for (var j = 0; j < inner_len; ++j) {
product *= sets[i][j];
}
result.push(product);
}
return result;
}
console.log(products(combos([1, 3, 5, 7, 9, 11], 3)));
A recursive function to do this when you need to select k numbers among n numbers. Have not tested. Find if there is any bug and rectify it :-)
var result = [];
foo(arr, 0, 1, k, n); // initial call
function foo(arr, s, mul, k, n) {
if (k == 1) {
result.push(mul);
return;
}
var i;
for (i=s; i<=n-k; i++) {
foo(arr, i+1, mul*arr[i], k-1, n-i-1);
}
}
This is a recursive function.
First parameter is array arr.
Second parameter is integer s. Each call calculates values for part of the array starting from index s. Recursively I am increasing s and so array for each call is recursively becoming smaller.
Third parameter is the value that is being calculated recursively and is being passed in the recursive call. When k becomes 1, it gets added in the result array.
k in the size of combination desired. It decreases recursively and when becomes 1, output appended in result array.
n is size of array arr. Actually n = arr.length
var create3Combi = function(array) {
var result = [];
array.map(function(item1, index1) {
array.map(function(item2, index2) {
for (var i = index2 + 1; i < array.length; i++) {
var item3 = array[i];
if (item1 === item2 || item1 === item3 || item2 === item3 || index2 < index1) {
continue;
}
result.push([item1, item2, item3]);
}
});
});
return result;
};
var multiplyCombi = function(array) {
var multiply = function(a, b){
return a * b;
};
var result = array.map(function(item, index) {
return item.reduce(multiply);
});
return result;
}
var numArr = [1, 3, 5, 9];
// create unique 3 number combination
var combi = create3Combi(numArr); //[[1,3,5],[1,3,9],[1,5,9],[3,5,9]]
// multiply every combination
var multiplyResult = multiplyCombi(combi); //[15,27,45,135];
https://github.com/dankogai/js-combinatorics
Found this library. Tested to be working. Below is from the library document:
var Combinatorics = require('js-combinatorics');
var cmb = Combinatorics.combination(['a','b','c','d'], 2);
while(a = cmb.next()) console.log(a);
// ["a", "b"]
// ["a", "c"]
// ["a", "d"]
// ["b", "c"]
// ["b", "d"]
// ["c", "d"]
Using node, you can do this pretty easily using a library. First install bit-twiddle using npm:
npm install bit-twiddle
Then you can use it in your code like this:
//Assume n is the size of the set and k is the size of the combination
var nextCombination = require("bit-twiddle").nextCombination
for(var x=(1<<(k+1))-1; x<1<<n; x=nextCombination(x)) {
console.log(x.toString(2))
}
The variable x is a bit-vector where bit i is set if the ith element is contained in the combination.

Categories

Resources