JavaScript. Push and read from Multidimensional Array - javascript

I am trying to push values into a multidimensional array and read values out of it based on code that i've seen on other posts on this site. This is my array push code.
SelectedWindowGraphs.push([ContainerIDValue,elementID+"chkbox"]);
ContainerIDValue is an integer and elementID+"chkbox" is what i want to store at that position in the array. Here is what i saw when i debugged my code:
This is not what i want. At position 0, i want CUT17chkbox, CUT18chkbox, and CUT19chkbox. How do i fix my array so that i does that?

// initialize an array at that position in case it has not been defined yet
SelectedWindowGraphs[ContainerIDValue] = (SelectedWindowGraphs[ContainerIDValue] ||
[]);
// push the value at the desired position
SelectedWindowGraphs[ContainerIDValue].push(elementID+"chkbox");

You have to push to a subarray:
if(!SelectedWindowGraphs[ContainerIDValue])
SelectedWindowGraphs[ContainerIDValue] = [];
SelectedWindowGraphs[ContainerIDValue]
.push(elementID+"chkbox");

You could add elements at certain position just doing:
var arr = [ 1, 2, 3, 4, 5, 6, 7 ]
arr[2] = "three";
console.log(arr);//[ 1, 2, 'three', 4, 5, 6, 7 ]
In a multidimensional array:
var arr = [ 1, [2, 3, 4, 5, 6], 7 ]
arr[1][2] = "four";
console.log(arr);//[ 1, [ 2, 3, 'four', 5, 6 ], 7 ]
When you perform push you are adding one or more elements at the end.
var arr = [1,2,3]
arr.push(4,5);//you are adding 4 and then 5
console.log(arr);//[ 1, 2, 3, 4, 5 ]
In a multidimensional array:
var arr = [1,2,[3,4]]
arr[2].push(5,6);//position 2
console.log(arr);//[ 1, 2, [ 3, 4, 5, 6 ] ]
To insert an element in a specific position (and move right element n positions) you could use splice(). In the following case, 2th and 3th position
var arr = [ 1, 2, 3, 4, 5 ]
arr.splice(2, 0, 999, 8888);
console.log(arr);//[ 1, 999, 8888, 2, 3, 4, 5 ]
In a multidimensional array:
var arr = [ 1, 2, [3,4,5], 6, 7 ]
arr.splice(2, 0, [8,9,10]);
console.log(arr);//[ 1, 2, [ 8, 9, 10 ], [ 3, 4, 5 ], 6, 7 ]

Related

How do I multiply an array of numbers with another array of numbers in javascript

I've been trying to make an array of numbers be able to times another array of numbers without doing array.join("") * array2.join("").
I've tried a lot of methods such as:
var input = [3, 6, 4];
var scalar = 5;
var output = input.map(x => x * scalar); // [15, 30, 20]
Although that's only one number the array can multiply to.
I'd like a function that can do:
var array = [ 1, 3, 2 ];
var array2 = [ 5, 3, 8, 2, 3, 5, 2 ];
someFunction(array, array2);
// [ 7, 1, 0, 4, 7, 0, 4, 6, 4 ]
Please note I don't want it to be something like
array.join("") * array2.join("")
I'm willing to give all my reputation as a bounty if someone is able to answer my question.
If scientific notation is the problem, turn the arrays into BigInts instead.
var array = [ 1, 3, 2, 4, 5, 6, 7, 8, 9, 1, 2, 3, 4, 5 ];
var array2 = [ 5, 3, 8, 2, 3, 5, 2, 4, 5, 6, 7, 8, 9, 1, 2, 3, 4, 5 ];
const someFunction = (arr1, arr2) => [...String(
BigInt(arr1.join('')) * BigInt(arr2.join(''))
)].map(Number);
console.log(someFunction(array, array2));

Javascript: Manipulate array to return shortest node

