const array = [
[
{ name: 'A', age: 10 },
{ name: 'B', age: 10 },
{ name: 'A', age: 10 },
{ name: 'B', age: 10 },
],
[
{ name: 'C', age: 10 },
{ name: 'D', age: 10 },
{ name: 'C', age: 10 },
{ name: 'D', age: 10 },
],
];
const groupBy = (collection, extractKey) => {
const cache = new Map();
return collection.reduce((accumulator, entry) => {
const key = extractKey(entry);
if (!cache.has(key)) {
const group = [];
cache.set(key, group);
accumulator.push(group);
}
cache.get(key).push(entry);
return accumulator;
}, []);
};
const newArray = array.map(entry => groupBy(entry, item => item.name));
console.log(newArray);
/*
[
[
[ { name: 'A', age: 10 }, { name: 'A', age: 10 } ],
[ { name: 'B', age: 10 }, { name: 'B', age: 10 } ]
],
[
[ { name: 'C', age: 10 }, { name: 'C', age: 10 } ],
[ { name: 'D', age: 10 }, { name: 'D', age: 10 } ]
]
]
*/
const groupBy = (collection, extractKey) => {
const cache = new Map();
for (const entry of collection) {
const key = extractKey(entry);
if (!cache.has(key)) {
cache.set(key, []);
}
cache.get(key).push(entry);
}
return [...cache.values()];
};
func checkStr(a string, b string, c []string) {
re := regexp.MustCompile(regexp.QuoteMeta(a) + `(\d+)` + regexp.QuoteMeta(b))
for _, str := range c {
fmt.Println(re.FindString(str))
}
}
package main
import (
"bytes"
"fmt"
"html/template"
"log"
)
func main() {
out := bytes.NewBuffer([]byte{})
t, err := template.New("foo").Parse(`<!DOCTYPE html>
<html>
<head>
<title></title>
<script type="text/javascript">
var data = JSON.parse( {{.}} );
</script>
</head>
<body>
</body>
</html>`)
if err != nil {
log.Fatal("Cannot parse the template: ", err)
}
err = t.Execute(out, `[{"ID":"1"}]`)
if err != nil {
log.Fatal("Cannot execute the template: ", err)
}
fmt.Println(string(out.Bytes()))
}
package main
import (
"bytes"
"fmt"
"html/template"
"log"
)
type A []struct {
ID string
}
func main() {
out := bytes.NewBuffer([]byte{})
t, err := template.New("foo").Parse(`<!DOCTYPE html>
<html>
<head>
<title></title>
<script type="text/javascript">
var data = {{.}};
</script>
</head>
<body>
</body>
</html>`)
if err != nil {
log.Fatal("Cannot parse the template: ", err)
}
data := A{
{ID: "1"},
}
err = t.Execute(out, data)
if err != nil {
log.Fatal("Cannot execute the template: ", err)
}
fmt.Println(string(out.Bytes()))
}
session.save_handler = redis
--
-- Структура таблицы `migrations`
--
CREATE TABLE IF NOT EXISTS `migrations` (
`migration` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`batch` int(11) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
--
-- Дамп данных таблицы `migrations`
--
INSERT INTO `migrations` (`migration`, `batch`) VALUES
('2015_10_20_154943_create_sessions_table', 1),
('2015_10_21_063052_create_visits_table', 1),
('2015_10_21_165314_create_claims_table', 1),
('2015_10_21_171026_create_notifications_table', 1);