Ai
1 Star 0 Fork 0

子安/fx

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
find.js 647 Bytes
一键复制 编辑 原始数据 按行查看 历史
Anton Medvedev 提交于 2018-12-15 15:46 +08:00 . Search on keys, not paths
'use strict'
function* find(v, regex, path = []) {
if (typeof v === 'undefined' || v === null) {
return
}
if (Array.isArray(v)) {
let i = 0
for (let value of v) {
yield* find(value, regex, path.concat(['[' + i++ + ']']))
}
return
}
if (typeof v === 'object' && v.constructor === Object) {
const entries = Object.entries(v)
for (let [key, value] of entries) {
const nextPath = path.concat(['.' + key])
if (regex.test(key)) {
yield nextPath
}
yield* find(value, regex, nextPath)
}
return
}
if (regex.test(v)) {
yield path
}
}
module.exports = find
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
JavaScript
1
https://gitee.com/andrewgithub/fx.git
git@gitee.com:andrewgithub/fx.git
andrewgithub
fx
fx
master

搜索帮助