We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
function get(source, path, defaultValue = undefined) { // a[3].b -> a.3.b -> [a,3,b] // path 中也可能是数组的路径,全部转化成 . 运算符并组成数组 const paths = path.replace(/\[(\d+)\]/g, ".$1").split("."); let result = source; for (const p of paths) { // 注意 null 与 undefined 取属性会报错,所以使用 Object 包装一下。 result = Object(result)[p]; if (result == undefined) { return defaultValue; } } return result; } // 测试用例 console.log(get({ a: null }, "a.b.c", 3)); // output: 3 console.log(get({ a: undefined }, "a", 3)); // output: 3 console.log(get({ a: null }, "a", 3)); // output: 3 console.log(get({ a: [{ b: 1 }] }, "a[0].b")); // output: 1
The text was updated successfully, but these errors were encountered:
No branches or pull requests
The text was updated successfully, but these errors were encountered: