implement _.set()
2022年5月1日小于 1 分钟
_.set()
implement Question
_.set(object, path, value) is a handy method to updating an object without checking the property existence.
Can you create your own set()
?
const obj = {
a: {
b: {
c: [1,2,3]
}
}
}
set(obj, 'a.b.c', 'BFE')
console.log(obj.a.b.c) // "BFE"
set(obj, 'a.b.c.0', 'BFE')
console.log(obj.a.b.c[0]) // "BFE"
set(obj, 'a.b.c[1]', 'BFE')
console.log(obj.a.b.c[1]) // "BFE"
set(obj, ['a', 'b', 'c', '2'], 'BFE')
console.log(obj.a.b.c[2]) // "BFE"
set(obj, 'a.b.c[3]', 'BFE')
console.log(obj.a.b.c[3]) // "BFE"
set(obj, 'a.c.d[0]', 'BFE')
// valid digits treated as array elements
console.log(obj.a.c.d[0]) // "BFE"
set(obj, 'a.c.d.01', 'BFE')
// invalid digits treated as property string
console.log(obj.a.c.d['01']) // "BFE"
Code
/**
* @param {object} obj
* @param {string | string[]} path
* @param {any} value
*/
function set(obj, path, value) {
// your code here
}