How can I make what I want array - javascript

I'm a student who has just studied javaScript.
I'm trying to solve an algorithm problem, but I can't solve the problem.
just simple Q ... but I couldn't solve the problem.
[ [ 1, 2 ], [ 1, 3 ], [ 2, 1 ], [ 2, 3 ], [ 3, 1 ], [ 3, 2 ] ]
please advice me How should make it what I want array
I need to turn a 2D array into a 1D array.
result i want [ 1, 2 ], [ 1, 3 ], [ 2, 1 ], [ 2, 3 ], [ 3, 1 ], [ 3, 2 ]
I've tried to do many thing.
At first I thought it would be easy , but I couldn't.
I tried many methods, but I didn't get the result.
let a = [ [ 1, 2 ], [ 1, 3 ], [ 2, 1 ], [ 2, 3 ], [ 3, 1 ], [ 3, 2 ] ]
a.toString().split('').join('')
let result = [];
result.push(a) // [ '1,2', '1,3', '2,1', '2,3' ] // string
How can I get the results [ 1, 2 ], [ 1, 3 ], [ 2, 1 ], [ 2, 3 ], [ 3, 1 ], [ 3, 2 ]?

This is the closest result I get :
let a = [ [ 1, 2 ], [ 1, 3 ], [ 2, 1 ], [ 2, 3 ], [ 3, 1 ], [ 3, 2 ] ]
let res = '';
a.forEach(b => {
res += `[${b}],`
});
console.log(res); // [1,2],[1,3],[2,1],[2,3],[3,1],[3,2],
I hope I helped !
Update
To answer OP from the comments below :
let a = [ [ 1, 2 ], [ 1, 3 ], [ 2, 1 ], [ 2, 3 ], [ 3, 1 ], [ 3,2 ] ]
let res = a.map(cell => cell = +cell.join(''))
console.log(res); // [12, 13, 21, 23, 31, 32]
.map() : see the MDN docs here
On the first call cell will be [ 1, 2 ]. Using .join('') on it will result in "12". Having a + sign before the .join('') call will parse the result in number.
I hope I explained the process well :)

I am trying to understand what you mean here, but i believe what you want is this
let a = [ [ 1, 2 ], [ 1, 3 ], [ 2, 1 ], [ 2, 3 ], [ 3, 1 ], [ 3, 2 ] ];
let arr = [];
for (i=0;i<a.length;++i){
var toTXT = a[i][0]+","+a[i][1];
arr[i] = toTXT;
}
console.log(arr) // (6) ['1,2', '1,3', '2,1', '2,3', '3,1', '3,2']

const arr = [ [ 1, 2 ], [ 1, 3 ], [ 2, 1 ], [ 2, 3 ], [ 3, 1 ], [ 3, 2 ] ];
let newArr = [];
for(let a of arr){
for(let b of a){
newArr.push(b);
}
}
console.log( newArr ); // this is 1 dimensional array.
/*
your request :
[ 1, 2 ], [ 1, 3 ], [ 2, 1 ], [ 2, 3 ], [ 3, 1 ], [ 3, 2 ]
that was an invalid sintax. you cant.
the valid one is :
[ [ 1, 2 ], [ 1, 3 ], [ 2, 1 ], [ 2, 3 ], [ 3, 1 ], [ 3, 2 ] ];
*/

Related

how to get sorted index of min values of 2d array

