var object={b:{c:4},d:[{e:5},{e:6}]}
parse(object,"b.c")==4 //true
parse(object,"d[0].c")==5 //true
parse(object,"d.0.c")==5 //true
parse(object,"d[1].c")==6 //true
parse(object,"d.1.c")==6 //true
parse(object,"f")==undefined //true
大神给个优秀的解决方案
完成parse函数的内部代码
5 个解决方案
#1
var object={b:{c:4},d:[{e:5},{e:6}]}
function parse(obj,v) {
try {
var str = ("obj."+v).replace(/\.(\d+)/g,"[$1]");
return eval(str);
} catch(eo){
return undefined;
}
}
console.log(parse(object,"b.c")==4);
console.log(parse(object,"d[0].e")==5);
console.log(parse(object,"d.0.e")==5);
console.log(parse(object,"d[1].e")==6);
console.log(parse(object,"d.1.e")==6);
console.log(parse(object,"f")==undefined);
#2
正则有点陌生
replace(/\.(\d+)/g,"[$1]")
是啥替换成啥
#3
d.0.e 数字属性这样是不合法的,要把 .0 替换成 [0]
#4
楼主这道题很有意思,有些像AST,我借鉴AST一丢丢思想,写了个解析函数:
测试:
function parse(object, sentence){
var chars = sentence.split(''),
nodes = [],
i, len, tmp;
// 解析语句
for(i=0, len=chars.length, tmp=''; i<len; ++i){
switch(chars[i]){
case '.':
if(tmp){
nodes.push(tmp);
tmp = '';
}
break;
case '[':
if(tmp){
nodes.push(tmp);
tmp = '';
}
for(++i; i<len; ++i){ // 寻找“]”
if(chars[i] === ']'){
nodes.push(tmp);
tmp = '';
break;
}else{
tmp += chars[i];
}
}
if(i === len) throw 'syntax error';
break;
default:
tmp += chars[i];
}
}
nodes.push(tmp);
// 获取数据
for(i=0, len=nodes.length, tmp=object; i<len; ++i){
tmp = tmp[ nodes[i] ];
}
return tmp;
}
测试:
var object={b:{c:4},d:[{e:5},{e:6}]};
console.log(parse(object,"b.c")==4);
console.log(parse(object,"d[0].e")==5);
console.log(parse(object,"d.0.e")==5);
console.log(parse(object,"d[1].e")==6);
console.log(parse(object,"d.1.e")==6);
console.log(parse(object,"f")==undefined);
#5
或者不用eval
var object={b:{c:4},d:[{e:5},{e:6}]}
function parse(obj,v) {
var arr = v.match(/[\w\$]+/g);
while(typeof obj=="object" && arr.length>0) {
obj = obj[arr.shift()];
}
return obj;
}
console.log(parse(object,"b.c")==4);
console.log(parse(object,"d[0].e")==5);
console.log(parse(object,"d.0.e")==5);
console.log(parse(object,"d[1].e")==6);
console.log(parse(object,"d.1.e")==6);
console.log(parse(object,"f")==undefined);
#1
var object={b:{c:4},d:[{e:5},{e:6}]}
function parse(obj,v) {
try {
var str = ("obj."+v).replace(/\.(\d+)/g,"[$1]");
return eval(str);
} catch(eo){
return undefined;
}
}
console.log(parse(object,"b.c")==4);
console.log(parse(object,"d[0].e")==5);
console.log(parse(object,"d.0.e")==5);
console.log(parse(object,"d[1].e")==6);
console.log(parse(object,"d.1.e")==6);
console.log(parse(object,"f")==undefined);
#2
var object={b:{c:4},d:[{e:5},{e:6}]}
function parse(obj,v) {
try {
var str = ("obj."+v).replace(/\.(\d+)/g,"[$1]");
return eval(str);
} catch(eo){
return undefined;
}
}
console.log(parse(object,"b.c")==4);
console.log(parse(object,"d[0].e")==5);
console.log(parse(object,"d.0.e")==5);
console.log(parse(object,"d[1].e")==6);
console.log(parse(object,"d.1.e")==6);
console.log(parse(object,"f")==undefined);
正则有点陌生
replace(/\.(\d+)/g,"[$1]")
是啥替换成啥
#3
var object={b:{c:4},d:[{e:5},{e:6}]}
function parse(obj,v) {
try {
var str = ("obj."+v).replace(/\.(\d+)/g,"[$1]");
return eval(str);
} catch(eo){
return undefined;
}
}
console.log(parse(object,"b.c")==4);
console.log(parse(object,"d[0].e")==5);
console.log(parse(object,"d.0.e")==5);
console.log(parse(object,"d[1].e")==6);
console.log(parse(object,"d.1.e")==6);
console.log(parse(object,"f")==undefined);
正则有点陌生
replace(/\.(\d+)/g,"[$1]")
是啥替换成啥
d.0.e 数字属性这样是不合法的,要把 .0 替换成 [0]
#4
楼主这道题很有意思,有些像AST,我借鉴AST一丢丢思想,写了个解析函数:
测试:
function parse(object, sentence){
var chars = sentence.split(''),
nodes = [],
i, len, tmp;
// 解析语句
for(i=0, len=chars.length, tmp=''; i<len; ++i){
switch(chars[i]){
case '.':
if(tmp){
nodes.push(tmp);
tmp = '';
}
break;
case '[':
if(tmp){
nodes.push(tmp);
tmp = '';
}
for(++i; i<len; ++i){ // 寻找“]”
if(chars[i] === ']'){
nodes.push(tmp);
tmp = '';
break;
}else{
tmp += chars[i];
}
}
if(i === len) throw 'syntax error';
break;
default:
tmp += chars[i];
}
}
nodes.push(tmp);
// 获取数据
for(i=0, len=nodes.length, tmp=object; i<len; ++i){
tmp = tmp[ nodes[i] ];
}
return tmp;
}
测试:
var object={b:{c:4},d:[{e:5},{e:6}]};
console.log(parse(object,"b.c")==4);
console.log(parse(object,"d[0].e")==5);
console.log(parse(object,"d.0.e")==5);
console.log(parse(object,"d[1].e")==6);
console.log(parse(object,"d.1.e")==6);
console.log(parse(object,"f")==undefined);
#5
或者不用eval
var object={b:{c:4},d:[{e:5},{e:6}]}
function parse(obj,v) {
var arr = v.match(/[\w\$]+/g);
while(typeof obj=="object" && arr.length>0) {
obj = obj[arr.shift()];
}
return obj;
}
console.log(parse(object,"b.c")==4);
console.log(parse(object,"d[0].e")==5);
console.log(parse(object,"d.0.e")==5);
console.log(parse(object,"d[1].e")==6);
console.log(parse(object,"d.1.e")==6);
console.log(parse(object,"f")==undefined);