Searching value in map of the map of the []string

How to search value from map[string]map[string][]string
I’ve tried to do C++ method but it didn’t work

func FindMapByKey(m map[string][]string, value string) bool {
	for _, val := range m {
		for _, n := range val {
			if strings.EqualFold(value, n) {
				return true
			}
		}
	}
	return false
}

im trying to search values with provided inner key . Also tried to do triple for loop which is probably make sense , but don’t know how to user that function . The use of code from triple for loop

func FindMapByKey(m map[string]map[string][]string, value string) bool {
	for _, val := range m {
		for _, innerVal := range val {
			for _, n := range innerVal {
				if strings.EqualFold(value, n) {
					return true
				}
			}
		}
	}
	return false
}

v := FindMapByKey(list.PaymentCmd["PaymentMethod"]["Online"], "Paypal") but im getting error

Which one?

I tried your first example in the playground and it seems to work. Go Playground - The Go Programming Language

The second seems to work as well: Go Playground - The Go Programming Language

there was my mistake . in declaring v := FindMapByKey(list.PaymentCmd["PaymentMethod"]["Online"], "Paypal") . i tried to declare v := FindMapByKey(list.PaymentCmd, "Paypal") and its work. Now there is another problem stands .This search will iterate thru all same values in map.

mp := map[string]map[string][]string{
		"Book1": {
			"Online": {
				"paypal", "YandexMoney",
			},
			"CreditCard": {
				"Visa", "MasterCard",
			},
		},
		"Book2": {
			"Online": {
				"paypal",
			},
			"CreditCard": {
				"Visa",
			},
		},
	}

The problem is how do i declare a variable in use of code with specified parameter of inner and outer map
v := FindMapByKey(list.PaymentCmd["Book1"]["Online"], "Paypal") will trigger error

cannot use mp[“Book1”][“Online”] (type string) as type map[string]map[string]string in argument to FindMapByKey

Sorry, I have no clue what you are trying to explain here…

But from the code you show below, it seems as if you do not want to look if a given string is somewhere in a nested map, but instead you want to know if it is in a slice.

func Contains(haystack []string, needle string) bool {
  for _, v := range haystack {
    if v == needle {
      return true
    }
  }
  return false
}

Here is better explanation .
My task is : if user chooses product, for example Book1 , and he chooses method of payment (Online). and he choose paypal as provider .
Now i need to check if product chosen by user has that method of payment available for this product(Book1) etc for other products. Each product has its own individual method of payment .
Since i have outer key(Book1) and inner key(Online) arguments provided by user, i need to check if provided Methods of payment(Paypal) exist in my map for this product.
So my problem is how do i declare variable for conditional if statement and provide arguments that i receive from user . What is the better way , make func that takes additional arguments or is there a way to do like in normal map by providing args in mp["Book1"]

If I understand you correctly, you simply use my Contains function and your data:

Contains(mp[book][payment], provider)

Do you mean it like this?

yes

Solved

func contains(mp map[string]map[string][]string, val string, outterKey string, innerKey string) bool {
	if outter, found := mp[outterKey]; found {
		if inner, foundTwo := outter[innerKey]; foundTwo {
			for _, n := range inner {
				if strings.EqualFold(val, n) {
					return true
				}
			}
		}
	}

	return false
}

Thx for help

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.