[英]find lowest and highest in array of object
我需要在 object 數組中按此順序查找 4 個值:
最低 x & y
最高 x 和最低 y
最高 x & y
最高 y 最低 x
我的 object 是這樣的:
object: [
{id: 1, x: 160, y: 160},
{id: 2, x: 192, y: 160},
{id: 3, x: 224, y: 160},
{id: 4, x: 224, y: 192},
{id: 5, x: 192, y: 192},
{id: 6, x: 160, y: 192},
{id: 7, x: 160, y: 224},
{id: 8, x: 192, y: 224},
{id: 9, x: 224, y: 224}
],
結果必須是包含 4 個 object 的數組。
謝謝
這不是完整的答案; highest x and lowest y
仍然存在,反之亦然,但它可能會給其他貢獻者一些如何解決問題的見解:
const object = [ {id: 9, x: 224, y: 224}, {id: 2, x: 192, y: 160}, {id: 3, x: 224, y: 160}, {id: 4, x: 224, y: 192}, {id: 5, x: 192, y: 192}, {id: 6, x: 160, y: 192}, {id: 7, x: 160, y: 224}, {id: 8, x: 192, y: 224}, {id: 1, x: 160, y: 160}, {id: 9, x: 224, y: 224} ]; object.sort((objA, objB) => (objA.x * objA.y) - (objB.x * objB.y)); const HighestXAndHighestY = object[object.length-1]; const lowestXAndlowestY = object[0]; console.log(HighestXAndHighestY) console.log(lowestXAndlowestY)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.