Javascript 如何使用字符串访问嵌套对象值以便编辑?
如果我有以下字符串或数组:Javascript 如何使用字符串访问嵌套对象值以便编辑?,javascript,Javascript,如果我有以下字符串或数组: var thestring = '[0]["more"][0]["more"][0]["text"]'; var thearray = ['0','more','0','more','0','text']; theobject[0]["more"][0]["more"][0]["text"]; theobject[thestring]; 如何使用该字符串或数组来标识对象的一部分,以便对其进行编辑? var theobject = [ {
var thestring = '[0]["more"][0]["more"][0]["text"]';
var thearray = ['0','more','0','more','0','text'];
theobject[0]["more"][0]["more"][0]["text"];
theobject[thestring];
如何使用该字符串或数组来标识对象的一部分,以便对其进行编辑?
var theobject = [
{
"id":1,
"text":"hello",
"more": [
{
"id":2,
"text":"hello",
"more":[
{
"id":3,
"text":"hello", // I want to edit this
"more": [
{
"id":4,
"text":"hello",
"more":[
]
}
]
}
]
}
]
},
{
"id":5,
"text":"hello"
},
{
"id":6,
"text":"hello"
},
{
"id":7,
"text":"hello"
}
];
基本上我正在尝试访问对象的这一部分:
var thestring = '[0]["more"][0]["more"][0]["text"]';
var thearray = ['0','more','0','more','0','text'];
theobject[0]["more"][0]["more"][0]["text"];
theobject[thestring];
但如果我用字符串执行,它将不起作用:
var thestring = '[0]["more"][0]["more"][0]["text"]';
var thearray = ['0','more','0','more','0','text'];
theobject[0]["more"][0]["more"][0]["text"];
theobject[thestring];
看看lodash u0.get和0.set函数,它们允许您使用路径类型语法访问对象,例如
_.get(object, 'property1.property2.property3', defaultValue);
存在一个等价的集合函数,它们都很有用
从lodash文档中:
var object = { 'a': [{ 'b': { 'c': 3 } }] };
_.get(object, 'a[0].b.c');
_.get(object, ['a', '0', 'b', 'c']);
_.get(object, 'a.b.c', 'default');
提供一个默认对象的能力也很好,它使访问一个深入连接的对象变得非常容易
Set以类似的方式工作:
var object = { 'a': [{ 'b': { 'c': 3 } }] };
_.set(object, 'a[0].b.c', 4);
console.log(object.a[0].b.c);
// => 4
_.set(object, ['x', '0', 'y', 'z'], 5);
console.log(object.x[0].y.z);
// => 5
看看lodash u0.get和0.set函数,它们允许您使用路径类型语法访问对象,例如
_.get(object, 'property1.property2.property3', defaultValue);
存在一个等价的集合函数,它们都很有用
从lodash文档中:
var object = { 'a': [{ 'b': { 'c': 3 } }] };
_.get(object, 'a[0].b.c');
_.get(object, ['a', '0', 'b', 'c']);
_.get(object, 'a.b.c', 'default');
提供一个默认对象的能力也很好,它使访问一个深入连接的对象变得非常容易
Set以类似的方式工作:
var object = { 'a': [{ 'b': { 'c': 3 } }] };
_.set(object, 'a[0].b.c', 4);
console.log(object.a[0].b.c);
// => 4
_.set(object, ['x', '0', 'y', 'z'], 5);
console.log(object.x[0].y.z);
// => 5
如果不需要依赖项,此函数将更新数组中描述的值:
function setObjectValue(o, a, v) {
a = a.slice(); // copy array
p = a.pop(); // get last value for updating
a.forEach(function(p) { o = o[p]; }); // traverse object for each property in a
o[p] = v; // update the final value
}
用法:
setObjectValue(对象,数组,'UPDATED')
如果您需要它而不需要依赖项,此函数将更新数组所描述的值:
function setObjectValue(o, a, v) {
a = a.slice(); // copy array
p = a.pop(); // get last value for updating
a.forEach(function(p) { o = o[p]; }); // traverse object for each property in a
o[p] = v; // update the final value
}
用法:
setObjectValue(对象,数组,'UPDATED')
Nice!!!!!!!!!!!!!!!!!!!!!!!!!!是否可以使用for()循环而不是forEach来执行此操作?(只是为了更好的兼容性)。当然,只需将forEach行替换为for(x=0;xNice-answer,我喜欢这种方法!很好!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!是否可以用for()循环代替forEach?(只是为了更好的兼容性)。当然,只需将forEach行替换为for(x=0;xNice-answer,我喜欢这种方法!