javascript - 我想在数组中的对象中返回一个属性,但它会导致代码中的其他问题
问题描述
所以我正在使用这个应该让汽车改变车道的功能。只要我不归还车牌,它就会变道return vehicleObject_list[i]
。但是当我按照指示归还车牌时,汽车停止了变道return vehicleObject_list[i].Number_Plate
。详情如下:
function moveLanes(target_car)
{
/*
This function should do the following:
- move target_car from one lane to the other.
- do the move in a single step without any extra animation.
- use Lane_Position_a and Lane_Position_b to effect the change.
- finally you should return target_car at the end of the function.
hint: You will need to modify the x property of target_car.
*/
if(checkCarInfront(target_car).x == Lane_Position_a ){
target_car.x = Lane_Position_b;
}
else {
target_car.x = Lane_Position_a;
}
}
function checkCarInfront( carObj )
{
/*
This function should do the following:
- determine if carObj is in the same lane and less than 200px behind any of the cars in vehicleObject_list.
- do this by traversing vehicleObject_list and comparing each car's Distance_Driven property to that of carObj.
- if you find a car that matches these requirements then return the Number_Plate property for the car. Otherwise return false.
*/
for (var i = 0; i < vehicleObject_list.length; i++)
{
if (carObj.x == vehicleObject_list[i].x && ((vehicleObject_list[i].Distance_Driven - carObj.Distance_Driven) < 200) && ((vehicleObject_list[i].Distance_Driven - carObj.Distance_Driven) > 0))
{
return vehicleObject_list[i].Number_Plate;
}
}
return false;
}
var vehicleObject_list = [
{ x: 500, y: 0, Distance_Driven: -200, Car_Type: 'greenCar', Number_Plate: 'MBH0WW', Gas_Amt: 2, exhaust: [ ]} , { x: 500, y: 0, Distance_Driven: 200, Car_Type: 'whiteCar', Number_Plate: 'RLDGCM', Gas_Amt: 2, exhaust: [ ]} , { x: 300, y: 0, Distance_Driven: 600, Car_Type: 'whiteCar', Number_Plate: '9WGXXI', Gas_Amt: 2, exhaust: [ ]} ]
Detective_CarObject =
{
x: roadLeftEdge + roadWidth/4,
y: 550,
Distance_Driven: 0,
Gas_Amt: 3,
EngineShudder_Value: 0,
Car_Type: 'detective',
Number_Plate: '5L3UTH',
exhaust: []
}
解决方案
您可以执行以下操作:
if(checkCarInfront(target_car) && target_car.x == Lane_Position_a ){
target_car.x = Lane_Position_b;
} else {
target_car.x = Lane_Position_a;
}
因为checkCarInfront(target_car)
如果有匹配项,将返回一个车牌字符串(它将作为非空字符串评估为真)。您可以将条件链接到target_car
具有.x
属性的对象。
请务必手动测试数据。(例如,如果您调用当前值vehicleObject_list
和值,这将是错误的(由于)。但是,您可以暂时将'sx 设置为并在列表中添加额外的测试车以检查您的逻辑:)Detective_CarObject
checkCarInfront(Detective_CarObject)
Distance_Driven
Detective_CarObject
500
{ x: 500, y: 0, Distance_Driven: 199, Car_Type: 'rainbowCar', Number_Plate: 'R41NB0W', Gas_Amt: 2, exhaust: [ ]}
作为旁注,我建议遵守 JS 命名约定。这里有几个例子:W3Schools、FreeCodeCamp、Google JavaScript Style Guide。过去我会说选择一个(例如下划线/snake_case(例如target_cat
)或camelCase(例如checkCarInFront
)),但不能同时选择两者并保持一致,但现在似乎大多数人都坚持camelCase。这样做将使以后在团队中工作以及共享/贡献于开源库和项目变得更加容易。
推荐阅读
- drools - 如何确定轮班女性员工的数量
- c# - 通过点击 shift 来保持移动和冲刺
- python - 如何解析分支名称的版本号
- xml - 比较用户的特定自定义属性
- memory - 了解 mips 代码中的数据缓存局部性
- java - Flutter Java android从/app_flutter文件夹中的文件获取数据
- django - Dockerizing Django 应用程序:ModuleNotFoundError:没有名为“X”的模块
- python - 如何使用 conda 环境在任何目录中导入包含 python 代码的特定文件夹?
- reactjs - 对象作为 React 子对象无效(找到:带有键 {} 的对象)。如果您打算渲染一组孩子,请改用数组
- signal-processing - 使用 FFTW 计算波长、幅度和相位