How to change places in an Array? - javascript

There's a list of 5 elements. Per default, the list (array) should show ["A", "B", "C", "D", "E"]. Every second the elements should rotate by one position:
List after 1 second: ["B", "C", "D", "E", "A"];
After 2 seconds: ["C", "D", "E", "A", "B"];
After 3 seconds: ["D", "E", "A", "B", "C"];
I'm learning and I need help. How would you solve this problem? This is my incorrect solution:
const testArray = ["A", "B", "C", "D", "E"];
const swap = function(theArray, indexA, indexB) {
let temp = theArray[indexA];
theArray[indexA] = theArray[indexB];
theArray[indexB] = temp;
};
swap(testArray, 0, 1);
console.log(testArray);

For no reason other than it's stupidly short.
let r = ['A','B','C','D','E'];
setInterval(_ => {r.push(r.shift());console.log(r)},1000)

You can try array.shift method:
const arr = [1, 2, 3, 4, 5];
setInterval(() => {
const firstElement = arr.shift();
arr.push(firstElement);
console.log(arr);
}, 1000);

this is a shorter version of the swap
const testArray = ["A", "B", "C", "D", "E"];
const swap = function(theArray) {
theArray.push(theArray.shift())
};
swap(testArray);
console.log(testArray);

You can shift the first element off the array, and then push it to the end of the array.
const arr = ['A', 'B', 'C', 'D', 'E'];
function swap(arr) {
// Log the joined array
console.log(arr.join(''));
// `shift` the first element off the array
// and `push` it back on
arr.push(arr.shift());
// Repeat by calling `swap` again after
// one second, passing in the updated array
setTimeout(swap, 1000, arr);
}
swap(arr);
Addition documentation
setTimeout

Related

How to remove duplicates array in multidimensional array where the key positions might not same in JavaScript?

let originalArray = [
["A", "B", "C"],
["B", "A", "C"],
["D", "E", "F"]
];
let uniqueArray = originalArray.filter((item, index, self) => {
return index === self.findIndex((t) => JSON.stringify(t) === JSON.stringify(item));
});
console.log(uniqueArray);
I want this result:
[
["A", "B", "C"],
["D", "E", "F"]
];
To remove duplicates from a multidimensional array in JavaScript, where the key positions might not be the same, you can use a combination of Array.prototype.map(), Array.prototype.filter(), and Array.prototype.includes() to create a new array with only unique elements.
You can use an object with Array.prototype.reduce, storing a stringified version of the sorted values as the cache.
let originalArray = [
["A", "B", "C"],
["B", "A", "C"],
["D", "E", "F"]
];
let uniqueArray = Object.values(originalArray.reduce((acc, cur) => {
const data = [...cur].sort();
const key = JSON.stringify(data);
acc[key] = acc[key] ?? cur;
return acc;
}, {}));
console.log(uniqueArray);
You can sort the inner arrays before inserting them into a Set for filtering.
let originalArray = [
["A", "B", "C"],
["B", "A", "C"],
["D", "E", "F"]
];
let vis = new Set;
let res = originalArray.filter(x => {
const str = JSON.stringify([...x].sort());
return !vis.has(str) && vis.add(str);
});
console.log(res);

Merging array of arrays

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)

how to get the x values from the sorted 'y' array [duplicate]

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"]

Use index of array to find the corresponding index in a filtered version of that array

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))

js: how to filter out the repeat item from a two dimensional Array?

Is there any way can solve the below problem:
this is the origin data:
var m = [["a", "b"], ["c", "d", "e"], ["f", "g", "h"]]
var n = ["a","d","e", "h"]
I want to get the data:
[["a"], ["d","e"], ["h"]]
I've tried:
function remove(arr, val) {
arr.forEach(v => {
var index = v.indexOf(val);
if (index === -1) {
v.splice(index, 1);
}
});
};
n.forEach(val=>{
remove(m, val);
})
console.log(m)
But i faild....
Is there a solution to solve the problem??
If you're OK with a new array, you can map and filter m by whether the item being iterated over is included in n:
var m = [["a", "b"], ["c", "d", "e"], ["f", "g", "h"]]
var n = ["a","d","e", "h"]
const filteredM = m.map(
arr => arr.filter(
item => n.includes(item)
)
);
console.log(filteredM);
You could iterate from the end of the array and splice not common items.
It is necessary to start from the end, because splice (in this case) changes the length of the array and the actual index, if iterating from start.
var m = [["a", "b"], ["c", "d", "e"], ["f", "g", "h"]],
n = ["a", "d", "e", "h"];
m.forEach(a => {
var i = a.length;
while (i--) {
if (!n.includes(a[i])) {
a.splice(i, 1);
}
}
});
console.log(m); // [["a"], ["d", "e"], ["h"]]
.as-console-wrapper { max-height: 100% !important; top: 0; }
var m = [["a", "b"], ["c", "d", "e"], ["f", "g", "h"]];
var n = ["a","d","e", "h"];
const newarr = m.map(val => val.filter(element => n.includes(element)));
console.log(newarr);

Categories

Resources