Есть ли способ отфильтровать вложенные нулевые значения в api — reactjs

#javascript #reactjs #api

Вопрос:

Это api, с помощью которого я устанавливаю его в состояние после вызова внутри componentDidMount() axios . Если вы посмотрите на api, некоторые profile_path из приведений таковы null . Мне было интересно, как я могу их удалить.

Вот пример codesandbox моего кода, в котором я вызываю api MovieDetails.jsx

Ответ №1:

Вы можете отфильтровать их:

 const mov = {"id":848278,"cast":[{"adult":false,"gender":0,"id":3148142,"known_for_department":"Acting","name":"Courtney Loggins","original_name":"Courtney Loggins","popularity":1.899,"profile_path":null,"cast_id":2,"character":"Parker Zahn","credit_id":"60e49dde07faa200774ff88a","order":0},{"adult":false,"gender":2,"id":1186714,"known_for_department":"Acting","name":"Ruben Pla","original_name":"Ruben Pla","popularity":3.134,"profile_path":"/iYdAE5VrBd2HKMy0mHcZypsuRXG.jpg","cast_id":3,"character":"Torres","credit_id":"60e49de926dac1002362250b","order":1},{"adult":false,"gender":0,"id":1875337,"known_for_department":"Acting","name":"Tarkan Dospil","original_name":"Tarkan Dospil","popularity":1.15,"profile_path":null,"cast_id":6,"character":"Valentine","credit_id":"60e49e44b76cbb00461768f0","order":2},{"adult":false,"gender":0,"id":1299438,"known_for_department":"Acting","name":"Joston Theney","original_name":"Joston Theney","popularity":0.608,"profile_path":"/btnrSBONyzu7YNerSLhr2W4f9iA.jpg","cast_id":5,"character":"Lindon","credit_id":"60e49e09258823004727e8fa","order":3},{"adult":false,"gender":0,"id":2052540,"known_for_department":"Acting","name":"Antuone Torbert","original_name":"Antuone Torbert","popularity":0.766,"profile_path":null,"cast_id":4,"character":"Blackhawk","credit_id":"60e49df76bdec300460a3fd8","order":4}],"crew":[{"adult":false,"gender":0,"id":117168,"known_for_department":"Directing","name":"Hank Braxtan","original_name":"Hank Braxtan","popularity":1.969,"profile_path":null,"credit_id":"60e49d2aa3b5e6005d8f127e","department":"Directing","job":"Director"},{"adult":false,"gender":0,"id":1346332,"known_for_department":"Writing","name":"Jeffrey Giles","original_name":"Jeffrey Giles","popularity":1.313,"profile_path":null,"credit_id":"6123c736c439c00046280d4e","department":"Writing","job":"Writer"},{"adult":false,"gender":0,"id":2223180,"known_for_department":"Writing","name":"Jacoby Bancroft","original_name":"Jacoby Bancroft","popularity":0.608,"profile_path":null,"credit_id":"6123c723bbcae0002ba28ec9","department":"Writing","job":"Writer"}]}
const result = {...mov}
const temp = result.cast.filter(d => d.profile_path !== null)
result.cast = temp
console.log(result)