Reorder the objects in the array - javascript

I have arrays as following
var A = ['C', 'D', 'E', 'F', 'G'];
var B = [3, 0, 4, 1, 2];
I need to rearrange array A with the given index values in array B. My solution to the problem is following
function reArrange(A,B){
var num;
var letter;
for(var i = 0; i < A.length; i++){
num = B[i];
letter = A[i];
A[num] = letter;
}
return A;
}
reArrange(A, B);
I get an output of ['D', 'C', 'E', 'C', 'E'] when it should be ['D', 'F', 'G', 'C', 'E']

Hope this will help.
var A = ['C', 'D', 'E', 'F', 'G'];
var B = [3, 0, 4, 1, 2];
var C = []
function reArrange(A,B){
var num;
var letter;
for(var i = 0; i < A.length; i++){
num = B[i];
letter = A[i];
C[num] = letter;
}
return C;
}
reArrange(A, B);
console.log(C)

You could use the second array for the indices for assinging the actual value of the given array.
var array = ['C', 'D', 'E', 'F', 'G'],
order = [3, 0, 4, 1, 2],
result = order.reduce((r, a, i) => (r[a] = array[i], r), []);
console.log(result); // ['D', 'F', 'G', 'C', 'E']

Simple and short
var A = ['C', 'D', 'E', 'F', 'G'];
var B = [3, 0, 4, 1, 2];
function reorderAB(first, second){
var result= new Array();
for(var i=0;i<second.length;i++){
result[second[i]]=first[i];
}
console.log(result);
}
reorderAB(A,B);

The problem is that you replaced array A with new value.
Example with the first i in your loop.
i = 0, num = 3 and letter = C
The original A[3] = F
Then you assign A[num] = letter, mean that A[3] (F) has new value/replaced C and so on.

You can create a Map from B -> A and then order the keys accordingly to output your expected array, however I am not sure what use case you are using it for.
const A = ['C', 'D', 'E', 'F', 'G'];
const B = [3, 0, 4, 1, 2];
let bToA = new Map();
B.forEach((a, i) => bToA.set(a, A[i]))
const final = [...bToA.keys()].sort().map(x => bToA.get(x))
console.log(final);

Related

Pushing spliced items returns nested array

