【问题标题】:What is the best way to find element in an Array?在数组中查找元素的最佳方法是什么?
【发布时间】:2020-03-16 10:28:28
【问题描述】:

我有一个包含患者列表的数组,他们都有唯一的 ID。

 this.state = { 
  patients = [
        {id: "1", name: "Joe", age: 6, location: ''},
        {id: "12", name: "Her", age: 2, location: ''},
        {id: "1123", name: "Jin", age: 10, location: ''},
        {id: "11234", name: "Nick", age: 23, location: ''},
        {id: "12345", name: "Fil", age: 50, location: ''},
    ];

当用户点击按钮时,它会在回调中发送患者唯一 ID 和位置。然后,我使用唯一 ID 在患者数组中查找患者并更新该患者的位置。我在数组中找到患者的方式是使用 map 循环遍历患者数组,检查是否 patientId 匹配数组中的 id 并添加该患者的位置。然而,地图总是会遍历每一个 patients array 中的耐心,所以如果数组变大,不必要的循环也会很昂贵。我知道还有其他方法可以在 数组,即findIndex() method,但它比地图更好吗?这个用例的最佳方法是什么?

 <Button 
    id={location} 
    onClick={() => addPatientLocation(patientId, location}
 > 
    {location}
</Button>

检查患者 ID 是否匹配并更新患者详细信息的功能

  addPatientLocation(patientId, location) {
        this.setState(prevState => ({

            patients: prevState.patients.map(p => {
                if (p.id === patientId) {
                    return { ...p, location: location };
                }
                return p;
            }),
        }));
    }

【问题讨论】:

  • 如果您在设置状态后多次使用find 操作。您可以将数组转换为具有patients = {id: &lt;object&gt;} 的对象。那么每次搜索都将是O(1),否则你将不得不进行线性搜索。
  • 检查此链接以使用 Array.find() developer.mozilla.org/en/docs/Web/JavaScript/Reference/… 并且不要忘记检查我可以用来决定你是否需要 polyfill
  • locaiton !== location

标签: javascript arrays reactjs


【解决方案1】:

我的示例使用了findIndex。我对 findIndex 与 map 函数进行了基准测试,请参见下面的结果。基准测试表明 findIndex 要快得多。

var t1 = performance.now();
const patients = [
        {id: "1", name: "Joe", age: 6, locaiton: ''},
        {id: "12", name: "Her", age: 2, locaiton: ''},
        {id: "1123", name: "Jin", age: 10, locaiton: ''},
        {id: "11234", name: "Nick", age: 23, locaiton: ''},
        {id: "12345", name: "Fil", age: 50, locaiton: ''},
    ];
 
const index = patients.findIndex((elem) => elem.id =="11234");
patients[index].location="Location";
console.log(patients[index]);
var t2 = performance.now();   
console.log("time consumption", (t2 - t1));

var t1 = performance.now();
const patients = [
        {id: "1", name: "Joe", age: 6, locaiton: ''},
        {id: "12", name: "Her", age: 2, locaiton: ''},
        {id: "1123", name: "Jin", age: 10, locaiton: ''},
        {id: "11234", name: "Nick", age: 23, locaiton: ''},
        {id: "12345", name: "Fil", age: 50, locaiton: ''},
    ];
 
const obj = patients.map((elem, index) => {
  if (elem.id =="11234") {
    return { ...elem, locaiton: "Location" };
  }
  return elem;
});
console.log(obj[3]);
var t2 = performance.now();   
console.log("time consumption", (t2 - t1));

【讨论】:

  • 结果不一致耗时14.12999999593012为使用map耗时1.9949999987147748为findIndex
  • @muddassir 如果您多次运行它,您的结果将被缓存。但即使你多次运行它(重新加载页面),findIndex 总是比 map 快!
【解决方案2】:

你可以使用 findIndex

let patients = [...this.state.patients]
let index = patients.findIndex(t=>t.id == patientId)
patients[index] = { ...patients[index], location: value }
this.setState({patients})

【讨论】:

【解决方案3】:

你能用 Map 代替 Array 吗?它将更快地通过 id 获取患者。
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map

【讨论】:

  • @Andy 我以为他担心当数组变大时会遍历整个数组(尽管这是不可能的,因为数组几乎无法达到数百万个元素来影响他的代码的性能)
猜你喜欢
  • 1970-01-01
  • 2010-10-14
  • 1970-01-01
  • 2018-04-14
  • 1970-01-01
  • 2012-03-21
  • 2022-06-15
  • 2014-04-19
  • 1970-01-01
相关资源
最近更新 更多