Reputation:
I have an array like this
var my_array = [
[3,9],
[1,5],
[7,2],
[5,4]
]
As you can see my_array
is a multidimensional array that includes in this case 4 different arrays.
All of the arrays are x/y-coordinates ([3,9]
---> X: 3, Y: 9) and I'd like to move the arrays of the array with ...
... to an object like this:
var my_object = {
"minX": [1,5],
"maxX": [7,2],
"minY": [7,2],
"maxY": [3,9]
}
The function I've created you can find below, but my function is just returning the listed values from above instead of returning the full array. So my current (wrong) result looks like this:
var wrong_object = {
"minX": 1, // missing 5
"maxX": 7, // missing 2
"minY": 2, // missing 7
"maxY": 9 // missing 3
}
var my_array = [
[3, 9],
[1, 5],
[7, 2],
[5, 4]
]
var extreme_result = {}
extreme_result.minX = Math.min.apply(Math, my_array.map(function(i) {
return i[0]
}));
extreme_result.maxX = Math.max.apply(Math, my_array.map(function(i) {
return i[0]
}));
extreme_result.minY = Math.min.apply(Math, my_array.map(function(i) {
return i[1]
}));
extreme_result.maxY = Math.max.apply(Math, my_array.map(function(i) {
return i[1]
}));
console.log(extreme_result)
I hope somebody can help me with this problem.
Thanks in advance, Greetings Pete
Upvotes: 2
Views: 558
Reputation: 2974
I would accomplish this by comparing in a forEach rather than mapping, sorting or whatever.
let my_array = [
[3, 9],
[1, 5],
[7, 2],
[5, 4]
]
let extreme_result = {
minX: my_array[0],
minY: my_array[0],
maxX: my_array[0],
maxY: my_array[0]
};
my_array.forEach( (el) => {
if ( extreme_result.minX[0] > el[0] ) {
extreme_result.minX = el;
} else if ( extreme_result.maxX[0] < el[0] ) {
extreme_result.maxX = el;
}
if ( extreme_result.minY[1] > el[1] ) {
extreme_result.minY = el;
} else if ( extreme_result.maxY[1] < el[1] ) {
extreme_result.maxY = el;
}
});
console.info(extreme_result);
Upvotes: 0
Reputation: 138235
function findExtremes( array ){
let maxX = array[0],
maxY = array[0],
minX = array[0],
minY = array[0];
for(const [x,y] of array){
if(x < minX[0]) minX = [x,y];
if(x > maxX[0]) maxX = [x,y];
if(y > minY[1]) minY = [x,y];
if(y < maxY[1]) maxY = [x,y];
}
return {maxX, maxY, minX, minY};
}
This is O(n) ...
Upvotes: 1
Reputation: 2376
var my_array = [
[3, 9],
[1, 5],
[7, 2],
[5, 4]
];
let extreme_result={};
extreme_result.MinX=my_array.reduce(function(a,b){
if(a[0]<b[0]){
return a;
}
else{
return b;
}
});
extreme_result.MinY=my_array.reduce(function(a,b){
if(a[1]<b[1]){
return a;
}
else{
return b;
}
});
extreme_result.MaxX=my_array.reduce(function(a,b){
if(a[0]>b[0]){
return a;
}
else{
return b;
}
});
extreme_result.MaxY=my_array.reduce(function(a,b){
if(a[1]>b[1]){
return a;
}
else{
return b;
}
});
console.log(extreme_result);
Upvotes: 0
Reputation: 1898
If you don't mind using es6, you can do something like this:
const my_array = [
[3,9],
[1,5],
[7,2],
[5,4],
];
const results = {
xMax: Math.max(...my_array.map(e => e[0])),
xMin: Math.min(...my_array.map(e => e[0])),
yMax: Math.max(...my_array.map(e => e[1])),
yMin: Math.min(...my_array.map(e => e[1]))
}
const points = {
xMax: my_array.filter(e => e[0] === results.xMax),
xMin: my_array.filter(e => e[0] === results.xMin),
yMax: my_array.filter(e => e[1] === results.yMax),
yMin: my_array.filter(e => e[1] === results.yMin)
}
console.log(points);
EDIT:
Sorry, I misread the question. Edit the answer above.
Upvotes: 0
Reputation: 24965
This sorts the array and grabs the first and last elements for the smallest and largest objects.
var my_array = [
[3, 9],
[1, 5],
[7, 2],
[5, 4]
];
my_array.sort(function(a, b){ return a[0] - b[0]; });
var minX = my_array[0];
var maxX = my_array[my_array.length - 1];
my_array.sort(function(a, b){ return a[1] - b[1]; });
var minY = my_array[0];
var maxY = my_array[my_array.length - 1];
console.log(JSON.stringify({
minX: minX,
maxX: maxX,
minY: minY,
maxY: maxY
}));
Upvotes: 1