I am trying to push 3 random items from an array into a new array. I use splice() to grab an item and remove it from the old array. When I push() the items, I would like to get them back in a single array.
For example:
["b", "f", "a"]
This is my code:
const letters = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j'];
let newletters = [];
for (let i = 0; i < 3; i++) {
newletters.push(letters.splice(Math.floor(Math.random() * letters.length), 1));
}
console.log(newletters);
It seems like I am pushing the spliced items as arrays into the new array, is there a way to fix this?
[
[
"b"
],
[
"f"
],
[
"a"
]
]
You could spread ... the array from Array#splice.
Or take the first element with and index.
letters.splice(Math.floor(Math.random() * letters.length)[0]
const letters = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j'];
let newletters = [];
for (let i = 0; i < 3; i++) {
newletters.push(...letters.splice(Math.floor(Math.random() * letters.length), 1));
}
console.log(newletters);
You can use the function Array.prototype.concat instead.
const letters = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j'];
let newletters = [];
for (let i = 0; i < 3; i++) {
newletters = newletters.concat(letters.splice(Math.floor(Math.random() * letters.length), 1));
}
console.log(newletters);
Use the ... operator inside the push statement.
const letters = ['a', 'b', 'c', 'd', 'e'];
const newLetters = []
newLetters.push(...letters.splice(1, 2)) // Equal to newLetters.push('b', 'c')
console.log(newletters)
const letters = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j'];
let newletters = [];
for (let i = 0; i < 3; i++) {
newletters.push(letters.splice(Math.floor(Math.random() * letters.length), 1));
}
console.log(newletters.flat());

Looping through nested arrays and reordering them in JavaScript

I am trying to loop through nested arrays and reorder them into new nested arrays. For example, take [[a,b,c,d], [e,f,g,h], [i,j,k,l]] and change it into [[a,e,i], [b,f,j], [c,g,k], [d,h,l]]
let rowArr = [[a,b,c,d], [e,f,g,h], [i,j,k,l]];
let newRowArr = [];
let length = rowArr.length;
for(let i = 0; i<length; i++){
for(let j = 0; j<rowArr.length; j++){
newRowArr.push(rowArr[i][j]);
j+=rowArr.length;
}
console.log(newRowArr) //I get only [a,e,i]
I am missing something obvious but why won't it loop the additional times to push the other letters into the array?
You could just use the nested loop where i and j are names for outer in inner index names and then use them to add to new array as result[j][i] = current inner loop value
let arr = [
['a', 'b', 'c', 'd'],
['e', 'f', 'g', 'h'],
['i', 'j', 'k', 'l']
];
const result = []
arr.forEach((a, i) => {
a.forEach((e, j) => {
if (!result[j]) result[j] = []
result[j][i] = e
})
})
console.log(result)
You can use Array.prototype.map:
let rowArr = [['a', 'b', 'c', 'd'], ['e', 'f', 'g', 'h'], ['i', 'j', 'k', 'l']];
let newRowArr = [];
let arraySize = 4;
const arrayColumn = (arr, n) => arr.map(x => x[n]);
for (let i = 0; i < arraySize; i++) {
newRowArr.push(arrayColumn(rowArr, i));
}
console.log(newRowArr);

How do I easily combine elements from two arrays in Javascript, alternating elements?

I have two arrays in JavaScript, of potentially different lengths:
var x = ['a', 'b', 'c'];
var y = ['g', 'h', 'i', 'j'];
I'd like to combine them into one array:
var z = ['a', 'g', 'b', 'h', 'c', 'i', 'j'];
How can I do that in JavaScript?
I see you answered your question at the same time as asking it. That's fine, but it's now clear that you were looking for a solution that leverages a library (eg, lodash) and not necessarily one that teaches you how to build such a procedure. In retrospect, I would've answered this differently, but nevertheless I think you can learn something from this answer.
I would recommend calling this something other than zip just because zip is used as name for a procedure that does something quite different from what you're looking for.
Here's a simple recursive definition of interleave -
const interleave = ([ x, ...xs ], ys = []) =>
x === undefined
? ys // base: no x
: [ x, ...interleave (ys, xs) ] // inductive: some x
const xs = [ 'a', 'b', 'c' ]
const ys = [ 'g', 'h', 'i', 'j' ]
console .log (interleave (xs, ys))
// [ a, g, b, h, c, i, j ]
And another variation that supports any number of input arrays -
const interleave = ([ x, ...xs ], ...rest) =>
x === undefined
? rest.length === 0
? [] // base: no x, no rest
: interleave (...rest) // inductive: no x, some rest
: [ x, ...interleave (...rest, xs) ] // inductive: some x, some rest
const ws = [ '0', '1', '2', '3' ]
const xs = [ 'a', 'b', 'c' ]
const ys = [ 'd', 'e', 'f' ]
const zs = [ 'g', 'h', 'i', 'j' ]
console .log (interleave (ws, xs, ys, zs))
// [ 0, a, d, g, 1, b, e, h, 2, c, f, i, 3, j ]
tl;dr: z = _.flatten(_.zip(x, y)).filter(element => element), as long as you don't care about null elements in the original arrays.
Some of the libraries providing functional tools, such as Lodash, provide enough mechanics to easily do this. For example, you can do this:
var z1 = _.zip(x, y);
// z1 is now [["a","g"],["b","h"],["c","i"],[null,"j"]]
var z2 = _.flatten(z1);
// z2 is now ["a","g","b","h","c","i",null,"j"]
var z3 = z2.filter(element => element)
// z3 is now ["a","g","b","h","c","i","j"]
Note that this will only work if the original arrays do not contain any null elements, as they are filtered out by the last step.
A simple implementation that will stitch the arrays:
function stitch(x, y) {
var arr = [];
var length = Math.max(x.length, y.length);
for(var i = 0; i < length; i++) {
i < x.length && arr.push(x[i]);
i < y.length && arr.push(y[i]);
}
return arr;
}
var x = ['a', 'b', 'c'];
var y = ['g', 'h', 'i', 'j'];
console.log(stitch(x, y));
This is the functional way to address the problem:
var x = ['a', 'b', 'c'];
var y = ['g', 'h', 'i', 'j'];
function stitch(x,y) {
var a = x.length > y.length ? x : y;
var b = x.length > y.length ? y : x;
var c = a.map(function (e, i) {
return b.length<i ? [e, b[i]] : [];
});
return [].concat.apply([],c)
}
Here's a very simple recursive solution:
const interlace = (xxs, ys) => {
if (xxs.length === 0) return ys;
const [x, ...xs] = xxs;
return [x, ...interlace(ys, xs)];
};
const xs = ['a', 'b', 'c'];
const ys = ['g', 'h', 'i', 'j'];
console.log(JSON.stringify(interlace(xs, ys)));
In addition, you can easily generalize this algorithm to an arbitrary number of arrays:
const interlace = (...xss) => xss.length > 0 ? interleave(...xss) : [];
const interleave = (xxs, ...yss) => {
if (xxs.length === 0) return interlace(...yss);
const [x, ...xs] = xxs;
return [x, ...interleave(...yss, xs)];
};
const xs = ['a', 'b', 'c'];
const ys = ['g', 'h', 'i', 'j'];
const zs = ['d', 'e', 'f'];
console.log(JSON.stringify(interlace()));
console.log(JSON.stringify(interlace(xs)));
console.log(JSON.stringify(interlace(xs, ys)));
console.log(JSON.stringify(interlace(xs, ys, zs)));
Hope that helps.
This can be done in regular Javascript. No need for fancy tricks:
function splicer(array, element, index) {
array.splice(index * 2, 0, element);
return array;
}
function weave(array1, array2) {
return array1.reduce(splicer, array2.slice());
}
var x = ['a', 'b', 'c'];
var y = ['g', 'h', 'i', 'j'];
var z = weave(x, y);
console.log(z);
var x = ['a', 'b', 'c'];
var y = ['g', 'h', 'i', 'j'];
var z=[];
if(y.length>=x.length){
for(var i=0;i<x.length;i++){
z.push(x[i]);
z.push(y[i]);
}
while(i<y.length)
z.push(y[i++]);
}else{
for(var i=0;i<y.length;i++){
z.push(x[i]);
z.push(y[i]);
}
while(i<x.length)
z.push(x[i++]);
}
window.alert(JSON.stringify(z)); // print ["a","g","b","h","c","i","j"]

How to get all unique elements in for an array of array but keep max count of duplicates

The question doesn't make much sense but not sure how to word it without an example. If someone can word it better, feel free to edit it.
Let's say I have an array of arrays such as this:
[ ['a', 'a', 'b', 'c'], [], ['d', 'a'], ['b', 'b', 'b', 'e'] ]
I would like the output to be:
['a', 'a', 'b', 'b', 'b', 'c', 'd', 'e']
Not sure if there is an easy way to do this in javascript/jquery/underscore. One way I could think of is to look through each of these arrays and count up the number of times each element shows up and keep track of the maximum amount of times it shows up. Then I can recreate it. But that seems pretty slow considering that my arrays can be very large.
You need to:
Loop over each inner array and count the values
Store each value and its count (if higher than current count) in a counter variable
In the end, convert the value and counts into an array
Following code shows a rough outline of the process. Remember to replace .forEach and for..in with appropriate code:
var input = [['a', 'a', 'b', 'c'], [], ['d', 'a'], ['b', 'b', 'b', 'e']],
inputCount = {};
input.forEach(function(inner) {
var innerCount = {};
inner.forEach(function(value) {
innerCount[value] = innerCount[value] ? innerCount[value] + 1 : 1;
});
var value;
for (value in innerCount) {
inputCount[value] = inputCount[value] ? Math.max(inputCount[value], innerCount[value]) : innerCount[value];
}
});
console.log(inputCount);
// Object {a: 2, b: 3, c: 1, d: 1, e: 1}
After messing around, I found a solution but not sure if I like it enough to use. I would probably use it if I can't think of another one.
I would use underscorejs countBy to get the count of all the elements.
var array = [ ['a', 'a', 'b', 'c'], [], ['d', 'a'], ['b', 'b', 'b', 'e'] ];
var count = _.map(array, function(inner) {
return _.countBy(inner, function(element) {
return element;
});
});
var total = {};
_.each(_.uniq(_.flatten(array)), function(element) {
var max = _.max(count, function(countedElement) {
return countedElement[element];
});
total[element] = max[element];
});
console.log(total); // {a: 2, b: 3, c: 1, d: 1, e: 1}
Then I would recreate the array with that total.
Here is example of simple nested loop approach:
var input = [ ['a', 'a', 'b', 'c'], [], ['d', 'a'], ['b', 'b', 'b', 'e'] ];
var countMap = {};
// iterate outer array
for (i=0; i < input.length; i++) {
// iterate inner array
for (j=0; j < input[i].length; j++) {
// increment map counter
var value = input[i][j];
if (countMap[input[i][j]] === undefined) {
countMap[value] = 1;
} else {
countMap[value]++;
}
}
}
console.log(countMap); // output such as {'a':2, 'b':4, 'c':1, 'd':1, 'e':1}
Not the most efficient solution but it should describe you the process:
var big = [ ['a', 'a', 'b', 'c'], [], ['d', 'a'], ['b', 'b', 'b', 'e'] ];
function map(arr){
var map = {}
for (var i=arr.length-1; i>-1; i--){
if(arr[i] in map) map[arr[i]]++;
else map[arr[i]] = 1;
}
return map;
}
function reduce(matrix){
var arrMap = {};
for (var i=matrix.length-1; i>-1; i--){
var arrRes = map(matrix[i]);
for (var key in arrRes){
if( !arrMap[key] || arrMap[key] < arrRes[key])
arrMap[key] = arrRes[key];
}
}
return arrMap;
}
function calc(matrix){
var res = [],
arrMap = reduce(matrix);
for (var key in arrMap){
while(arrMap[key] > 0 ){
res.push(key);
arrMap[key]--;
}
}
return res;
}
console.log(calc(big));
// Array [ "e", "b", "b", "b", "a", "a", "d", "c" ]

Split array into two arrays

var arr = ['a', 'b', 'c', 'd', 'e', 'f'];
var point = 'c';
How can I split the "arr" into two arrays based on the "point" variable, like:
['a', 'b']
and
['d', 'e', 'f']
var arr2 = ['a', 'b', 'c', 'd', 'e', 'f'];
arr = arr2.splice(0, arr2.indexOf('c'));
To remove 'c' from arr2:
arr2.splice(0,1);
arr contains the first two elements and arr2 contains the last three.
This makes some assumptions (like arr2 will always contain the 'point' at first assignment), so add some correctness checking for border cases as necessary.
Use indexOf and slice
var arr = ['a', 'b', 'c', 'd', 'e', 'f'];
var indexToSplit = arr.indexOf('c');
var first = arr.slice(0, indexToSplit);
var second = arr.slice(indexToSplit + 1);
console.log({first, second});
Sharing this convenience function that I ended up making after visiting this page.
function chunkArray(arr,n){
var chunkLength = Math.max(arr.length/n ,1);
var chunks = [];
for (var i = 0; i < n; i++) {
if(chunkLength*(i+1)<=arr.length)chunks.push(arr.slice(chunkLength*i, chunkLength*(i+1)));
}
return chunks;
}
Sample usage:
chunkArray([1,2,3,4,5,6],2);
//returns [[1,2,3],[4,5,6]]
chunkArray([1,2,3,4,5,6,7],2);
//returns [[1,2,3],[4,5,6,7]]
chunkArray([1,2,3,4,5,6],3);
//returns [[1,2],[3,4],[5,6]]
chunkArray([1,2,3,4,5,6,7,8],3);
//returns [[1,2],[3,4,5],[6,7,8]]
chunkArray([1,2,3,4,5,6,7,8],42);//over chunk
//returns [[1],[2],[3],[4],[5],[6],[7],[8]]
Try this one:
var arr = ['a', 'b', 'c', 'd', 'e', 'f'];
var point = 'c';
var idx = arr.indexOf(point);
arr.slice(0, idx) // ["a", "b"]
arr.slice(idx + 1) // ["d", "e", "f"]
var arr = ['a', 'b', 'c', 'd', 'e', 'f'];
var point = 'c';
Array.prototype.exists = function(search){
for (var i=0; i<this.length; i++) {
if (this[i] == search) return i;
}
return false;
}
if(i=arr.exists(point))
{
var neewArr=arr.splice(i);
neewArr.shift(0);
console.log(arr); // output: ["a", "b"]
console.log(neewArr); // output: ["d", "e", "f"]
}​
Here is an example.
var arr = ['a', 'b', 'c', 'd', 'e', 'f'];
var point = 'c';
var i = arr.indexOf(point);
var firstHalf, secondHalf, end, start;
if (i>0) {
firstHalf = arr.slice(0, i);
secondHalf = arr.slice(i + 1, arr.length);
}
//this should get you started. Can you think of what edge cases you should test for to fix?
//what happens when point is at the start or the end of the array?
When splitting the array you are going to want to create two new arrays that will include what you are splitting, for example arr1 and arr2. To populate this arrays you are going to want to do something like this:
var arr1, arr2; // new arrays
int position = 0; // start position of second array
for(int i = 0; i <= arr.length(); i++){
if(arr[i] = point){ //when it finds the variable it stops adding to first array
//starts adding to second array
for(int j = i+1; j <= arr.length; j++){
arr2[position] = arr[j];
position++; //because we want to add from beginning of array i used this variable
}
break;
}
// add to first array
else{
arr1[i] = arr[i];
}
}
There are different ways to do this! good luck!
Yet another suggestion:
var segments = arr.join( '' ).split( point ).map(function( part ) {
return part.split( '' );
});
now segments contains an array of arrays:
[["a", "b"], ["d", "e", "f"]]
and can get accessed like
segments[ 0 ]; // ["a", "b"]
segments[ 1 ]; // ["d", "e", "f"]
if you want to split into equal half; why no simple while loop ?
var arr = ['a', 'b', 'c', 'd', 'e', 'f'];
var c=[];
while(arr.length > c.length){
c.push(arr.splice(arr.length-1)[0]);
}
Kaboom :).
Separate two arrays with given array elements as string array and number array;
let arr = [21,'hh',33,'kk',55,66,8898,'rtrt'];
arrStrNum = (arr) => {
let str = [],num = [];
for(let i = 0;i<arr.length;i++){
if(typeof arr[i] == "string"){
str.push(arr[i]);
}else if(typeof arr[i] == "number"){
num.push(arr[i]);
}
}
return [str, num]
}
let ans = arrStrNum(arr);
let str = ans[0];
let num = ans[1];
console.log(str);
console.log(num);

Categories