如何检查 x 和 y 是否在一个对象中?

huangapple go评论85阅读模式
英文:

How to check if x and y are in an object?

问题

我有一个包含一些x和y位置的对象:

var positions = [
0: {x: 720, y: 389.5},
1: {x: 736, y: 373.5},
2: {x: 736, y: 357.5},
3: {x: 720, y: 373.5},
4: {x: 736, y: 389.5},
6: {x: 720, y: 373.5},
7: {x: 752, y: 389.5},
8: {x: 704, y: 357.5},
9: {x: 752, y: 341.5},
10: {x: 720, y: 405.5},
11: {x: 704, y: 373.5},
]

在一个setInterval循环中,我生成新的坐标并希望检查这些坐标是否存在于我的对象中,以便重新生成它们,直到获得不存在的坐标。假设:

var newPosition = [720, 389.5];
// 返回true
var newPosition = [720, 357.5];
// 返回true
英文:

I have an object containing some x and y positions:

var positions = [
0: {x: 720, y: 389.5},
1: {x: 736, y: 373.5},
2: {x: 736, y: 357.5},
3: {x: 720, y: 373.5},
4: {x: 736, y: 389.5},
6: {x: 720, y: 373.5},
7: {x: 752, y: 389.5},
8: {x: 704, y: 357.5},
9: {x: 752, y: 341.5},
10: {x: 720, y: 405.5},
11: {x: 704, y: 373.5},
]

In a setInterval loop, I generate new coordinates and want to check if these coordinates exists in my object in order to regenerate them until getting unexisting coordinates. Let say:

var newPosition = [720, 389.5];
// return true
var newPosition = [720, 357.5];
// return true

答案1

得分: 1

你可以使用 Array#some 来检查属性。

const addTo = array => ([x, y]) => {
    if (positions.some(o => o.x === x && o.y === y)) return;
    array.push({ x, y });
};

var positions = [{ x: 720, y: 389.5 }, { x: 736, y: 373.5 }, { x: 736, y: 357.5 }, { x: 720, y: 373.5 }, { x: 736, y: 389.5 }, { x: 720, y: 373.5 }, { x: 752, y: 389.5 }, { x: 704, y: 357.5 }, { x: 752, y: 341.5 }, { x: 720, y: 405.5 }, { x: 704, y: 373.5 }],
    add = addTo(positions);

add([720, 389.5]);
console.log(positions); // 没有添加

add([720, 357.5]);
console.log(positions); // 已添加
.as-console-wrapper { max-height: 100% !important; top: 0; }
英文:

You could Array#some and check the properties.

<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
const
addTo = array => ([x, y]) => {
if (positions.some(o => o.x === x && o.y === y)) return;
array.push({ x, y });
};

var positions = [{ x: 720, y: 389.5 }, { x: 736, y: 373.5 }, { x: 736, y: 357.5 }, { x: 720, y: 373.5 }, { x: 736, y: 389.5 }, { x: 720, y: 373.5 }, { x: 752, y: 389.5 }, { x: 704, y: 357.5 }, { x: 752, y: 341.5 }, { x: 720, y: 405.5 }, { x: 704, y: 373.5 }],
    add = addTo(positions);

add([720, 389.5]);
console.log(positions); // not added

add([720, 357.5]);
console.log(positions); // added

<!-- language: lang-css -->
.as-console-wrapper { max-height: 100% !important; top: 0; }
<!-- end snippet -->

答案2

得分: 0

最简单的解决方案是:

var positions = [{ x: 720, y: 389.5 }, { x: 736, y: 373.5 }, { x: 736, y: 357.5 }, { x: 720, y: 373.5 }, { x: 736, y: 389.5 }, { x: 720, y: 373.5 }, { x: 752, y: 389.5 }, { x: 704, y: 357.5 }, { x: 752, y: 341.5 }, { x: 720, y: 405.5 }, { x: 704, y: 373.5 }];

const adder = ([x, y]) => {
    if (!positions.find(obj => obj.x === x && obj.y === y)) {
        positions.push({x, y});
    }
};

adder([10, 20]); // 已添加
console.log(positions);
adder([720, 389.5]); // 未被添加
console.log(positions);
.as-console-wrapper { max-height: 100% !important; top: 0; }
英文:

The simplest solution would be:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

var positions = [{ x: 720, y: 389.5 }, { x: 736, y: 373.5 }, { x: 736, y: 357.5 }, { x: 720, y: 373.5 }, { x: 736, y: 389.5 }, { x: 720, y: 373.5 }, { x: 752, y: 389.5 }, { x: 704, y: 357.5 }, { x: 752, y: 341.5 }, { x: 720, y: 405.5 }, { x: 704, y: 373.5 }];


const adder = ([x, y]) =&gt; {
    if (!positions.find(obj =&gt; obj.x === x &amp;&amp; obj.y === y)) {
        positions.push({x, y});
    }
};

adder([10, 20]); // was added
console.log(positions);
adder([720, 389.5]); // wasn&#39;t added
console.log(positions);

<!-- language: lang-css -->

.as-console-wrapper { max-height: 100% !important; top: 0; }

<!-- end snippet -->

huangapple
  • 本文由 发表于 2020年1月3日 20:52:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/59578989.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定