I have an array as below
var a = [ [ 5, 5, 1, -4 ], [ 3, 7, 3, -1 ], [ 7, 3, 4, 1 ], [ 5, 5, 5, 0 ] ]
Every nested array index:2 element indicates its distance from 0(Zero) and every index:3 element indicate how near it is to its next poll point.
I am trying to sort this array so I can get nested array which is near to index:0 with reference to index:2 element and its next poll point is very near.
For example, my answer here is [ 3, 7, 3, -1 ] because
though [ 5, 5, 1, -4 ] , index:2 is very near to 0 its next point is located at after/before 4 positions. But for [ 3, 7, 3, -1 ] next poll point is at one position.
I tried with sorting like below
js
inDiff = inDiff.sort( function(a,b ){
if ( a[2] < b[2]) {
if ( Math.abs(a[3]) < Math.abs(b(3)) ){
return Math.abs(b[3]) - Math.abs(a[3]);
}
}
});
Update 1
As asked in the comment I am adding explanation to each element of the nested array. For example in nested array [ 5, 5, 1, -4 ]
Index:0: Value 5 Represents 1st Number that I am looking for
Index:1 Value 5 Represents 2nd Number ( next poll point number)
By Adding these two numbers I will achieve my requirement of finding two numbers which can sum up for 10.
Index 2 : Value 1 : Indicates index of 1st number nothing but 5 in the source array
Index 3 : Value -4 : Indicates difference between indexes of Index:0 and Index:1 number of nested array from source array.
But nothing happens with my array.
Any help, much appreciated.
Assuming the input always follows requirements this demo uses .reduce()
const AA = [
[5, 5, 1, -4],
[3, 7, 3, -1],
[7, 3, 4, 1],
[5, 5, 5, 0]
];
let result = AA.reduce((min, now, idx, AoA) => {
let distTo0 = array => Math.abs(Math.floor(array[2]) + Math.floor(array[3]));
min = distTo0(now) < distTo0(min) ? now : min;
return min;
});
console.log(result);
The following demo includes all of the rules as I understood them:
const crazyLogic = arrayOfArrays => {
let AAClone = JSON.parse(JSON.stringify(arrayOfArrays));
const shared = AAClone[0][0] + AAClone[0][1];
const rules = [`Must be an array of number arrays`, `The sum of index 0 and 1 of each sub-array must be identical`, `Mismatched sub-array lengths`];
let message = !AAClone.every(sub => Array.isArray(sub)) ? rules[0] : !AAClone.every(sub => sub.every(num => num + 0 === num)) ? rules[0] : !AAClone.every(sub => sub[0] + sub[1] === shared) ? rules[1] : !AAClone.every(sub => sub.length === 4) ? rules[2] : null;
if (message !== null) {
return message;
}
return AAClone.reduce((min, now, idx, AoA) => {
let distTo0 = array => Math.abs(Math.floor(array[2]) + Math.floor(array[3]));
min = distTo0(now) < distTo0(min) ? now : min;
return min;
});
};
/* Input rules:
1. Must be an array of arrays (only numbers)
2. Each sum of subArray[0] + subArray[1] must be identical
3. Each subArray.length = 4
*/
// AAa returns [ 3, 7, 3, -1 ]
const AAa = [
[5, 5, 1, -4],
[3, 7, 3, -1],
[7, 3, 4, 1],
[5, 5, 5, 0]
];
// AA1 breaks rule 1
const AA1 = [
[5, 5, 1, -4],
[3, 7, 3, -1],
[7, 3, ' X', 1],
[5, 5, 5, 0]
];
// AAO breaks rule 1
const AAO = [
[5, 5, 1, -4],
[3, 7, 3, -1],
[7, 3, 4, 1],
[5, 5, 5, 0], {}
];
// AA2 breaks rule 2
const AA2 = [
[5, 5, 1, -4],
[3, 17, 3, -1],
[7, 3, 4, 1],
[5, 5, 5, 0]
];
// AA3 breaks rule 3
const AA3 = [
[5, 5, 1, -4],
[3, 7, 3, -1],
[7, 3, 4, 1],
[5, 5, 5]
];
console.log(crazyLogic(AAa));
console.log(crazyLogic(AA1));
console.log(crazyLogic(AAO));
console.log(crazyLogic(AA2));
console.log(crazyLogic(AA3));