having 2d number array like;
const arr = [
[1, 5, 9],
[2, 7, 8],
[3, 0, 6],
];
what is the simplest way to get sorted array of array indexes where sort critera is values of original 2d array?
result should be:
`[2,1]`, // (value=0)
`[0,0]`, // (value=1)
`[2,0]`, // (value=2)
`[0,1]`, // (value=3)
...
btw, actual values are floats not that it matters.
but complexity does matter as loop runs on each frame.
You could get the indices first and sort them by the value of the matrix.
const
array = [[1, 5, 9], [2, 7, 8], [3, 0, 6]],
result = array
.flatMap((a, i) => a.map((_, j) => [i, j]))
.sort((a, b) => array[a[0]][a[1]] - array[b[0]][b[1]]);
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
Something like this?
const arr = [
[1, 5, 9],
[2, 7, 8],
[3, 0, 6],
];
const map = []
arr.forEach((row, rowIndex) => row.forEach((col, colIndex) => {
map.push({ c: colIndex, r: rowIndex, value: col });
}))
console.log(map)
const sorted = map.sort((a, b) => {
if(a.value === b.value) return 0;
return a.value > b.value ? 1 : -1;
})
console.log(sorted)
console.log(sorted.map(v => [v.r, v.c]))
it's working version, not optimalized
EDIT:
little optimalization with execution time measure:
const arr = [
[1, 5, 9],
[2, 7, 8],
[3, 0, 6],
];
const sortFn = (a, b) => a.value === b.value ? 0 : a.value > b.value ? 1 : -1;
let count = 3;
function process(arr) {
const map = []
arr.forEach((row, rowIndex) => row.forEach((col, colIndex) => {
map.push({ row: rowIndex, col: colIndex, value: col });
}))
return map.sort(sortFn).map(v => [v.row, v.col]);
}
const interval = setInterval(() => {
console.time("process");
console.log(process(arr))
console.timeEnd("process");
if(--count === 0) clearInterval(interval);
}, 100)
Output:
[
[ 2, 1 ], [ 0, 0 ],
[ 1, 0 ], [ 2, 0 ],
[ 0, 1 ], [ 2, 2 ],
[ 1, 1 ], [ 1, 2 ],
[ 0, 2 ]
]
process: 3.822ms
[
[ 2, 1 ], [ 0, 0 ],
[ 1, 0 ], [ 2, 0 ],
[ 0, 1 ], [ 2, 2 ],
[ 1, 1 ], [ 1, 2 ],
[ 0, 2 ]
]
process: 1.661ms
[
[ 2, 1 ], [ 0, 0 ],
[ 1, 0 ], [ 2, 0 ],
[ 0, 1 ], [ 2, 2 ],
[ 1, 1 ], [ 1, 2 ],
[ 0, 2 ]
]
process: 1.665ms
EDIT 2:
(inspired by another answer with flatMap)
const arr = [
[1, 5, 9],
[2, 7, 8],
[3, 0, 6],
];
const sortFn = (a, b) => a.value === b.value ? 0 : a.value > b.value ? 1 : -1;
arr.flatMap((_row, row) => _row.map((value, col) => { return { row, col, value } })).sort(sortFn).map(v => [v.row, v.col]);
let count = 3;
const process = (arr) => arr.flatMap((_row, row) => _row.map((value, col) => { return { row, col, value } })).sort(sortFn).map(v => [v.row, v.col]);
const interval = setInterval(() => {
console.time("process");
console.log(process(arr))
console.timeEnd("process");
if(--count === 0) clearInterval(interval);
}, 100)
Result:
[
[ 2, 1 ], [ 0, 0 ],
[ 1, 0 ], [ 2, 0 ],
[ 0, 1 ], [ 2, 2 ],
[ 1, 1 ], [ 1, 2 ],
[ 0, 2 ]
]
process: 10.979ms
[
[ 2, 1 ], [ 0, 0 ],
[ 1, 0 ], [ 2, 0 ],
[ 0, 1 ], [ 2, 2 ],
[ 1, 1 ], [ 1, 2 ],
[ 0, 2 ]
]
process: 1.346ms
[
[ 2, 1 ], [ 0, 0 ],
[ 1, 0 ], [ 2, 0 ],
[ 0, 1 ], [ 2, 2 ],
[ 1, 1 ], [ 1, 2 ],
[ 0, 2 ]
]
process: 2.043ms

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

Matrix possibilities in JavaScript

Hello
As you can see in my gist below, I have 03 different properties in an object or array (I could use any one of this as input), and I have to discover all the possibilities of combination between them.
In this case, we just have 03 properties, but we can easily have more than 20 props ...
How could I implement this in a smart way? Thinking about a bigger amount of data ...
https://gist.github.com/matheus-rossi/c0261d1d8e138475c049f500fb2616fa
const configPossibilities = [ [ 1 ], [ 1, 2 ], [ 1, 2, 3, 4 ] ]
const configPossibilities2 = {
ID_LINHA: [ 1 ],
ID_IMPLEMENTO: [ 1 , 2 ],
ID_COMPOSICAO: [ 1 , 2 , 3 , 4 ]
}
const desiredResult = [
[1, 1, 1 ],
[1, 1, 2 ],
[1, 1, 3 ],
[1, 1, 4 ],
[1, 2, 1 ],
[1, 2, 2 ],
[1, 2, 3 ],
[1, 1, 4 ],
]
function getPossibilities(obj){
return cartesian(...Object.values(obj))
}
function cartesian() {
var r = [], arg = arguments, max = arg.length-1
function helper(arr, i) {
for (var j=0, l=arg[i].length; j<l; j++) {
var a = arr.slice(0)
a.push(arg[i][j])
if (i==max) {
r.push(a)
}
else {
helper(a, i+1)
}
}
}
helper([], 0)
return r
}
const desiredResult = getPossibilities(configPossibilities2)

how to concatenate two jagged arrays in javascript

