Javascript数组元素消失

时间:2017-04-18 00:18:51

标签: javascript arrays angular typescript javascript-objects

我对我遇到的错误感到困惑。我一直在努力寻找解决方案的方法,我想知道是否有人之前已经看过这样的事情或知道发生了什么。

基本上发生的事情是我有两个用对象填充的数组。我想查看它们并检查第二个数组的所有元素(在compareArrays()函数中比较)是否存在于第一个数组(myArray)中。如果比较数组中有同一个对象的多个实例,则第一个中应该有相同的数字。

我正在使用带有Typescript的Angular。这是我的代码:

/*
 * I get todaysItems a little higher in the constructor from a different service. It looks
 * like this: 
 *       {
 *           day: "Monday",
 *           price: 5.00,
 *           items_name: "1/4 Pounder with Cheese and Chips",
 *           items: [
 *               {name: "1/4 Pounder with Cheese", price: 4.50},
 *               {name: "Small Chips", price: 2.50}
 *           ],
 *           ad_hoc_price: 7
 *       }
 */
private todaysItems: Item[] = [];

compareArrays (myArray: Item[], compare: Item[]) {
    var self = this;
    if (compare.every(function(val) {
        var index = self.arrayObjectIndexOf(myArray, val.name, "name");
        if (index !== -1) {
            myArray.splice(index, 1);
            return true;
        } else {
            return false;
        }
    })) {
        return true;
    } else {
        return false;
    }
}

arrayObjectIndexOf(myArray, searchTerm, property) {
    for(var i = 0, len = myArray.length; i < len; i++) {
        if (myArray[i][property] === searchTerm) {
            return i;
        }
    }
    return -1;
}

/*
 * The cart has a property items_in_cart which is working fine, I can add elements to it
 * and remove them with no issue.
 */

addItem (item: Item, totalPrice: number, cart: Cart) {
    totalPrice += item.price;
    cart.items_in_cart.push(item);
    return {
        newPrice: totalPrice,
        newCart: cart
    }
}

好的,所以addItem()函数运行正常,就像预期的那样。但奇怪的是,当我改变它时它看起来像

addItem (item: Item, totalPrice: number, cart: Cart) {
    totalPrice += item.price;
    cart.items_in_cart.push(item);
    console.log(this.compareArrays(cart.items_in_cart, this.todaysItems));
    return {
        newPrice: totalPrice,
        newCart: cart
    }
}

只需添加调用我的compareArrays()函数的小console.log(),就可以从cart.items_in_cart中获取this.todaysItems的所有元素。这是非常奇怪的,我不知道发生了什么。

有什么想法吗?

1 个答案:

答案 0 :(得分:2)

  

只需添加调用我的compareArrays()函数的小console.log()即可启动所有元素

deferred.promise .then( function(data) { console.log('loadClassTimetable - data', data); // Calculating Percentages angular.forEach(data, function(value, key) { var percentage = parseInt(value.number_active_students, 10) / parseInt(value.number_total_students, 10); console.log("Percentage - ", percentage); if (percentage == 0 || isNaN(percentage)) { data[key].color = '#BBC6CE'; } else if (percentage >= 0.75) { data[key].color = '#11202d'; } else if (percentage >= 0.5) { data[key].color = '#324f67'; } else if (percentage >= 0.25) { data[key].color = '#486175'; } else { // Less thant 0.25 data[key].color = '#8297a9'; } }); // Getting classes and adding events AFTER setting the colours var classes = ClassManager.getAllClasses(); parseEventnAdd(classes, data); }, angular.noop) .catch(function(response) { console.warn("Exception caught in loadClassTimetable", response); }) .finally(function() { $scope.loading = false; }); 函数改变其数组参数,例如

compareArrays

由于compareArrays (myArray: Item[], compare: Item[]) { var self = this; if (compare.every(function(val) { var index = self.arrayObjectIndexOf(myArray, val.name, "name"); if (index !== -1) { myArray.splice(index, 1); // HERE 不是纯函数,因此可以通过调用来更改数组。