Adding elements from each sub array in a 2D array in Javascript

So I have this 2D permutations array of ints which looks like this:
arr = [
[ 5, 2, 6 ],
[ 2, 5, 6 ],
[ 6, 5, 2 ],
[ 5, 6, 2 ],
[ 2, 6, 5 ],
[ 6, 2, 5 ]
]
and essentially I want to be able to get a string that looks like this '652,625,562,526,256'
This means that the numbers are ordered and are in string format.
What I have done so far is:
arr.map(c => c.join("")).join()
Which combines it to a array, however now my thought process would be to convert this to a array of ints and then order and re-parse as strings, but there must be some kind of easier way to do this?
I'm quite new to JavaScript so any help is appreciated.
Don't do the second join immediately - instead, sort the array of joined strings first, then join:
const arr = [
[ 5, 2, 6 ],
[ 2, 5, 6 ],
[ 6, 5, 2 ],
[ 5, 6, 2 ],
[ 2, 6, 5 ],
[ 6, 2, 5 ]
];
const result = arr
.map(subarr => subarr.join(''))
.sort((a, b) => b.localeCompare(a, undefined, { numeric: true }))
.join();
console.log(result);
or map to numbers and subtract in the comparator:
const arr = [
[ 5, 2, 6 ],
[ 2, 5, 6 ],
[ 6, 5, 2 ],
[ 5, 6, 2 ],
[ 2, 6, 5 ],
[ 6, 2, 5 ]
];
const result = arr
.map(subarr => Number(subarr.join('')))
.sort((a, b) => b - a)
.join();
console.log(result);

Why does shifting an array within an array affect copied (not by reference) arrays, but shifting the whole initial array does not? (Javascript)

I am trying to shift an array and use that array, then set it to its original values using a copied array that was copied not by reference. For some reason, shifting arrays from the original array does not affect the copied array, but shifting elements from within arrays in the initial array does affect the copied array. How do I copy an array, modify it, use it, then set it back to its original form?
I originally was having trouble because I was copying by reference, but then I learned that using the rest operator allowed me to copy the array (not by reference). It seems the two datum are still linked.
var m = [[1,2,3],[4,5,6],[7,8,9]]
var matrix = [...m]
m.shift();
console.log(matrix);
m[0].shift();
console.log(m);
console.log(matrix);
Expected:
[ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ] ]
[ [ 5, 6 ], [ 7, 8, 9 ] ]
[ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ] ]
Actual:
[ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ] ]
[ [ 5, 6 ], [ 7, 8, 9 ] ]
[ [ 1, 2, 3 ], [ 5, 6 ], [ 7, 8, 9 ] ]
The problem is that the second degree arrays are still being passed by reference when you do [...m]. I'm not sure how you could still use the rest operator but another thing you could do is:
var matrix = JSON.parse(JSON.stringify(m));
to avoid the problem.
var m = [[1,2,3],[4,5,6],[7,8,9]];
var matrix = JSON.parse(JSON.stringify(m));
m.shift();
console.log(matrix);
m[0].shift();
console.log(m);
console.log(matrix);

Merge property values of 2 objects into an array using underscore chain

I have the following:
a = {x:1, y:3, w:4}
b = {c:2, d:3}
And I want to obtain all the values of these objects iterating only once.
result = [1, 3, 4, 2, 3]
I have the following solution but it has multiple iterations.
result = _.chain(a).values().union(_.values(b)).value();
I would like to avoid the "_.values(b)" and do this using the same chain from a.
I also tried this, but it is not working properly:
result = _.chain({}).extend(a,b).values().value();
If you're intent on chaining, then
_.chain([a, b]) . // [ { x: 1, y: 3, w: 4 }, { c: 2, d: 3 } ]
map(_.values) . // [ [ 1, 3, 4 ], [ 2, 3 ] ]
flatten() . // [ 1, 3, 4, 2, 3 ]
uniq() . // [ 1, 3, 4, 2 ]
value()
How about.
var a = {x:1, y:3, w:4},
b = {c:2, d:3};
result = _.values(_.extend(a,b));

Categories

Resources