goutil 有将对象数组,指定对象的某个字段转成该字段数组的方法吗?

This issue has been created since 2022-10-27.

因为是从Java转过来的,习惯于stream一些集合操作,在goutil中找到了Find,FindOrDefault,TakeWhile等方法
想问一下有没有取对象数组每一个对象的某个字段,然后转成数组的方法?
和java的 list.stream.map(Product::getXXX) 相似

kuunnss wrote this answer on 2022-10-30

orders := make([]Order, 2)
orders[0] = Order{name: "lisi", age: 18}
orders[1] = Order{name: "wang", age: 24}
输出 string数组 ["listi","wang"]

wenccc wrote this answer on 2022-11-06
func arrayColumn[T any, V any](s []T, callback func(val T) (target V, find bool)) []V {
	res := make([]V, 0)
	for _, v := range s {
		target, find := callback(v)
		if find {
			res = append(res, target)
		}
	}
	return res
}

?

inhere wrote this answer on 2022-11-10

thanks @wenccc !

@kuunnss 已经新增方法:

// Map an object list [object0{},object1{},...] to flatten list [object0.someKey, object1.someKey, ...]
func Map[T any, V any](list []T, mapFn func(obj T) (val V, find bool)) []V {
flatArr := make([]V, 0, len(list))
for _, obj := range list {
if target, find := mapFn(obj); find {
flatArr = append(flatArr, target)
}
}
return flatArr
}
// Column alias of Map func
func Column[T any, V any](list []T, mapFn func(obj T) (val V, find bool)) []V {
return Map(list, mapFn)
}

下个版本发布

inhere wrote this answer on 2022-11-29
More Details About Repo
Owner Name gookit
Repo Name goutil
Full Name gookit/goutil
Language Go
Created Date 2018-07-03
Updated Date 2023-03-22
Star Count 1263
Watcher Count 29
Fork Count 142
Issue Count 3

YOU MAY BE INTERESTED

Issue Title Created Date Comment Count Updated Date
[Result and question] GPU memory seems working well but GPU itself is not working. 2 2022-10-31 2023-03-04
Make revoxelization on GPU faster 4 2022-10-29 2023-03-04
View / open Blocks as a text buffer / separate window 2 2023-01-13 2023-02-23
File to Dataframe support - GCS/S3 to Pandas Dataframe 0 2023-02-07 2023-02-23
Can't use on WSL when connecting to local lotus node and local ipfs daemon "context deadline exceeded" 2 2021-07-24 2023-03-01
official gitlab vlc-rs 1 2021-12-01 2023-03-15
Save json as camelCase 1 2022-07-15 2023-03-16
TypeError: Class extends value undefined is not a constructor or null 1 2023-01-28 2023-02-28
Login with twitter id:pass 0 2021-09-08 2023-02-27
support for PATCH requests in oauth2 1 2016-03-30 2023-01-26
post method throw error 2 2021-12-09 2023-02-16
git clone https://github.com/kinghacker0/WishFish 1 2021-05-22 2023-02-24
Cant change directory 1 2021-03-25 2023-02-09
Temp sensors not on area card 2 2022-05-14 2023-02-10
The playstore is not working for me 1 2022-04-20 2023-02-19
UserError is not defined 0 2021-11-17 2023-02-03
Streamline the redrun package 2 2021-09-19 2023-01-23
Support Node 12 too 5 2021-04-18 2022-01-23
Modal cannot be focus when using ConfigProvider 1 2021-12-23 2023-02-07
Intent to Deprecate and Remove: Expect-CT in Google Chrome 1 2022-10-05 2023-02-15
Fonts Causing Issues in Vercel Deployment 8 2022-08-01 2023-02-15
pdfmake.js:73734 Malformed table row 1 2022-09-26 2023-02-15
Prefetching custom fonts 1 2022-09-14 2023-02-15
Uncaught Unrecognized document structure: {"alignment":"center"} in Table 1 2022-06-15 2023-02-15
pdfMake and two (maybe) specific pdf files -> preview shows blank page when both added to preview 2 2022-08-02 2023-02-15
[Upload]希望支持 onFileNameClick 用来干预文件名点击之后的动作 2 2021-10-28 2023-02-22
Bump Material UI 0 2021-09-26 2022-01-13
Add reports in master graph 0 2021-09-26 2022-09-24
Kube context not found 1 2021-07-17 2023-03-01
Cannot get overloaded constant for type ascription, when qualifing `as` a function 3 2021-09-30 2023-03-08
the user's password cannot be changed 3 2023-01-08 2023-02-14
{TE 7.5] remove fabric fails makes device un-responsive on other fabrics 1 2022-01-27 2023-02-25
Error in installing/building phc-toolkit with PLT_TR_CONTRACTS enabled 1 2021-04-18 2023-02-20
api/v1/log does not have a default/stable content type 1 2022-07-13 2022-12-09
Migrate kube_exec_auth to `client.authentication.k8s.io/v1beta1` api version 2 2021-07-29 2023-03-04
test-instrumentation-celery failing and blocking PRs 0 2022-10-04 2023-03-16
Future development 46 2018-09-04 2022-01-20
Potential version mismatch in "simple" schema 1 2021-04-20 2023-02-25
Cannot connect to a SAP Cloud Platform OData service 8 2020-06-25 2023-03-22
Failed to fetch instance destinations. (Timed out after 10000ms) 25 2021-05-03 2023-02-26
how can i use texture2d variable in glsl,? 1 2022-10-26 2023-03-13
port: flip turn memory scope to be included in the snapshot (#5686) 0 2021-06-17 2023-02-21
--joplin-background-color2 is not updated 5 2021-11-19 2023-02-25
Feature Request: add `Paths` type 1 2021-05-20 2023-03-15
[ECR Public] [request]: Metrics for public traffic to ECR Public repos 2 2021-12-01 2023-02-26
Problem with protobuf codec of Instant 1 2022-02-07 2023-02-20
Use `brace_style` for `async` blocks 1 2021-08-06 2023-02-19
Protected constructor Binder(PropertySet) should have Javadoc link to static method Binder.withPropertySet() 1 2022-02-24 2022-10-30
Improve support of Beats on Kubernetes restricted environments 2 2020-07-02 2023-02-09
Build failed: dotnet-arcade-validation-official/main #20221025.3 0 2022-10-26 2022-10-22