简体   繁体   English

根据动态值Javascript删除数组元素

[英]Remove array elements based on dynamic value Javascript

I have 2 arrays within my Ember application, one array holds a string of ID's and another holds Ember objects. 我的Ember应用程序中有2个数组,一个数组保存一串ID,另一个数组保存Ember对象。

I want to send the Ember objects to a function, get the array of ID's from a cookie stored in the browser and then filter out the Ember objects if the ID property corresponds to a string in my array of IDS. 我想将Ember对象发送给函数,从存储在浏览器中的cookie中获取ID数组,然后如果ID属性对应于我的IDS数组中的字符串,则过滤掉Ember对象。

I am attempting to use filter for this but every time it just brings back the full list of Ember objects, even if there ID is held in the ID string array. 我试图为此使用过滤器,但是每次它只带回Ember对象的完整列表,即使ID字符串数组中保留了ID。 Here is my code from my Ember component.. 这是我的Ember组件中的代码。

init () {
        this._super(...arguments);

this.get('userNotificationServices').fetchActiveUserNotifications().then(result => {
            this.updatedArray = this.filterArray(result);
            this.set('activeNotifications', result);
        });

    },

filterArray(currentActiveNotifications) {
    var ids;
    var currentNonDimissedNotifications;
    if (this.getCookie("dismissed-notifications")) {
        ids = this.getCookie("dismissed-notifications").split(',');
        currentNonDimissedNotifications = currentActiveNotifications.filter(function (obj) {
            for (var id in ids) {
                return obj.id !== id;
            }
        });
    }
    return currentNonDimissedNotifications;
}

There are two problems with your filter : 您的filter有两个问题:

currentNonDimissedNotifications = currentActiveNotifications.filter(function (obj) {
    for (var id in ids) {
        return obj.id !== id;
    }
});
  1. It always returns the result of the first check it does; 它总是返回它所做的第一次检查的结果。 it never does subsequent checks, because you're using return unconditionally. 它永远不会进行后续检查,因为您无条件地使用return

  2. You've used for-in , which means id will be the index of each entry, not its value. 您使用了for-in ,这意味着id将是每个条目的索引 ,而不是它的值。 More about looping arrays, and why for-in isn't the right too most of the time, in my answer here . 有关循环数组的更多信息,以及为什么for-in大多数情况下for-in不太合适,请参见此处

We want to only return when we've either found a match (we can stop right away) or have looped through the whole array of IDs, and we want to loop correctly. 我们只想在找到匹配项(我们可以立即停止)或遍历整个ID数组并且想要正确循环时返回。

So it could be: 因此可能是:

currentNonDimissedNotifications = currentActiveNotifications.filter(function (obj) {
    for (var i = 0; i < ids.length; ++i) { // Note loop
        if (obj.id === ids[i]) {
            // Found it, so we want to filter it out
            return false;
        }
    }
    // Didn't find it, so keep it
    return true;
});

Live Example: 现场示例:

 var currentActiveNotifications = [ {id: 1}, {id: 2}, {id: 3} ]; var ids = [2, 3]; var currentNonDimissedNotifications = currentActiveNotifications.filter(function (obj) { for (var i = 0; i < ids.length; ++i) { // Note loop if (obj.id === ids[i]) { // Found it, so we want to filter it out return false; } } // Didn't find it, so keep it return true; }); console.log(currentNonDimissedNotifications); 

...but arrays have a handy function called indexOf that lets us check if a value is in an array (newer versions of JavaScript also have includes ). ...但是数组有一个方便的函数叫做indexOf ,它使我们可以检查数组中是否有值(JavaScript的新版本也includes )。 So we can use that: 因此我们可以使用:

currentNonDimissedNotifications = currentActiveNotifications.filter(function (obj) {
    return ids.indexOf(obj.id) === -1;
});

Live Example: 现场示例:

 var currentActiveNotifications = [ {id: 1}, {id: 2}, {id: 3} ]; var ids = [2, 3]; var currentNonDimissedNotifications = currentActiveNotifications.filter(function (obj) { return ids.indexOf(obj.id) === -1; }); console.log(currentNonDimissedNotifications); 

Or with the newer includes : 或更新的includes

currentNonDimissedNotifications = currentActiveNotifications.filter(function (obj) {
    return !ids.includes(obj.id);
});

Live Example: 现场示例:

 var currentActiveNotifications = [ {id: 1}, {id: 2}, {id: 3} ]; var ids = [2, 3]; var currentNonDimissedNotifications = currentActiveNotifications.filter(function (obj) { return !ids.includes(obj.id); }); console.log(currentNonDimissedNotifications); 

You should not use for-loop to filter objects, because return obj.id !== id; 您不应该使用for循环来过滤对象,因为return obj.id !== id; will always return true, and it only checks the first id also. 将始终返回true,并且也仅检查第一个ID。

Try to use indexOf: 尝试使用indexOf:

currentNonDimissedNotifications = currentActiveNotifications.filter(function(obj) {
  return ids.indexOf(obj.id) === -1
});

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

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