我们的其他packages:
npm i @itwmw/obj
import Obj from 'index'
Obj.get()
var Obj = require('index')
Obj.get()
let user = {
age: 18,
sex: 0
}
Set an object item to a given value using "dot" notation. If no key is given to the method, the entire object will be replaced.
设置属性,如果Key值为空,则替换整个数组
Object.set(user,"info.name","xieshao")
console.log(user)
{ age: 18, sex: 0, info: { name: 'xieshao' } }
Get an item from an object using "dot" notation.
获取属性
Obj.get(user,"age") // 18
Obj.get(user,"info.name") // xieshao
Add an element to an array using "dot" notation if it doesn't exist.
添加属性,如果指定值不存在,则加入
Obj.add(user,"name","itwmw")
If the object property contains ".":
取带点的属性,如:
let testObj = {
"a.a" : 1,
"b.b":{
"b.c.s" : "1.2.3"
}
}
Obj.get(testObj,"a.?a")
Add a
?
after the.
在
.
后面加一个?