我有一个对象数组,如果用户做出一些选择,我会向其中添加新对象。然后,当我刷新页面时,我会确保保存这些选项,以继续显示它们,除非用户删除它们。
同时,如果用户再次尝试添加相同的选项,背后的代码不会让他们这样做。
问题是,刷新后,如果我再次尝试添加相同的选项,数组不会将现有项目识别为与我尝试添加的项目相同,但它允许我这样做。
实际上,这是一个项目示例以及发生这种情况时我在数组中的内容: 项目:
r = [-this.dx/2.0, -this.dy/2.0]
points.push(new Point(this.x+r[0]*Math.cos(angle)+r[1]*Math.sin(angle), this.y- r[0]*Math.sin(angle)+r[1]*Math.cos(angle)));
r = [this.dx/2.0, -this.dy/2.0]
points.push(new Point(this.x+r[0]*Math.cos(angle)+r[1]*Math.sin(angle), this.y-r[0]*Math.sin(angle)+r[1]*Math.cos(angle)));
r = [this.dx/2.0, this.dy/2.0];
points.push(new Point(this.x+r[0]*Math.cos(angle)+r[1]*Math.sin(angle), this.y-r[0]*Math.sin(angle)+r[1]*Math.cos(angle)));
r = [-this.dx/2.0, this.dy/2.0];
points.push(new Point(this.x+r[0]*Math.cos(angle)+r[1]*Math.sin(angle), this.y-r[0]*Math.sin(angle)+r[1]*Math.cos(angle)));
数组:filtered。
{name: "Vinos", type_products: "Vinos", active: true}
active: true
name: "Vinos"
type_products: "Vinos"
[[Prototype]]: Object
所以,如您所见,这是同一件事,但是,当我进行任何这些验证时,我总是收到错误的值:
this.filtersSelected[0]
{name: "Vinos", type_products: "Vinos", active: true}
active: true
name: "Vinos"
type_products: "Vinos"
[[Prototype]]: Object
如何检查/比较数组中的值与我的项目之间的值,以执行某些操作,仅当该项目不在数组中时?
(喜欢
const index = this.filtersSelected.indexOf(item); //the value is always -1
this.filtersSelected.filter(el => el == item) //the value is always [] (an empty array)
this.filtersSelected.includes(item) //the value is always false
)
非常感谢!
答案 0 :(得分:2)
您必须确定这些对象的哪些属性是使它们唯一的“键”,并迭代数组以查找该键。当没有找到key时,则将其添加到数组中,否则,什么都不做。
例如,如果 :moveFiles
@for /f "eol=? delims=" %%R in ('dir /b "current" 2^>nul') do @(
echo ___
echo Moving %%R to FTP server.
"%java_cmd%" -jar "f17ftp.jar" -dput -file:"%%R" %select%
if errorlevel 1 (
echo Transfer of %%R failed.
setlocal enabledelayedexpansion
set "log="
set "build="
call :readLog
echo !log!
"%java_cmd%" -cp "SQLQueryXML.jar" com.saliencrgt.chums.AlertMessage "Contents of ftp log are:<br/><br>"!log!
endlocal
) else (
echo Transfer of %%R succeeded.
move "current\%%R" "xmlfiles" 1>nul
)
)
@exit /b
:readLog
@for /f "usebackq delims=" %%x in ("f17ftp.debug.log") do @(
if not defined build (set "build=%%x") else set "build=!build!,%%x"
)
@set "log=%build%"
@exit /b
+ name
代表您可以执行的键:
type_products