npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

ojbk

v1.1.2

Published

Easily get Object key in deep.

Readme

更新日志

1.1.2

  1. 修复底层结果为0、''、undefined 时的异常结果返回
  2. 修复整串索引位置错误后,返回部分取值结果,修复为返回null

为什么要写 ojbk

当查询一条 APi, 或进行一次数据库查询的时候会出现一些要获取的数据层级很深 且 非固定出现的场景。如

// 请求结果
var data = {
  code: 0,
  data: {
    _id: 1,
    name: '张三',
    org: {
      _id: 2,
      name: 'XXX省XX公司'
    }
  }
}

其中 org 为单位信息,当用户未绑定单位信息时会反馈如下数据:

var data = {
  code: 0,
  data: {
    _id: 1,
    name: '张三'
  }
}

如果前端直接通过data.data.org.name获取单位名称时就会存在隐患。即:org不存在时会报错。通常的处理方式是不断的加前置判断,现在通过ojbk来实现前置判断处理。

如何使用

1. 下载

目前只能通过 npm/Yarn 下载

目前支持 AMD/CMD/CommonJs , cdn 以后再说。

  npm install ojbk --save
  yarn add ojbk # 推荐

2.1 纯函数使用

import ojbk from 'ojbk';

const a = {
  b: {
    c: {
      d: 1
    },
    e: [
      2,
      3,
      5,
      {
        f: {
          g: 1
        }
      }
    ]
  }
}

ojbk(a, 'b.e.3.f.g') // => 1
ojbk(a, 'b.e.3.f') // => { g: 1 }
ojbk(a, 'b.f') // => null

2.2 Object 原型函数扩展

import 'ojbk/polyfill'; // 此时将 ojbk函数扩展至 Object 原型

const a = {
  b: {
    c: {
      d: 1
    },
    e: [
      2,
      3,
      5,
      {
        f: {
          g: 1
        }
      }
    ]
  }
}

a.ojbk('b.e.3.f.g') // => 1
a.ojbk('b.e.3.f') // => { g: 1 }
a.ojbk('b.f') // => null

为什么叫 ojbk ?

ojbk在此处有两重含义

  1. 2017 年网络流行用语,意为 完全Ok可行;
  2. 原本是想命名 objk,意为 object key,写出来发现被中文输入法纠偏为了 ojbk 那就这样吧。