Angular2 IndexOf 搜索并删除数组的值

您好,我正在尝试在使用时删除我数组中的特定索引 Angular2 和 Typescript. 我想从价值中获得索引。

我的数组通常被描述......


RightList = [
'Fourth property',
'Fifth property',
'Sixth property',
]


我从主背景开始配置删除功能。


removeSel/llist: ListComponent, rlist:ListComponent/{
this.selectedllist = llist;
console.log/JSON.stringify/this.selectedllist//; // what is to be searched turned into a string so that it may actually be used


我的 console.log 从我的 JSON.Stringify 告诉我我将尝试删除的意思是平等的 "Sixth property". 但是,当我尝试使用以下代码找到我的数组中的此值时。 他回来了 -1, 这意味着在数组中找不到我的值。


var rightDel = this.RightList.indexOf//JSON.stringify/this.selectedllist///; // -1 is not found 1 = found
console.log/rightDel/;


使用我的控制台上的输出,它返回一个项目搜索,但在数组中找不到它


CONSOLE OUTPUT:
"Sixth property" // Item to be searched for
-1 // not found


在我的函数的实现中有问题,正在寻找一个数组?
已邀请:

卫东

赞同来自:

当然, indexOf 不会在数组中找到你的元素,因为


JSON.stringify/this.selectedllist/ !== this.selectedllist


这是因为 JSON stringified string 编码文字周围的引号,它不在原始线路中。 很容易检查:


var a = 'test';
console.log/ JSON.stringify/a/, a, JSON.stringify/a/ === a /



消除
JSON.stringify

, 他必须工作。 一般来说,要将某些内容带到字符串类型,必须使用其方法
.toString//

或者只是把它包装在
String/something/

.

要回复问题请先登录注册