简体   繁体   English

未在循环中调用接头,在数组中找不到ID

[英]Splice not being called in loop, id not found in array

I'm trying to splice an array by parameter given from an onClick element like deleteFavourite(2) . 我试图通过从诸如deleteFavourite(2)的onClick元素给出的参数拼接数组。 Three console logs show: id (2), fired message, and saved message, but the spliced message is ignored. 三个控制台日志显示:id(2),已触发消息和已保存消息,但已拼接的消息将被忽略。 Why is it not hitting the if? 为什么没有达到if?

Function: 功能:

deleteFavourite: function (id) {
    console.log(id);
    console.log("fired");
    var array = this.favourites;
    for (var i = 0; i < array.length; i++) {
        if (array[i] === id) {
            array.splice(i, 1);
            console.log("spliced");
        }
    }
    this.save();
    console.log("saved");
}

The array is outputted like this in Chrome 在Chrome中像这样输出数组

Array(3)
0: {id: 0, title: "↵Page 0    ", url: "/page-0/"}
1: {id: 1, title: "↵Social media    ", url: "/social-media/"}
2: {id: 2, title: "↵Get involved    ", url: "/get-involved/"}
length: 3
__proto__: Array(0)

Because you're working with an array containing plain objects, so your control should be: 因为您正在使用包含普通对象的数组,所以控件应为:

    deleteFavourite: function (id) {
    console.log(id);
    console.log("fired");
    var array = this.favourites;
    for (var i = 0; i < array.length; i++) {
        if (array[i].id === id) { 
            array.splice(i, 1);
            console.log("spliced");
        }
    }
    this.save();
    console.log("saved");
}

Indeed, you want to compare the id inside the array with the int you passed as a parameter. 确实,您想将数组内的id与您作为参数传递的int进行比较。

Otherwise, you're comparing an object (the array[i] is an object containing 3 attributes) to an int, which will never result as true. 否则,您正在将一个对象(array [i]是一个包含3个属性的对象)与一个int进行比较,这将永远不会得出true。

I guess you don't need to loop your array for that. 我猜你不需要为此循环数组。 To make it work as it is try to not compare in if statement the whole section of array to a number but only iteration of your array with a number. 为了使其正常工作,请尝试不要在if语句中将数组的整个部分与一个数字进行比较,而只能将带有数字的数组进行比较。 In my opinion this should look like this first check if element of array exist then slice it. 在我看来,这应该看起来像是先检查数组元素是否存在,然后对其进行切片。

if (array[id]) {
    array.splice(id, 1);
    console.log("spliced");
}

Try: 尝试:

deleteFavourite: function (id) {
    console.log("deleteFavourite: ", id);
    var array = this.favourites;

    for (var i = 0; i < array.length; i++) {
        if (array[i].id === id) {  // <---- MY CHANGE
            array.splice(i, 1);
            console.log("spliced");
        }
    }
    this.save();
    console.log("saved");
}

You should have used array[i].id since the index of the items in your array does not match the actual id index. 您应该使用array[i].id因为数组中各项的索引与实际的id索引不匹配。

Using ES2015 findIndex : 使用ES2015 findIndex

deleteFavourite: function (id) {
    var array = this.favourites,
        index = array .findIndex(item => item.id == id);

    if( index > -1 ){
        array.splice(index, 1);
        this.save();
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM