GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — GitHub - dev-parvej/js_array_method: Map filter reduce in golang

BranchesTags
Open more actions menu

Latest commit

 

History

21 Commits
21 Commits

Folders and files

NameName
Last commit message
Last commit date
CONTRIBUTING.md
CONTRIBUTING.md
 
 
LICENSE
LICENSE
 
 
README.md
README.md
 
 
every_test.go
every_test.go
 
 
filter_test.go
filter_test.go
 
 
find_index_test.go
find_index_test.go
 
 
find_test.go
find_test.go
 
 
forech_test.go
forech_test.go
 
 
go.mod
go.mod
 
 
include_test.go
include_test.go
 
 
js_array_method.go
js_array_method.go
 
 
map_test.go
map_test.go
 
 
reduce_test.go
reduce_test.go
 
 
reverse_test.go
reverse_test.go
 
 
some_test.go
some_test.go
 
 
util.go
util.go
 
 

Repository files navigation

go-js-array-methods

This package is to implement javacript array methods which is missing in golang

Installation

go get github.com/dev-parvej/js_array_method

Usage

import js "github.com/dev-parvej/js_array_method"

type User struct {
	Id   int
	Name string
}

type Foo struct {
	Bar string
}

users := []User{{Id: 10, Name: "Go"}, {Id: 11, Name: "Lang"}}

Find

user, err := js.Find(users, func(user User, index int) bool {
    return user.Id == 10
})
// {Id: 10, Name: "Go"}

Filter

filteredUsers := js.Filter(users, func(user User, index int) bool {
    return user.Id == 10
})
// {{Id: 10, Name: "Go"}}

Map

js.Map(users, func(user User, index int) Foo {
    return Foo{
        Bar: fmt.Sprintf("%d %s", user.Id, user.Name),
    }
})
// {{Bar: "10 Go"}, {Bar: "11 Lang"}}

Reduce

numbers := []int{10, 11, 12, 13}
js.Reduce(numbers, func(s int, n int, i int) int {
    return s + n
}, 0)

// 46

Every

users := []User{{Id: 12, Name: "Go"}, {Id: 14, Name: "Go"}}
resultTrue := Every(users, func(user User, _ int) bool {
    return user.Name == "Go"
})
// true

Every([]string{"hey", "hi"}, "hi")
// false

Foreach

var slices = []string{"Go", "Typescript", "Nodejs"}

Foreach(slices, func(ln string, index int) {
    // ln contains the item of the slice
    // index contains item index
})

Includes

languages := []User{{Id: 12, Name: "Go"}, {Id: 14, Name: "Lang"}, {Id: 15, Name: "Typescript"}}

Includes(languages, func(ln User, _ int) bool { return ln.Name == "Go" })
//true
Includes(languages, func(ln User, _ int) bool { return ln.Name == "MySql" })
//false

languages := []string{"go", "PHP", "MySql", "TypeScript"}
Includes(languages, "TypeScript").
//true

Includes

users := []User{{Id: 10, Name: "John"}, {Id: 11, Name: "Doe"}, {Id: 12, Name: "Sabrina"}}
Reverse(users)
// {{Id: 12, Name: "Sabrina"}, {Id: 11, Name: "Doe"}, {Id: 10, Name: "John"}}

FindIndex

users := []User{{Id: 10, Name: "John"}, {Id: 11, Name: "Doe"}, {Id: 12, Name: "Sabrina"}}
FindIndex(users, func(user User, _ int) bool {
    return user.Name == "Doe"
})
// 2

users := []string{"User1", "User2", "User3", "User4"}

FindIndex(users, "User1")
//0
FindIndex(users, "User5")
//-1

If you find it usefull make sure you star the repo

Buy me a coffe

About

Map filter reduce in golang

Topics

everyfilterfindfindindexgolanghelper-functionshelpersmapreduceslicesome

Resources

Readme
MIT license

Contributing

Contributing
Activity

Stars

6 stars

Watchers

1 watching

Forks

0 forks
Report repository

Releases

Packages

Used by

Contributors

Languages

You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.