Stuck sorting and seperating a nested array in JS - javascript

so I'm trying to find any way to separate this array based on ID's.
[ [ 1, 123412341234, 2.44 ], [ 1, 123912341234, 23.44 ], [ 1, 623412341234, 82.44 ], [ 2, 123412341234, 22.44 ], [ 2, 123412381234, 2.44 ], [ 2, 723412341234, 29.44 ], [ 3, 123412341234, 24.44 ], [ 3, 123412377234, 34.44 ], [ 3, 520312341234, 54.44 ], [ 4, 123412341234, 12.44 ], [ 4, 938412341234, 19.44 ], [ 4, 603412341234, 10.44 ] ]
weather in separate array or in the same array but with an additional [] around the data with same ID
so this
[
[[ 1, 123412341234, 2.44 ], [ 1, 123912341234, 23.44 ], [ 1, 623412341234, 82.44 ]],
[[ 2, 123412341234, 22.44 ], [ 2, 123412381234, 2.44 ], [ 2, 723412341234, 29.44 ]],
[[ 3, 123412341234, 24.44 ], [ 3, 123412377234, 34.44 ], [ 3, 520312341234, 54.44 ]],
[[ 4, 123412341234, 12.44 ], [ 4, 938412341234, 19.44 ], [ 4, 603412341234, 10.44 ]]
]
or this
[[ 1, 123412341234, 2.44 ], [ 1, 123912341234, 23.44 ]]
[[ 2, 123412341234, 22.44 ], [ 2, 123412381234, 2.44 ], [ 2, 723412341234, 29.44 ]]
[[ 3, 123412341234, 24.44 ], [ 3, 123412377234, 34.44 ], [ 3, 520312341234, 54.44 ]]
[[ 4, 123412341234, 12.44 ], [ 4, 938412341234, 19.44 ], [ 4, 603412341234, 10.44 ]]
This is what I tried to make by myself but it's not working ( the c[i] is not working)
for(let i = 0; i < this.z.length;i++) {
for (let j = 0; j < this.z.length; j++) {
if(this.z[j][0]==i){
this.c[i].push(this.z[j])
}
}
}

You could group the array by checking the predecessor and build a new group for a changing first value of the inner array. This approach needs a sorted array.
const
data = [[1, 123412341234, 2.44], [1, 123912341234, 23.44], [1, 623412341234, 82.44], [2, 123412341234, 22.44], [2, 123412381234, 2.44], [2, 723412341234, 29.44], [3, 123412341234, 24.44], [3, 123412377234, 34.44], [3, 520312341234, 54.44], [4, 123412341234, 12.44], [4, 938412341234, 19.44], [4, 603412341234, 10.44]],
result = data.reduce((r, row, i, a) => {
if (!i || a[i - 1][0] !== row[0]) r.push([]);
r[r.length - 1].push(row);
return r;
}, []);
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

const array = [
[1, 123412341234, 2.44],
[1, 123912341234, 23.44],
[1, 623412341234, 82.44],
[2, 123412341234, 22.44],
[2, 123412381234, 2.44],
[2, 723412341234, 29.44],
[3, 123412341234, 24.44],
[3, 123412377234, 34.44],
[3, 520312341234, 54.44],
[4, 123412341234, 12.44],
[4, 938412341234, 19.44],
[4, 603412341234, 10.44]
];
let result = [];
for (let id = 0; id < 4; id++) {
result[id] = [];
for (let index = 0; index < array.length; index++) {
if (array[index][0] === id+1) {
result[id].push(array[index]);
}
}
}
console.log(result) ;

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

How can I make what I want array

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 ] ];
*/

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

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)

Concat multi-dimensional array into one-dimensional array

I have an array with objects
const nodes = [ { children: [1, 2, 3] }, { children: [1, 2, 3] } ];
I want a new array [ 1, 2, 3, 1, 2, 3 ].
I have tried
nodes.map(node => node.children);
but it gives me [ [ 1, 2, 3 ], [ 1, 2, 3 ] ].
I have tried
[].concat(nodes.map(node => node.children));
but it doesn't work since it is just concatenating [] with [ [ 1, 2, 3 ], [ 1, 2, 3 ] ] which is just [ [ 1, 2, 3 ], [ 1, 2, 3 ] ].
You could use Array#reduce
const nodes = [ { children: [1, 2, 3] }, { children: [1, 2, 3] } ],
result = nodes.reduce((r, node) => r.concat(node.children), []);
console.log(result);
console.log([... new Set(result)]); // for unique values
.as-console-wrapper { max-height: 100% !important; top: 0; }
You can do this with Array#reduce
const nodes = [ { children: [1, 2, 3] }, { children: [1, 2, 3] } ];
var result = nodes.reduce(function(r, o) {
r = r.concat(o.children);
return r;
}, []);
console.log(result)
Another way to do this using Array#forEach:
const nodes = [ { children: [1, 2, 3] }, { children: [1, 2, 3] } ]
final = []
nodes.forEach(x => final = final.concat(x.children))
console.log(final)
Another shorter way is (a little modification to what OP was trying to do):
const nodes = [ { children: [1, 2, 3] }, { children: [1, 2, 3] } ];
var result = [].concat.apply([], nodes.map(x => x.children))
console.log(result);

Categories

Resources