英文:
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]) => {
if (!positions.find(obj => obj.x === x && obj.y === y)) {
positions.push({x, y});
}
};
adder([10, 20]); // was added
console.log(positions);
adder([720, 389.5]); // wasn't added
console.log(positions);
<!-- language: lang-css -->
.as-console-wrapper { max-height: 100% !important; top: 0; }
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论