obj-util

2018-12-08 admin

obj-util是什么

什么是obj-util,A simple helper to set/get keys from objects using a string path like 'some.key.here'

obj-util使用教程帮助文档

obj-util

A simple helper to set/get keys from objects using a string path like ‘some.key.here’

Install

npm i --save obj-util

Usage

obj.getKeyValue

Returns the value from the given object which matches the passed key

  • param obj {Object} The object to get the values from
  • param key {String} a string representing a key in the object. Subkeys are supported separating them with dots. i.e. key1.subkey1.subsubkey1
  • returns {Mixed} the value of the given key in the passed obj
var objUtil = require('obj-util');

var obj = {
  some: {
    key: 'some value'
  }
};

// getKeyValue
objUtil.getKeyValue(obj, 'some.key'); // 'some value'

obj.setKeyValue

Sets a value in an object if a matching key is found inside the given object

  • param obj {Object} the object where to set the value using if the key is found
  • param key {String} a string that represents the key. Subkeys are supported by separating them with dots.
  • param val the value to be set in the object
var objUtil = require('obj-util');

var obj = {
};
objUtil.setKeyValue(obj, 'some.key', 'some value');
obj.some.key === 'some value' //==> true

That’s it!

本站文章除注明转载外,均为本站原创或编译。欢迎任何形式的转载,但请务必注明出处。

转载请注明:文章转载自 JavaScript中文网 [https://www.javascriptcn.com]

本文地址:https://www.javascriptcn.com/read-47574.html

文章标题:obj-util

回到顶部