如何从JSON对象获取对象数组中的值

时间:2014-06-16 11:07:49

标签: javascript jquery

var v = {
    test: {
        test1: {
            test11: '123',
            test12: '234'
        },
        test2: {
            test21: '345',
            test22: '567'
        }
    }
}

如何获得test.test2.test22?这个对象值。试试我自己,如下所示

var v = {
    test: {
        test1: {
            test11: '123',
            test12: '234'
        },
        test2: {
            test21: '345',
            test22: '567'
        }
    }
}
var gv = 'test.test2.test22';
var gv_split = gv.split('.');
console.log(v[gv_split[0]][gv_split[1]][gv_split[2]]);

就像使用某些拆分功能从对象获得值。

如果有任何其他选项......

2 个答案:

答案 0 :(得分:0)

尝试类似的东西

var values = {name: 'misko', gender: 'male'};
     var log = [];
     angular.forEach(values, function(value, key) {
       this.push(key + ': ' + value);
     }, log);
     expect(log).toEqual(['name: misko', 'gender: male']);

答案 1 :(得分:0)

我认为这就是答案

var v = {
    test: {
        test1: {
            test11: '123',
            test12: '234'
        },
        test2: {
            test21: '345',
            test22: '567'
        }
    }
}
var gv = 'test.test2.test22';
var gv_split = gv.split('.');
console.log(v[gv_split[0]][gv_split[1]][gv_split[2]]);

(或)

以下帖子工作正常Underscore.js findWhere nested objects