T
T
tron212021-09-04 20:05:14
JavaScript
tron21, 2021-09-04 20:05:14

How to sort two arrays of objects in React?

Good day! I can't solve one problem. There are two arrays of objects. One is a filtered list of movies from a third party api. The second is a filtered list from the local api. Objects in their composition are slightly different. Objects from the local api were created based on objects from a third-party . It is necessary to sort both arrays so that an object from the local array of objects is added to the final array in queue 1. Objects have the following values ​​by which they can be compared - these are nameRU , nameEN , movieId for local ones, which is identical to id for movies from a third-party api. I can't figure out how to solve this problem. I will be grateful for any advice. Below is the code for this function from React

//обработчик для поиска фильмов
    function handleSearchFilm(data) {

      const allMovies = JSON.parse(localStorage.getItem('movies'));
      const localMovies = JSON.parse(localStorage.getItem('saved-movies'));
      
      //фильтр по локальным фильмам
      const localSearch = localMovies.filter(item => ((item.nameRU != null && 
      item.nameRU.toLowerCase().includes(data.toLowerCase())) || (item.nameEN != null && 
      item.nameEN.toLowerCase().includes(data.toLowerCase()))))
     
      //фильтр по всем фильмам 
      const allSearch =  allMovies.filter(item => ((item.nameRU != null && 
      item.nameRU.toLowerCase().includes(data.toLowerCase())) || (item.nameEN != null && 
      item.nameEN.toLowerCase().includes(data.toLowerCase()))))

setResultSearchFilm('Сюда необходимо добавить итог');
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
W
WbICHA, 2021-09-05
@timofeus91

const all = [{id:0}, {id:1}, {id:2}];
  const local = [{movieId:1}];
  
  const result = all.map((am) => {
    const lm = local.find((m) => m.movieId === am.id)
    return lm ?? am;
  });

can you please elaborate on the return value of return lm ?? am;

https://developer.mozilla.org/en/docs/Web/JavaScript...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question