I have two arrays
One is:
[ [ 2, 'c' ],
[ 2, 'e' ],
[ 3, 'a' ],
[ 3, 'b' ] ]
and another is :
[ 3, [ 1, 'g' ], [ 2, [ 1, 'd' ], [ 1, 'f' ] ] ]
how can I concatenate both and get output like
[ [ 2, 'c' ],
[ 2, 'e' ],
[ 3, 'a' ],
[ 3, 'b' ],
[ 3, [ 1, 'g' ], [ 2, [ 1, 'd' ], [ 1, 'f' ] ] ]
var arr1 = [ [ 2, 'c' ],
[ 2, 'e' ],
[ 3, 'a' ],
[ 3, 'b' ] ];
var arr2 = [ 3, [ 1, 'g' ], [ 2, [ 1, 'd' ], [ 1, 'f' ] ] ];
arr1.push(arr2);
Have you tried using the spread operator? Please be aware that it's not supported in IE.
const x =
[ [ 2, 'c' ],
[ 2, 'e' ],
[ 3, 'a' ],
[ 3, 'b' ] ];
const y = [ 3, [ 1, 'g' ], [ 2, [ 1, 'd' ], [ 1, 'f' ] ] ];
const z = [...x, y];
console.log(z);
You could wrap the additional array in extra brackets for use with Array#concat.
var a = [[2, 'c'], [2, 'e'], [3, 'a'], [3, 'b']],
b = [3, [1, 'g'], [2, [1, 'd'], [1, 'f']]];
console.log(a.concat([b]));
.as-console-wrapper { max-height: 100% !important; top: 0; }
concat will merge two arrays into one array without changing the original arrays
Code
const x =
[ [ 2, 'c' ],
[ 2, 'e' ],
[ 3, 'a' ],
[ 3, 'b' ] ]
const y = [ 3, [ 1, 'g' ], [ 2, [ 1, 'd' ], [ 1, 'f' ] ] ];
const z = x.concat([y])
Example
const x = [
[2, 'c'],
[2, 'e'],
[3, 'a'],
[3, 'b']
]
const y = [3, [1, 'g'],
[2, [1, 'd'],
[1, 'f']
]
];
const z = x.concat([y])
console.log(z)

Understanding the geometry of a truncated Icosahedron, for rendering

I'm trying to render a truncated icosahedron like above with clickable zones using Three.js.
I found the code for a regular icosahedron
var t = ( 1 + Math.sqrt( 5 ) ) / 2;
var vertices = [
[ -1, t, 0 ], [ 1, t, 0 ], [ -1, -t, 0 ], [ 1, -t, 0 ],
[ 0, -1, t ], [ 0, 1, t ], [ 0, -1, -t ], [ 0, 1, -t ],
[ t, 0, -1 ], [ t, 0, 1 ], [ -t, 0, -1 ], [ -t, 0, 1 ]
];
var faces = [
[ 0, 11, 5 ], [ 0, 5, 1 ], [ 0, 1, 7 ], [ 0, 7, 10 ], [ 0, 10, 11 ],
[ 1, 5, 9 ], [ 5, 11, 4 ], [ 11, 10, 2 ], [ 10, 7, 6 ], [ 7, 1, 8 ],
[ 3, 9, 4 ], [ 3, 4, 2 ], [ 3, 2, 6 ], [ 3, 6, 8 ], [ 3, 8, 9 ],
[ 4, 9, 5 ], [ 2, 4, 11 ], [ 6, 2, 10 ], [ 8, 6, 7 ], [ 9, 8, 1 ]
];
THREE.PolyhedronGeometry.call( this, vertices, faces, radius, detail );
And drew the conclusion that t is φ & vertices consists of all the permutations of:
(0, ±1, ±φ) (±1, ±φ, 0) (±φ, 0, ±1) - From Here
So I modified my vertices as per:
(0, ±1, ±3φ) (±2, ±(1+2φ), ±φ) (±1, ±(2+φ), ±2φ) - From Here
Resulting in:
var vertices = [
[-2, (1+2*t,t], [2,(1+2*t), t ], [-2,-(1+2*t),-t], [2,-(1+2*t),-t ],
[0,-1,3*t], [0,1,3*t], [0,-1,-3*t], [0,1,-3*t],
[1,-(2+t),-2*t ],[1,(2+t),2*t],[-1,-(2+t),-2*t],[-1,(2+t),2*t]
];
Now I understand I have to modify the faces as well. Icosahedron seems to have 20 triangular faces & I can construct any polygon in Three.js with triangles, only.
Does it then follow, that I need the coordinates for 5 pentagons & 12 hexagons in the form of:
5 * 12 + 6 * 20 = 180 triangles
If so, how should I proceed in generating those coordinates? Or even if I am wrong regarding the whole thing.
The JSModeler framework can generate a lot of solids, including truncated icosahedron, so maybe the source can help you.
You can find the code here if you find for GenerateTruncatedIcosahedron:
https://github.com/kovacsv/JSModeler/blob/master/src/extras/solidgenerator.js
The code creates polygons with five and six vertices, but it is easy to replace them with triangles.

Categories

Resources