如何在嵌套的映射中添加数据?

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

How to add data in a nested map?

问题

我有一个嵌套的映射数据,其中一个是列表,我想使用append方法向列表中添加数据,但是返回了一个错误:

invalid argument: baseData["a"].(map[string]interface{})["actions"] (map index expression of type interface{}) is not a slice

以下是你的代码:

baseData := map[string]interface{}{}

dateData := map[string]interface{}{
	"date": map[string]interface{}{
		"updatedAt": "yyyy-mm-dd",
		"isChanged": true,
	},
	"actions": []interface{}{},
}
baseData["a"] = dateData

.....示例
{
	"a": {
		"actions": [],
		"date": {
			"isChanged": true,
			"updatedAt": "yyyy-mm-dd"
		}
	},
	"b": {
		"actions": [],
		"date": {
			"isChanged": true,
			"updatedAt": "yyyy-mm-dd"
		}
	}
}

我的代码

actionData := map[string]interface{}{
	"id":     data.Id,
	"action": data.Action,
}
baseData["a"].(map[string]interface{})["actions"] = append(baseData["a"].(map[string]interface{})["actions"], actionData)

希望这可以帮助到你!

英文:

I have a nested map data that
One of them is a list, and I want to add data to the list using the append method
but it return an error :

invalid argument: baseData["a"].(map[string]interface{})["actions"] (map index expression of type interface{}) is not a slice
baseData := map[string]interface{}{}

dateData := map[string]interface{}{
		"date": map[string]interface{}{
			"updatedAt": "yyyy-mm-dd",
			"isChanged": true,
		},
		"actions": []interface{}{},
	}
baseData["a"] = dateData

.....example
{
"a": {
    "actions": [],
    "date": {
        "isChanged": true,
        "updatedAt": "yyyy-mm-dd"
    }
},
"b": {
    "actions": [],
    "date": {
        "isChanged": true,
        "updatedAt": "yyyy-mm-dd"
    }
}

}

My codes

actionData := map[string]interface{}{
		"id":        data.Id,
		"action":    data.Action,
	}
	baseData["a"].(map[string]interface{})["actions"] = append(baseData["a"].(map[string]interface{})["actions"], actionData)

答案1

得分: 1

做这个:

package main

import "fmt"

func main() {
	dateData := map[string]interface{}{
		"actions": []interface{}{},
	}

	dateData["actions"] = append(dateData["actions"].([]interface{}), "foobar")
	fmt.Printf("%+v\n", dateData)
}

输出:

map[actions:[foobar]]
英文:

Do this:

package main

import "fmt"

func main() {
	dateData := map[string]interface{}{
		"actions": []interface{}{},
	}

	dateData["actions"] = append(dateData["actions"].([]interface{}), "foobar")
	fmt.Printf("%+v\n", dateData)
}

Output:

map[actions:[foobar]]

答案2

得分: 0

baseData := map[string]interface{}{}
dateData := map[string]interface{}{
"date": map[string]interface{}{
"updatedAt": "yyyy-mm-dd",
"isChanged": true,
},
"actions": []interface{}{},
}
baseData["a"] = dateData

actionData := map[string]interface{}{
"id": data.Id,
"action": data.Action,
}
baseData["a"].(map[string]interface{})["actions"] = append(baseData["a"].(map[string]interface{})["actions"].([]interface{}), actionData)

英文:
baseData := map[string]interface{}{}
dateData := map[string]interface{}{
	"date": map[string]interface{}{
		"updatedAt": "yyyy-mm-dd",
		"isChanged": true,
	},
	"actions": []interface{}{},
}
baseData["a"] = dateData

actionData := map[string]interface{}{
	"id":     data.Id,
	"action": data.Action,
}
baseData["a"].(map[string]interface{})["actions"] = append(baseData["a"].(map[string]interface{})["actions"].([]interface{}), actionData)

huangapple
  • 本文由 发表于 2021年8月15日 14:33:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/68789235.html
匿名

发表评论

匿名网友

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

确定