简体   繁体   English

在另一个数组内的数组中搜索对象的最佳/最有效方法是什么?

[英]What is the best/most efficient way to search for an object in an array inside another array?

I am building a simple application that stores places I've visited. 我正在构建一个简单的应用程序,用于存储我访问过的地方。 I have a local express server using a db.json file as my database. 我有一个使用db.json文件作为数据库的本地快递服务器。 I am making 2 requests and experiencing a problem. 我提出2个要求,遇到问题。

What I'm trying to do is iterate over both arrays so that when the app loads, countries I've been too are already preselected. 我想做的是遍历两个数组,以便在加载应用程序时,也已经预先选择了我曾经去过的国家。 this seems like a super expensive call to make and has quite slow performance already 这似乎是一个非常昂贵的调用,并且性能已经很慢

Also it's not actually doing what I want until I trigger a second re-render of the DOM and then it updates. 同样,它直到我触发第二次DOM的重新渲染然后更新时才真正实现我想要的功能。

eg if I pre-select Croatia and France in the database and then load the app, none are selected. 例如,如果我在数据库中预先选择了“克罗地亚和法国”,然后加载该应用程序,则不会选择任何一个。 but if I then select Korea (eg) then in the visited list, suddenly all 3 are visible 但是如果我选择韩国(例如),然后在访问列表中,突然所有3个都可见

what would be a better way to compare the arrays? 比较数组的更好方法是什么? considering the object keys are not necessarily the same 考虑对象键不一定相同

componentDidMount(){
axios.get('https://restcountries.eu/rest/v2/all').then((data) => {
  const updatedCountries = data.data.map((country) => {
    return {...country, visited: false, cities: [], showCities: false}
  })
  axios.get('http://localhost:3007/countries').then((countries) => {
    const visitedCountries = countries.data
    for (var i = 0; i < visitedCountries.length; i++){
      for (var k = 0; k < updatedCountries.length; k++){
        if(visitedCountries[i].name === updatedCountries[k].name){
          updatedCountries[k].visited = true
        }
      }
    }
  })
  this.setState({countries: updatedCountries})
})
  }

Instead of using an array to store updatedCountries, you should instead use an object. 与其使用数组来存储updatedCountries,不如使用一个对象。 That way instead of having each element of updatedCountries compare to every element of visitedCountries, you can do a constant lookup. 这样,您可以进行恒定查找,而不是让updatedCountries的每个元素与visitedCountries的每个元素进行比较。 This will change your lookup speed from (n*n) to (n). 这会将您的查找速度从(n * n)更改为(n)。

The reason why you do not initially see any updates is because you have an async call: 您最初看不到任何更新的原因是因为您有一个异步调用:

axios.get('http://localhost:3007/countries')

inside of a synchronous function. 同步功能内部。 As a result, you are resetting the state while you are making the get request. 结果,您在发出get请求时正在重置状态。 Instead you should chain your api calls like 相反,您应该像这样链接api调用

axios.get('https://restcountries.eu/rest/v2/all').then((data) => {
  // edit data
  return axios.get('http://localhost:3007/countries')
}).then((data) => {
  // run function comparing both data
  this.setState({countries: updatedCountries})
})

You need update state in second request success callback function 您需要在第二个请求成功回调函数中更新状态

componentDidMount(){
axios.get('https://restcountries.eu/rest/v2/all').then((data) => {
  const updatedCountries = data.data.map((country) => {
    return {...country, visited: false, cities: [], showCities: false}
  })
  axios.get('http://localhost:3007/countries').then((countries) => {
    const visitedCountries = countries.data
    for (var i = 0; i < visitedCountries.length; i++){
      for (var k = 0; k < updatedCountries.length; k++){
        if(visitedCountries[i].name === updatedCountries[k].name){
          updatedCountries[k].visited = true
        }
      }
    }

    this.setState({countries: updatedCountries})
  })

})
  }

For efficient way to search 高效的搜索方式

axios.get('http://localhost:3007/countries').then((countries) => {
    let visitedCountriesName = new Set(countries.data.map(country => country.name));
    updatedCountries = updatedCountries.map((country) => {
        if (visitedCountriesName.has(country.name)) country.visited = true
        return country
    });
    this.setState({countries: updatedCountries})
})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 促进将标签属性附加到数组内的每个对象的最佳/最有效方法是什么? - What is the best/most efficient way to facilitate the appending of a label property to each object inside an array, inside an object? 搜索对象值数组的最有效方法 - Most efficient way to search array of object values 搜索对象和数组定位匹配的最有效方法 - Most efficient way to search through an object and array locating match 将对象数组转换为对象的最有效方法是什么 - What is the most efficient way to convert an array of objects to an object 通过属性名称重新创建 object 数组的最有效方法是什么? - what is the most efficient way to recreate object array by property names? 在 react reducer 中处理数组 object 的最有效方法是什么? - What is most efficient way to handle array object in react reducer? 搜索javascript对象或对象数组最有效 - Most efficient to search a javascript object or an array of objects 在javascript中搜索对象数组以查找另一个数组的最佳方法是什么? - What is the best way to search an array of objects to find another array in javascript? 在 Javascript 中反转数组的最有效方法是什么? - What is the most efficient way to reverse an array in Javascript? JavaScript在数组中查找对象的最有效方法 - JavaScript Most Efficient Way to find Object in Array
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM