The problem, I'm trying to solve is as followed
we do have an array
const array = [[1, 2, 3], ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J"],["+", "-", "*", "/", "?"]];
we want to have an output like this:
const output = [[1,"A","+"],[1,"A","-"],[1,"A","*"],[1,"A","/"],[1,"A","?"],[1,"B","+"],[1,"B","-"],[1,"B","*"],[1,"B","/"],[1,"B","?"],[1,"C","+"],[1,"C","-"],[1,"C","*"],[1,"C","/"],[1,"C","?"],[1,"D","+"],[1,"D","-"],[1,"D","*"],[1,"D","/"],[1,"D","?"],[1,"E","+"],[1,"E","-"],[1,"E","*"],[1,"E","/"],[1,"E","?"],[1,"F","+"],[1,"F","-"],[1,"F","*"],[1,"F","/"],[1,"F","?"],[1,"G","+"],[1,"G","-"],[1,"G","*"],[1,"G","/"],[1,"G","?"],[1,"H","+"],[1,"H","-"],[1,"H","*"],[1,"H","/"],[1,"H","?"],[1,"I","+"],[1,"I","-"],[1,"I","*"],[1,"I","/"],[1,"I","?"],[1,"J","+"],[1,"J","-"],[1,"J","*"],[1,"J","/"],[1,"J","?"],[2,"A","+"],[2,"A","-"],[2,"A","*"],[2,"A","/"],[2,"A","?"],[2,"B","+"],[2,"B","-"],[2,"B","*"],[2,"B","/"],[2,"B","?"],[2,"C","+"],[2,"C","-"],[2,"C","*"],[2,"C","/"],[2,"C","?"],[2,"D","+"],[2,"D","-"],[2,"D","*"],[2,"D","/"],[2,"D","?"],[2,"E","+"],[2,"E","-"],[2,"E","*"],[2,"E","/"],[2,"E","?"],[2,"F","+"],[2,"F","-"],[2,"F","*"],[2,"F","/"],[2,"F","?"],[2,"G","+"],[2,"G","-"],[2,"G","*"],[2,"G","/"],[2,"G","?"],[2,"H","+"],[2,"H","-"],[2,"H","*"],[2,"H","/"],[2,"H","?"],[2,"I","+"],[2,"I","-"],[2,"I","*"],[2,"I","/"],[2,"I","?"],[2,"J","+"],[2,"J","-"],[2,"J","*"],[2,"J","/"],[2,"J","?"],[3,"A","+"],[3,"A","-"],[3,"A","*"],[3,"A","/"],[3,"A","?"],[3,"B","+"],[3,"B","-"],[3,"B","*"],[3,"B","/"],[3,"B","?"],[3,"C","+"],[3,"C","-"],[3,"C","*"],[3,"C","/"],[3,"C","?"],[3,"D","+"],[3,"D","-"],[3,"D","*"],[3,"D","/"],[3,"D","?"],[3,"E","+"],[3,"E","-"],[3,"E","*"],[3,"E","/"],[3,"E","?"],[3,"F","+"],[3,"F","-"],[3,"F","*"],[3,"F","/"],[3,"F","?"],[3,"G","+"],[3,"G","-"],[3,"G","*"],[3,"G","/"],[3,"G","?"],[3,"H","+"],[3,"H","-"],[3,"H","*"],[3,"H","/"],[3,"H","?"],[3,"I","+"],[3,"I","-"],[3,"I","*"],[3,"I","/"],[3,"I","?"],[3,"J","+"],[3,"J","-"],[3,"J","*"],[3,"J","/"],[3,"J","?"]]
We dont't know the size of the parent Array and children can have various sizes and types
#pilchard has point out this solution: All possible combinations of a 2d array in Javascript
function combos(list, n = 0, result = [], current = []){
if (n === list.length) result.push(current)
else list[n].forEach(item => combos(list, n+1, result, [...current, item]))
return result
}
I can confirm that it works
#Andrew
I tried this approach let me know if it will work.
const array = [
[1, 2, 3],
["A", "B", "C", "D", "E", "F", "G", "H", "I", "J"],
["+", "-", "*", "/", "?"]
];
let result;
let innerArray;
innerArray = array[0].map(a1 =>
array[1].map(a2 =>
array[2].map(a3 => [a1, a2, a3])
)
)
result = innerArray.flat(2)
console.log("Result", result)
You can use an inner map:
const array = [
[1, 2, 3],
['A', 'B', 'C'],
['+', '-', '*']
];
const res = array.map((e, i) => array.map(f => f[i]))
console.log(res)
I have 2 arrays:
[2, 4, -2, 4, 1, 3]
["a", "b", "c", "d", "e", "f"]
and I want them to be sorted by the numerical array:
// output
[-2, 1, 2, 3, 4, 4] // <-sorted by numerical order
["c", "e", "a", "f", "b", "d"] // sorted exactly the same order as the first array
while its actually not important if "b" or "d" comes first (they both have 4 in this example)
I found many questions about this online but none of them worked for me can anyone help me with that?
You could sort the keys of the first array based on their value. This will return an array with indices of the array sorted based on the value of numbers array. Then use map to get the sorted values based on the indices
const numbers = [2, 4, -2, 4, 1, 3],
alphabets = ["a", "b", "c", "d", "e", "f"]
const keys = Array.from(numbers.keys()).sort((a, b) => numbers[a] - numbers[b])
const sortedNumbers = keys.map(i => numbers[i]),
sortedAlphabets = keys.map(i => alphabets[i])
console.log(
sortedNumbers,
sortedAlphabets
)
A standard method is to take the indices of the key array for sorting and sort the indices as pattern for all other arrays by taking the index and the value from the key array.
At the end map the sorted arrays.
var array1 = [2, 4, -2, 4, 1, 3],
array2 = ["a", "b", "c", "d", "e", "f"],
indices = [...array1.keys()].sort((a, b) => array1[a] - array1[b]);
[array1, array2] = [array1, array2].map(a => indices.map(i => a[i]));
console.log(...array1);
console.log(...array2);
I would recommend storing the entire thing in a Map. That way, you can independently sort the first array however you want and then use those values as keys to call respective value of second array.
You can do this in a single line by associating your two arrays and then ordering the items:
const x = ["a", "b", "c", "d", "e", "f"]
const y = [2, 4, -2, 4, 1, 3]
const result = y.map((val, index)=>({x:x[index], y:val})).sort((a,b)=>a.y-b.y).map(v=>v.x)
// -> ["c", "e", "a", "f", "b", "d"]
Given the following array:
const arr = ["a", "c", "b", "c", "b"]
And given an index of 4, how can we return the index of the corresponding element in ["b", "b"]? In this example, the answer is 1.
More generally, how can we use the index of arr to find the corresponding index of b in arr2 where const arr2 = arr.filter(item => item === "b")?
Examples:
["a", "c", "b", "c", "b"][4] corresponds to ["b", "b"][1]
["a", "c", "b", "c", "b", "b"][4] corresponds to ["b", "b", "b"][1]
Inputs and expected outputs
function getCorrespondingIndexInFilteredArray(array, index, filterValue) {...}
getCorrespondingIndexInFilteredArray(["a", "c", "b", "c", "b"], 4, "b") // 1
getCorrespondingIndexInFilteredArray(["a", "c", "b", "c", "b", "b"], 4, "b") // 1
getCorrespondingIndexInFilteredArray(["b", "b"], 0, "b") // 0
getCorrespondingIndexInFilteredArray(["b", "b"], 1, "b") // 1
getCorrespondingIndexInFilteredArray(["a", "a"], 0, "b") // either 0 or -1 is alright here
You can use slice() upto the given index and then get the count of elements which are equal to element at given index.
const arr = ["a", "b", "c", "b"]
const other = (arr, ind) => {
return arr.slice(0,ind).reduce((ac, a) => a === arr[ind] ? ac + 1: ac, 0);
}
console.log(other(arr,3))
console.log(other(arr,1))
You could use a counter object and a for loop
function customIndex(arr, index) {
const counter = {};
for (let i = 0; i < arr.length; i++) {
counter[arr[i]] = counter[arr[i]] + 1 || 0;
if (index === i)
return counter[arr[i]]
}
return 'index out of bound'
}
console.log(customIndex(["a", "b", "c", "b"], 3))
console.log(customIndex(["a", "a", "b", "a"], 3))
console.log(customIndex(["a", "a", "b", "a"], 1))
You could filter the array to get the items which match arr[index] but only until index. (This will not short circuit like this first snippet)
const customIndex = (arr, index) =>
arr.filter((n, i) => n === arr[index] && i <= index).length - 1
console.log(customIndex(["a", "b", "c", "b"], 3))
console.log(customIndex(["a", "a", "b", "a"], 3))
console.log(customIndex(["a", "a", "b", "a"], 1))