From eb97a0b352af062bb617cd388ba94de44369dba5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=99=BD=E8=8C=B6=E6=B8=85=E6=AC=A2?= Date: Mon, 31 Jul 2023 17:26:12 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0in=E5=88=A4=E6=96=AD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- array.go | 30 ++++++++++++++++++++++++++++++ array_test.go | 5 +++++ 2 files changed, 35 insertions(+) diff --git a/array.go b/array.go index 99b0bf5..1b4678b 100644 --- a/array.go +++ b/array.go @@ -239,3 +239,33 @@ func (at *ArrayType) Unique() []interface{} { } return []interface{}{} } + +// In 查询一个值是否在列表里, 在的话, 返回首次出现的索引, 不在返回-1 +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 16:28 2023/7/31 +func (at *ArrayType) In(input interface{}) int { + for idx := 0; idx < len(at.convertResult); idx++ { + if nil == input { + if nil != at.convertResult[idx] { + continue + } + return idx + } + if at.convertResult[idx] == nil { + continue + } + if reflect.TypeOf(at.convertResult[idx]).String() != reflect.TypeOf(input).String() { + // 类型不同 + continue + } + sourceByte, _ := json.Marshal(at.convertResult[idx]) + inputByte, _ := json.Marshal(input) + if string(sourceByte) != string(inputByte) { + continue + } + return idx + } + return -1 +} diff --git a/array_test.go b/array_test.go index 221e7c8..39bde6d 100644 --- a/array_test.go +++ b/array_test.go @@ -24,4 +24,9 @@ func TestArray(t *testing.T) { c := map[string]interface{}{"name": "de"} fmt.Println(reflect.DeepEqual(b, c)) fmt.Println(reflect.DeepEqual(a, b)) + fmt.Println(Array(valInt).In("1")) + fmt.Println(Array(valInt).In(1)) + fmt.Println(Array(valInt).In(2)) + fmt.Println(Array(valInt).In(7)) + fmt.Println(Array(valInt).In(20)) }