过滤出在字符串数组中出现的用户。

huangapple go评论52阅读模式
英文:

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: &quot;abc&quot;,
    notebookUsers: [1, 2, 3, 4],
  },
  {
    notebookId: &quot;cde&quot;,
    notebookUsers: [2, &quot;foo&quot;, 4, 3],
  },
  {
    notebookId: &quot;fgh&quot;,
    notebookUsers: [&quot;bla&quot;, 4, 5, &quot;123&quot;],
  },
  {
    notebookId: &quot;qqq&quot;,
    notebookUsers: [33, 16, 12],
  },
  {
    notebookId: &quot;ab&quot;,
    notebookUsers: [&quot;abc&quot;, 23213, 2131, 33],
  },
];

const y = [&quot;abc&quot;, &quot;123&quot;, &quot;bla&quot;, &quot;foo&quot;];

const filteredUsersArray = x.filter((nb) =&gt; {
  const filteredUsers = nb.notebookUsers.filter(
    (user) =&gt; !y.includes(user)
  );
  return (
    nb.notebookId !== &quot;welcome&quot; &amp;&amp;
    nb.notebookId !== &quot;null&quot; &amp;&amp;
    nb.notebookId !== &quot;1234&quot; &amp;&amp;
    filteredUsers.length &gt; 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:

&#39;Before&#39; [
  {
    notebookId: &#39;abc&#39;,
    notebookUsers: [ 1, 2, 3, 4 ]
  },
  {
    notebookId: &#39;cde&#39;,
    notebookUsers: [ 2, &#39;foo&#39;, 4, 3 ]
  },
  {
    notebookId: &#39;fgh&#39;,
    notebookUsers: [ &#39;bla&#39;, 4, 5, &#39;123&#39; ]
  },
  {
    notebookId: &#39;qqq&#39;,
    notebookUsers: [ 33, 16, 12 ]
  },
  {
    notebookId: &#39;ab&#39;,
    notebookUsers: [ &#39;abc&#39;, 23213, 2131, 33 ]
  }
]
&#39;After&#39; [
  {
    notebookId: &#39;abc&#39;,
    notebookUsers: [ 1, 2, 3, 4 ]
  },
  { notebookId: &#39;cde&#39;, notebookUsers: [ 2, 4, 3 ] },
  { notebookId: &#39;fgh&#39;, notebookUsers: [ 4, 5 ] },
  {
    notebookId: &#39;qqq&#39;,
    notebookUsers: [ 33, 16, 12 ]
  },
  {
    notebookId: &#39;ab&#39;,
    notebookUsers: [ 23213, 2131, 33 ]
  }
]

Here is the code to return the same Array but without the forbiddenIDs

const forbiddenIDs = [&quot;abc&quot;, &quot;123&quot;, &quot;bla&quot;, &quot;foo&quot;];
const filteredUsersArray = notebooksArray.map((nb) =&gt; {
const filteredUsers = nb.notebookUsers.filter(
    (user) =&gt; !forbiddenIDs.includes(user)
  );
  return (
    nb.notebookId !== &quot;welcome&quot; &amp;&amp;
    nb.notebookId !== &quot;null&quot; &amp;&amp;
    nb.notebookId !== &quot;1234&quot; &amp;&amp;
    filteredUsers.length &gt; 0
  ) &amp;&amp; { notebookId: nb.notebookId, notebookUsers: filteredUsers};
});
console.log(filteredUsersArray);

.map returns a new Array that will be stored in filteredUsersArray.
With &amp;&amp; { 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 =&gt; id &gt;= 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) =&gt; {
        const filteredUsers = nb.notebookUsers.filter(
          (user) =&gt; !y.includes(user)
        );
        return { ...nb, notebookUsers: filteredUsers };
      }).filter((nb) =&gt; {
        return (
          nb.notebookId !== &quot;welcome&quot; &amp;&amp;
          nb.notebookId !== &quot;null&quot; &amp;&amp;
          nb.notebookId !== &quot;1234&quot; &amp;&amp;
          nb.notebookUsers.length &gt; 0
        );
      });

答案4

得分: 0

我不确切知道你想要什么但你可以尝试这种方式 -

&lt;!-- begin snippet: js hide: false console: true babel: false --&gt;

&lt;!-- language: lang-js --&gt;

    const x = [
        {
          notebookId: &quot;abc&quot;,
          notebookUsers: [1, 2, 3, 4],
        },
        {
          notebookId: &quot;cde&quot;,
          notebookUsers: [2, &quot;foo&quot;, 4, 3],
        },
        {
          notebookId: &quot;fgh&quot;,
          notebookUsers: [&quot;bla&quot;, 4, 5, &quot;123&quot;],
        },
        {
          notebookId: &quot;qqq&quot;,
          notebookUsers: [33, 16, 12],
        },
        {
          notebookId: &quot;ab&quot;,
          notebookUsers: [&quot;abc&quot;, 23213, 2131, 33],
        },
      ];

      const y = [&quot;abc&quot;, &quot;123&quot;, &quot;bla&quot;, &quot;foo&quot;];

      const filteredUsersArray = x.filter((nb) =&gt; {
        return nb.notebookUsers.filter(
          (user) =&gt; !y.includes(user)
        )
      });

      console.log(filteredUsersArray);

&lt;!-- end snippet --&gt;
英文:

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: &quot;abc&quot;,
notebookUsers: [1, 2, 3, 4],
},
{
notebookId: &quot;cde&quot;,
notebookUsers: [2, &quot;foo&quot;, 4, 3],
},
{
notebookId: &quot;fgh&quot;,
notebookUsers: [&quot;bla&quot;, 4, 5, &quot;123&quot;],
},
{
notebookId: &quot;qqq&quot;,
notebookUsers: [33, 16, 12],
},
{
notebookId: &quot;ab&quot;,
notebookUsers: [&quot;abc&quot;, 23213, 2131, 33],
},
];
const y = [&quot;abc&quot;, &quot;123&quot;, &quot;bla&quot;, &quot;foo&quot;];
const filteredUsersArray = x.filter((nb) =&gt; {
return nb.notebookUsers.filter(
(user) =&gt; !y.includes(user)
)
});
console.log(filteredUsersArray);

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年2月19日 18:16:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/75499393.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定