【问题标题】:Remove elements from array of objects where ID matches regular expression in React Native从 ID 与 React Native 中的正则表达式匹配的对象数组中删除元素
【发布时间】:2020-09-12 11:55:44
【问题描述】:

我将消息存储在状态中,并且很少有消息具有 GUID 格式的 _id 键,我正在尝试使用拼接将其从状态中删除

this.state.messages : [{
   "_id":"1599906957896",
   "createdAt":"9/12/2020 10:35:57 AM +00:00",
   "text":"Message from Lakshay at 4:05",
   "user":{
      "_id":"6a59e693-a12a-4f7a-9a11-ad195492f57f",
      "avatar":"https://placeimg.com/140/140/any",
      "name":"student1 User"
   }
},
{
   "_id":"1599907249640",
   "createdAt":"9/12/2020 10:40:49 AM +00:00",
   "text":"Sending at 4:10",
   "user":{
      "_id":"6a59e693-a12a-4f7a-9a11-ad195492f57f",
      "avatar":"https://placeimg.com/140/140/any",
      "name":"student1 User"
   }
},
{
   "_id":"f5f535eb-d11b-4c7a-8992-3893e5d432e2",
   "createdAt":2020-09-12T10:40:40.911Z,
   "text":"Sending at 4:10",
   "user":{
      "_id":"6a59e693-a12a-4f7a-9a11-ad195492f57f",
      "avatar":"https://placeimg.com/140/140/any"
   }
}]

我想删除遵循 GUID 模式的消息

var pattern = /^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i;

并将其推回状态。任何帮助表示赞赏!

【问题讨论】:

    标签: javascript arrays react-native


    【解决方案1】:

    正确的方法是使用过滤器方法。如果模式不匹配,映射将返回一个未定义,因此您的数组将有一个未定义而不是原始对象。

    const result = messages.filter(message => !message['_id'].match(pattern));
    

    【讨论】:

      【解决方案2】:

      您可以使用map循环遍历数组并检查是否与__id匹配,如果是,请使用拼接删除对象。

      let messages = [{
         "_id":"1599906957896",
         "createdAt":"9/12/2020 10:35:57 AM +00:00",
         "text":"Message from Lakshay at 4:05",
         "user":{
            "_id":"6a59e693-a12a-4f7a-9a11-ad195492f57f",
            "avatar":"https://placeimg.com/140/140/any",
            "name":"student1 User"
         }
      },
      {
         "_id":"1599907249640",
         "createdAt":"9/12/2020 10:40:49 AM +00:00",
         "text":"Sending at 4:10",
         "user":{
            "_id":"6a59e693-a12a-4f7a-9a11-ad195492f57f",
            "avatar":"https://placeimg.com/140/140/any",
            "name":"student1 User"
         }
      },
      {
         "_id":"f5f535eb-d11b-4c7a-8992-3893e5d432e2",
         "createdAt": '2020-09-12T10:40:40.911Z',
         "text":"Sending at 4:10",
         "user":{
            "_id":"6a59e693-a12a-4f7a-9a11-ad195492f57f",
            "avatar":"https://placeimg.com/140/140/any"
         }
      }]
      
      let pattern = /^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i
      
      
      let result = messages.map((message, index) => !message['_id'].match(pattern) ? message : messages.splice(index, 1))
      
      console.log(result)

      【讨论】:

      • 没有理由在这里使用let,而您可以使用const 是吗?
      • 对于这样的答案,我不理解这个观察结果。它不是使用一个叫做消息的变量,而是一个状态对象
      • 使用constvarlet 根本不会影响答案
      • 这是一个编写清晰和惯用的 JavaScript 的问题。如果您不打算重新分配值,const 是更好的选择:a single identifier should only be used to represent a single concept. medium.com/javascript-scene/…
      • answer 中不这样做的任何特殊原因?
      猜你喜欢
      • 2014-11-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-08
      • 1970-01-01
      • 1970-01-01
      • 2017-10-16
      • 1970-01-01
      相关资源
      最近更新 更多