Как я могу отфильтровать массив, но с одним исключением? - PullRequest
0 голосов
/ 21 июня 2019

Я пытаюсь отфильтровать массив, чтобы не отправлять ложные значения.

const notFalsyItems = my_team.filter(
  ({ about, email, mobile, last_name, country_code_mobile }) =>
    about && email && mobile && last_name && country_code_mobile,
);

UpdateMyTeamAPICall(notFalsyItems);

Но внутри этого массива есть элемент, который я могу отправить, даже если это null или нет; что photo_id.

Так что мне нужно включить photo_id, даже если оно ложное или нет -> notFalsyItems

Что я могу сделать?

Ответы [ 2 ]

1 голос
/ 21 июня 2019

Если я вас правильно понимаю, я думаю, все, что вам нужно сделать, это изменить ваше состояние, как показано ниже:

const notFalsyItems = my_team.filter(
  ({ about, email, mobile, last_name, country_code_mobile, photo_id }) =>
    {
      //photo_id will always be available whether null or not
      return (about && email && mobile && last_name && country_code_mobile)
    }
);
1 голос
/ 21 июня 2019

У вас может быть более сложная функция для ее фильтрации. Array.prototype.filter () разрешает это.

const my_team = [
  // should pass
  { id: false, about: true, email: true },
  // should not pass
  { id: true, about: false, email: true },
  // should pass
  { id: true, about: true, email: true }
]

// Note that I actually not even changed your code
//(removed some attrs to make it short actually)
//just made it more explicity.
// id will not even be looked at.
const notFalsyItems = my_team.filter(team => {
    const { about, email } = team
    
    // If you return true, item will be added
    // if you return false, item will be skipped
    return about && email
  }
    
);

console.log(notFalsyItems)
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...