let yammerConfig = JSON.parse(this.configService.getConfigSettings("yammerConfig"));
Asked
Active
Viewed 69 times
-2
-
This doesn't look like a valid object or an array.. Please share correct and valid data. – gurvinder372 Jan 05 '18 at 09:52
-
2Also, what have you tried so far? – SpoonMeiser Jan 05 '18 at 09:52
-
i want maximum top and left values – rajiv gandhi Jan 05 '18 at 09:54
-
just for loop, save maxTop and maxLeft, and create a simple if statment if a value is greater than the previous values have been – inubs Jan 05 '18 at 11:48
2 Answers
0
I think you want something like...
var array = [ {left: 482.01, top: 76}, {left: 584.01, top: 177.01}, {left: 786, top: 157.01}, {left: 399, top: 382} ];
var maxLeft = 0, maxTop = 0;
array.forEach(e => {
e.left > maxLeft && (maxLeft = e.left);
e.top > maxTop && (maxTop = e.top);
});
console.log(maxLeft, maxTop);
Cleaner way with ES6:
var array = [ {left: 482.01, top: 76}, {left: 584.01, top: 177.01}, {left: 786, top: 157.01}, {left: 399, top: 382} ];
let { maxLeft, maxTop } = maxLeftAndMaxTop(array);
console.log("maxLeft: ", maxLeft, "maxTop: ", maxTop);
function maxLeftAndMaxTop(arr, maxLeft = 0, maxTop = 0) {
arr.forEach(e => {
e.left > maxLeft && (maxLeft = e.left);
e.top > maxTop && (maxTop = e.top);
});
return { maxLeft, maxTop };
}
console.log(maxLeft, maxTop);

Faly
- 13,291
- 2
- 19
- 37
-1
If you are just looking to return the highest value, you can reduce the array and return the highest value for left
or top
. For example:
array = [{type: "rect", originX: "left", originY: "top", left: 482.01, top: 76},
{type: "rect", originX: "left", originY: "top", left: 584.01, top: 177.01},
{type: "rect", originX: "left", originY: "top", left: 786, top: 157.01},
{type: "path-group", originX: "left", originY: "top", left: 399, top: 382},
{type: "path-group", originX: "left", originY: "top", left: 623.01, top: 378.01},
{type: "circle", originX: "left", originY: "top", left: 103, top: 173}];
// Get left
array.reduce((result, item) => item.left > result ? item.left : result, 0); // => 786
// Get top
array.reduce((result, item) => item.top > result ? item.top : result, 0); // => 382
If you don't want to use ES6 syntax, you would use a normal function:
array.reduce(function(result, item) {
return item.left > result ? item.left : result
}, 0);

Blake Simpson
- 1,078
- 6
- 10