Dealing with response data? in business logic through Map structured data

in the process of business development, there is a high probability that when docking data with the background, you will encounter this data format

{
  ...
  data: [
    {
      ...
      id: 10000,
      name: "",
      ...
    },
  ],
  ...
}

suppose we have rendered this data as a list, and now there is a requirement to do some business processing on the corresponding id item in data after entering id, in a Input component.
according to the previous habit, that"s it

// Input
handleInput(id) {
  data.forEach(item => {
    if(item.id === id) {
    // item
    }
  })
}

but in this case, it feels like you have to go through the entire data every time, which feels like a waste of performance.
so I was wondering if we could use Map to deal with it, such as

.
// dataMapnew Map()
toMap(data, mainKey = "id") {
  const map = {};
  data.forEach(item => {
    map[mainKey] = item;
  });
  return map;
}

const map = toMap(data);

after this conversion, you can directly use map [id] in the handleInput (id) method or, if you use Map, map.get (id) to get the corresponding item. I don"t know if this is feasible in actual business development.


Yes, but what you write is too troublesome

  • Filter directly use `filter:

    [{id:1, name:'1'},{id:2, name:'2'}].filter(({id})=>id===1)
  • convert directly with map :

[{id:1, name:'1'},{id:2, name:'2'}].map(d=>({[d.id]:d}))

```
  • is written as a common toMap :

    function toMap(data, key){
       return data.map(d=>({[d[key]]:d}))
    }
  • want to get the original list:

    Object.values(toMap(data))

I don't know about js, so I'll answer from c/cPP 's point of view.

map is a good choice. First of all, the complexity has been reduced from n to logn. If your data order of magnitude is more than a thousand, you can do a test to compare the time gap between n and logn (the larger the magnitude, the greater the gap).

Furthermore, this is the code after you use map,

data.forEach(item => {
    map[mainKey] = item;
  });

where forEach , I don't know how js is implemented, at least in map if you want to find a data based on key, you should use an interface similar to find provided by this map. The implementation complexity of this interface is logn.

use forEach , the mechanism it implements may be traversal in the middle order (using left and right child pointer, specific reference clue binary tree), that is to say, its search complexity is n, and the purpose of generally using this method is to output all data in order.

so, whether the interface of js's map container uses find , or forEach , you have to see the documentation, or Google, to take a look at their specific internal implementation mechanism.

-split-

looked at the answer above, if your data can be evaluated by O (1) index, you can also build a hash map, but the cost of memory is estimated by yourself. The hash I'm talking about is not using map,. It's an integer array, with id as the array subscript and data index as the array value.


data.some (item = > {

)
if(item.id === id) {
   //
 return true
}

})


since it is a list, why not pass the index? Just know which item it is, there is no need to cycle it again

// Input
handleInput(i) {
  data[i]
}
MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1b3d28e-2c384.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1b3d28e-2c384.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?