jiaqingjiayi-xiaochengxu/甲情_甲意/node_modules/antd-mini/es/_util/set.js

22 lines
728 B
JavaScript
Raw Normal View History

2024-11-15 03:51:28 +00:00
export function set(obj, path, value) {
// If path is not an array, convert it to an array
if (!Array.isArray(path)) {
path = path.split('.').reduce(function (acc, key) {
key.split(/\[([^}]+)\]/g).forEach(function (k) {
if (k !== '')
acc.push(isNaN(k) ? k : Number(k));
});
return acc;
}, []);
}
// Traverse the object according to the path
path.slice(0, -1).reduce(function (acc, key, index) {
if (acc[key] === undefined) {
acc[key] = typeof path[index + 1] === 'number' ? [] : {};
}
return acc[key];
}, obj)[path[path.length - 1]] = value;
return obj;
}
export default set;