在数组的映射中查找并删除一个项目。

huangapple go评论71阅读模式
英文:

Find an item in array of maps and delete it

问题

我有一个包含映射的数组,我想根据其"key"删除其中的一个元素,如果存在的话。

如何做到呢?我希望它的执行速度不要太慢,保持顺序不重要。

myMaps := []map[string]interface{}{
  map[string]interface{}{"key": "aaa", "key2": 222, "key3": "aafdsafd"},
  map[string]interface{}{"key": "key_to_delete", "key2": 366, "key3": "333aafdsafd"},
  map[string]interface{}{"key": "cccc", "key2": 467, "key3": "jhgfjhg"},
}

for i, x := range myMaps {
  if x["key"] == "key_to_delete" {
    // 如何删除这个元素:a) 作为映射的键 b) 作为数组元素的映射?
    myMaps = append(myMaps[:i], myMaps[i+1:]...)
    break
  }
}

delete(...) 函数:

在遍历数组时,循环体中传递的是它的副本。那么 delete(...) 如何从真正的数组中删除元素呢?

更新:

我需要知道如何删除两种类型的实体,对于我的情况:

  • 数组的元素 - 一个映射
  • 映射的元素 - 具有特定键的元素

不使用第三方库。

英文:

I have an array of maps, from which I want to delete an element if it exists, which is determined by its "key".

How to do it? I want it not to be slow. Preserving the order isn't important.

myMaps = []map[string]interface{} {
  map[string]interface{} {"key": "aaa", "key2": 222, "key3": "aafdsafd"},
  map[string]interface{} {"key": "key_to_delete", "key2": 366, "key3": "333aafdsafd"},
  map[string]interface{} {"key": "cccc", "key2": 467, "key3": "jhgfjhg"}, 
}
for _, x := range myMaps {
  if x["key"] == "key_to_delete" {
    //delete this element as a) key of the map b) the map-element as an element of the array; How?
  }
}

The delete(...) function:

when iterating over an array, a copy of it is what gets passed in the body of a loop. No? How would then delete(...) delete an element from the real array?

update:

I need to know of how to delete 2 types of entities, and for my case:

  • an element of an array - a map
  • an element of a map, with a certain key

Without using a third-party library.

答案1

得分: 0

如果你想从地图中删除键:

for _, x := range myMaps {
    if x["key"] == "key_to_delete" {
        delete(x, "key")
    }
}

如果你想从数组中删除地图,情况就会变得复杂,最好创建一个第二个数组,并在当前地图需要保留时将其插入其中:

myFilteredMaps := make([]map[string]interface{}, 0, len(myMaps))
for _, x := range myMaps {
    if x["key"] != "key_to_delete" {
        myFilteredMaps = append(myFilteredMaps, x)
    }
}
myMaps = myFilteredMaps

这两种方法都很快,只要len(myMaps)不是太大,它们的运行时间与该长度成线性关系。

英文:

If you want to delete the key from the map:

for _, x := range myMaps {
    if x["key"] == "key_to_delete" {
        delete(x, "key")
    }
}

If what you want is to delete the map from the array it gets complicated, you're better off creating a second array and inserting into it if the current map is to be kept:

myFilteredMaps := make([]map[string]interface{}, 0, len(myMaps))
for _, x := range myMaps {
    if x["key"] != "key_to_delete" {
        myFilteredMaps = append(myFilteredMaps, x)
    }
}
myMaps = myFilteredMaps

Both of these are pretty quick so long as len(myMaps) isn't too large, both have linear runtime with respect to that length.

huangapple
  • 本文由 发表于 2021年10月26日 19:56:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/69722669.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定