React Native Filter数据

时间:2018-04-25 10:15:28

标签: react-native

我正在与N个参与者进行聊天应用,并希望按照名字进行搜索。

 const filteredData = this.state.rawData.filter(id => 
   id.members[0].first_name.toUpperCase().indexOf(event.toUpperCase())  !== -1
   ||
   id.members[1].first_name.toUpperCase().indexOf(event.toUpperCase())  !== -1
 ) 

是不可接受的,因为有n个成员,而不是两个成员。

const filteredData = this.state.rawData.filter(id => 
  id.members.forEach(element => {
    element.first_name.toUpperCase().indexOf(event.toUpperCase())  !== -1
  }
) 

返回我,undefined不是对象'element.first_name.toUpperCase'

为什么?

编辑:

示例数据

rawData: [
        {
          new: 0,
          id: '1',
          members:
            [
              {
                email: 'grantmarshall@test.com',
                first_name: 'Grant',
                last_name: 'Marshall',
                id: "1",
                last_login_time: 10002,
                phone_number: '1234665',
                phone_number_international: '+1',
                picture: '',
                status:'',
                username:'Grant Marshall',
                user:'',
              },
              {
                email: 'you@you.com',
                first_name: 'youfn',
                last_name: 'yousn',
                id: "5ad0007828b1930043a5303d",
                last_login_time: 10002,
                phone_number: '1234665',
                phone_number_international: '+1',
                picture: '',
                status:'',
                username:'youfn youfn',
                user:'',
              },

              { email: 'self@self.com' }],
          messages:
            [
              { chat_id: 1, _id: "1", from: 'Grant Marshall', text: 'roident est duis duis sit occaecat ea eiusmod laboris mollit', time: '9:22', parse_mode: 'none' }
              ,
              { chat_id: 1, _id: "5ad0007828b1930043a5303d", from: 'Dave Pilkinton', text: 'Should not display', time: '9:22', parse_mode: 'none' },

              { chat_id: 1, _id: "1", from: 'Grant Marshall', text: 'my second', time: '9:22', parse_mode: 'none' }
              ,
              { chat_id: 1, _id: "5ad0007828b1930043a5303d", from: 'Dave Pilkinton', text: 'my second Should not display', time: '9:22', parse_mode: 'none' },
            ]
        },

1 个答案:

答案 0 :(得分:1)

这是因为forceCreateUnit在数组的第3次迭代中是homeRoutes.get("/prueba", (req, res) => homeController.forceCreateUnit(req, res));

将您的条件更改为:

element.first_name