英文:
Filtering out users that appear in an array of strings
问题
const filteredUsersArray = x.filter((nb) => {
const filteredUsers = nb.notebookUsers.filter(
(user) => !y.includes(user.toString())
);
return (
nb.notebookId !== "welcome" &&
nb.notebookId !== "null" &&
nb.notebookId !== "1234" &&
filteredUsers.length > 0
);
});
console.log(filteredUsersArray);
英文:
I am trying to filter out any user whose id appears in an array of strings. I am trying to use a filter() method to do this but strugglign with implementing the logic.
const x = [
{
notebookId: "abc",
notebookUsers: [1, 2, 3, 4],
},
{
notebookId: "cde",
notebookUsers: [2, "foo", 4, 3],
},
{
notebookId: "fgh",
notebookUsers: ["bla", 4, 5, "123"],
},
{
notebookId: "qqq",
notebookUsers: [33, 16, 12],
},
{
notebookId: "ab",
notebookUsers: ["abc", 23213, 2131, 33],
},
];
const y = ["abc", "123", "bla", "foo"];
const filteredUsersArray = x.filter((nb) => {
const filteredUsers = nb.notebookUsers.filter(
(user) => !y.includes(user)
);
return (
nb.notebookId !== "welcome" &&
nb.notebookId !== "null" &&
nb.notebookId !== "1234" &&
filteredNotebookUsers.length > 0
);
});
console.log(filteredUsersByNotebookArray);
Result:
[
{
notebookId: "abc",
notebookUsers: [1, 2, 3, 4, 2, "foo", 4, 3]
},
{
notebookId: "cde",
notebookUsers: [2, "foo", 4, 3]
},
{
notebookId: "fgh",
notebookUsers: ["bla", 4, 5, "123"]
},
{
notebookId: "qqq",
notebookUsers: [33, 16, 12, "abc", 23213, 2131, 33]
},
{
notebookId: "ab",
notebookUsers: ["abc", 23213, 2131, 33]
}
]
This doesn't appear to remove the forbidden ids. Not sure where i am wrong.
答案1
得分: 1
你正在返回对一个名为filteredNotebookUsers
的变量的检查,该变量不存在,请更新返回语句以检查filteredUsers
数组的长度。
英文:
you are returning a check on a variable called filteredNotebookUsers
which does not exist, update the return statement to check the length of the filteredUsers array
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
const x = [
{
notebookId: "abc",
notebookUsers: [1, 2, 3, 4],
},
{
notebookId: "cde",
notebookUsers: [2, "foo", 4, 3],
},
{
notebookId: "fgh",
notebookUsers: ["bla", 4, 5, "123"],
},
{
notebookId: "qqq",
notebookUsers: [33, 16, 12],
},
{
notebookId: "ab",
notebookUsers: ["abc", 23213, 2131, 33],
},
];
const y = ["abc", "123", "bla", "foo"];
const filteredUsersArray = x.filter((nb) => {
const filteredUsers = nb.notebookUsers.filter(
(user) => !y.includes(user)
);
return (
nb.notebookId !== "welcome" &&
nb.notebookId !== "null" &&
nb.notebookId !== "1234" &&
filteredUsers.length > 0
);
});
console.log(filteredUsersArray);
<!-- end snippet -->
答案2
得分: 0
我不确定我是否理解你试图实现的内容。也许是这样的:
'Before' [
{
notebookId: 'abc',
notebookUsers: [ 1, 2, 3, 4 ]
},
{
notebookId: 'cde',
notebookUsers: [ 2, 'foo', 4, 3 ]
},
{
notebookId: 'fgh',
notebookUsers: [ 'bla', 4, 5, '123' ]
},
{
notebookId: 'qqq',
notebookUsers: [ 33, 16, 12 ]
},
{
notebookId: 'ab',
notebookUsers: [ 'abc', 23213, 2131, 33 ]
}
]
'After' [
{
notebookId: 'abc',
notebookUsers: [ 1, 2, 3, 4 ]
},
{ notebookId: 'cde', notebookUsers: [ 2, 4, 3 ] },
{ notebookId: 'fgh', notebookUsers: [ 4, 5 ] },
{
notebookId: 'qqq',
notebookUsers: [ 33, 16, 12 ]
},
{
notebookId: 'ab',
notebookUsers: [ 23213, 2131, 33 ]
}
]
这是返回相同的`Array`但没有`forbiddenIDs`的代码:
```javascript
const forbiddenIDs = ["abc", "123", "bla", "foo"];
const filteredUsersArray = notebooksArray.map((nb) => {
const filteredUsers = nb.notebookUsers.filter(
(user) => !forbiddenIDs.includes(user)
);
return (
nb.notebookId !== "welcome" &&
nb.notebookId !== "null" &&
nb.notebookId !== "1234" &&
filteredUsers.length > 0
) && { notebookId: nb.notebookId, notebookUsers: filteredUsers};
});
console.log(filteredUsersArray);
.map
返回一个新的Array
,将存储在filteredUsersArray
中。通过 && { notebookId: nb.notebookId, notebookUsers: filteredUsers};
我们的意思是,如果前面的条件为真,我们将返回一个对象,就像我们之前有过的,但是用户已经被过滤了。因此,我们添加了与之前相同的notebookId
,而notebookUsers
数组现在将包含过滤后的用户。
英文:
I'm not sure if i understood what you are trying to achieve.
Maybe it is the following:
'Before' [
{
notebookId: 'abc',
notebookUsers: [ 1, 2, 3, 4 ]
},
{
notebookId: 'cde',
notebookUsers: [ 2, 'foo', 4, 3 ]
},
{
notebookId: 'fgh',
notebookUsers: [ 'bla', 4, 5, '123' ]
},
{
notebookId: 'qqq',
notebookUsers: [ 33, 16, 12 ]
},
{
notebookId: 'ab',
notebookUsers: [ 'abc', 23213, 2131, 33 ]
}
]
'After' [
{
notebookId: 'abc',
notebookUsers: [ 1, 2, 3, 4 ]
},
{ notebookId: 'cde', notebookUsers: [ 2, 4, 3 ] },
{ notebookId: 'fgh', notebookUsers: [ 4, 5 ] },
{
notebookId: 'qqq',
notebookUsers: [ 33, 16, 12 ]
},
{
notebookId: 'ab',
notebookUsers: [ 23213, 2131, 33 ]
}
]
Here is the code to return the same Array
but without the forbiddenIDs
const forbiddenIDs = ["abc", "123", "bla", "foo"];
const filteredUsersArray = notebooksArray.map((nb) => {
const filteredUsers = nb.notebookUsers.filter(
(user) => !forbiddenIDs.includes(user)
);
return (
nb.notebookId !== "welcome" &&
nb.notebookId !== "null" &&
nb.notebookId !== "1234" &&
filteredUsers.length > 0
) && { notebookId: nb.notebookId, notebookUsers: filteredUsers};
});
console.log(filteredUsersArray);
.map
returns a new Array that will be stored in filteredUsersArray
.
With && { notebookId: nb.notebookId, notebookUsers: filteredUsers};
we mean that if the previous condition is true we return an object like we had before but with the filtered users. So we add the same notebookId
that we had before, and the notebookUsers
array will now contain the filtered users.
答案3
得分: 0
看起来 filter
不完全做你期望的事情。
考虑这个小例子:
const userIds = [1, 2, 3, 4];
const filteredUserIds = userIds.filter(id => id >= 3);
console.log(userIds); // [1, 2, 3, 4]
console.log(filteredUserIds); // [3, 4]
filter
不会改变它正在过滤的数组
看起来你想要做的是map
笔记本,以便筛选用户,然后筛选映射后的笔记本:
const filteredNotebooks =
x.map((nb) => {
const filteredUsers = nb.notebookUsers.filter(
(user) => !y.includes(user)
);
return { ...nb, notebookUsers: filteredUsers };
}).filter((nb) => {
return (
nb.notebookId !== "welcome" &&
nb.notebookId !== "null" &&
nb.notebookId !== "1234" &&
nb.notebookUsers.length > 0
);
});
英文:
It seems that filter
doesn't do quite what you expect.
Consider this small example:
const userIds = [1,2,3,4];
const filteredUserIds = userIds.filter(id => id >= 3);
console.log(userIds); // [1,2,3,4]
console.log(filteredUserIds); // [3,4]
filter
does not change the array that it is filtering
It looks like what you want to do is to map
the notebooks so that the users are filtered, and then filter the mapped notebooks:
const filteredNotebooks =
x.map((nb) => {
const filteredUsers = nb.notebookUsers.filter(
(user) => !y.includes(user)
);
return { ...nb, notebookUsers: filteredUsers };
}).filter((nb) => {
return (
nb.notebookId !== "welcome" &&
nb.notebookId !== "null" &&
nb.notebookId !== "1234" &&
nb.notebookUsers.length > 0
);
});
答案4
得分: 0
我不确切知道你想要什么。但你可以尝试这种方式 -
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
const x = [
{
notebookId: "abc",
notebookUsers: [1, 2, 3, 4],
},
{
notebookId: "cde",
notebookUsers: [2, "foo", 4, 3],
},
{
notebookId: "fgh",
notebookUsers: ["bla", 4, 5, "123"],
},
{
notebookId: "qqq",
notebookUsers: [33, 16, 12],
},
{
notebookId: "ab",
notebookUsers: ["abc", 23213, 2131, 33],
},
];
const y = ["abc", "123", "bla", "foo"];
const filteredUsersArray = x.filter((nb) => {
return nb.notebookUsers.filter(
(user) => !y.includes(user)
)
});
console.log(filteredUsersArray);
<!-- end snippet -->
英文:
I don't know exactly what you are wanting. But you can try in this way -
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
const x = [
{
notebookId: "abc",
notebookUsers: [1, 2, 3, 4],
},
{
notebookId: "cde",
notebookUsers: [2, "foo", 4, 3],
},
{
notebookId: "fgh",
notebookUsers: ["bla", 4, 5, "123"],
},
{
notebookId: "qqq",
notebookUsers: [33, 16, 12],
},
{
notebookId: "ab",
notebookUsers: ["abc", 23213, 2131, 33],
},
];
const y = ["abc", "123", "bla", "foo"];
const filteredUsersArray = x.filter((nb) => {
return nb.notebookUsers.filter(
(user) => !y.includes(user)
)
});
console.log(filteredUsersArray);
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论