在JavaScript中访问嵌套数组/属性 [英] Accessing nested arrays/properties in javascript
问题描述
此答案中的实用程序功能允许轻松访问对象的嵌套属性,如果有一个则返回null(或未定义)的父属性不存在。
原始代码:
get = function(obj,key){
return key.split(。)。reduce(function(o,x){
return(typeof o == undefined || o === null)?o:o [x];
},obj);
}
get(user,'loc.lat')// 50
get(user,'loc.foo.bar')//未定义
我真的很想使用它,但是我还需要能够使用嵌套数组。 / p>
示例:
var childArray = [0,1,2]
var parentArray = [{myArray:childArray}]
var obj = {key:parentArray}
我想像这样扩展实用程序功能:
get(obj, 'key [0] .myArray [2]'); // 2
get(obj,’key [0] .foo [2]’); // null
get(obj,’key [0] .myArray [42]’); // null
理想情况下,它也应该能够对此进行评估
var childArray = [0,1,2]
var parentArray = [childArray,childArray]
var obj = {key: parentArray}
get(obj,'key [1] [0]'); // 0
get(obj,‘foo [1] [0]’); // null
问题:
是否可以使用给定的字符串引用(如 arr [0] $ c)访问数组
arr
$ c>(不使用正则表达式删除括号...)?
您知道一种更优雅的解决方案,可以实现上面示例中显示的结果吗?
最简单的方法是将要传递的路径/密钥更改为 get()
来自
get(obj,'key [0] .myArray [2]');
到
get(obj,'key.0.myArray.2');
var get = function(obj,key){返回key.split( 。)。reduce(function(o,x){return(typeof o == undefined || o === null)?o:o [x];},obj);} var childArray = [0 ,1,2] var parentArray = [{myArray:childArray}] var obj = {key:parentArray} console.log(get(obj,'key.0.myArray.2'));; // 2console.log(get(obj,’key.0.foo.2’)); // nullconsole.log(get(obj,’key.0.myArray.42’)); // nullvar childArray2 = [0,1,2] var parentArray2 = [childArray2,childArray2] var obj2 = {key:parentArray2} console.log(get(obj2,‘key.1.0’)); // 0console.log(get(obj2,‘foo.1.0’)); // null
The utility function from this answer allows to easily access nested properties of objects and returns null (or undefined) if one of the parent properties does not exist.
original Code:
get = function(obj, key) { return key.split(".").reduce(function(o, x) { return (typeof o == "undefined" || o === null) ? o : o[x]; }, obj); } get(user, 'loc.lat') // 50 get(user, 'loc.foo.bar') // undefined
I really want to use this, but i need to be able to work with nested arrays as well.
Examples:
var childArray = [0,1,2]
var parentArray = [{myArray: childArray}]
var obj = {key: parentArray}
I want to extend the utility function like this:
get(obj, 'key[0].myArray[2]'); // 2
get(obj, 'key[0].foo[2]'); // null
get(obj, 'key[0].myArray[42]'); // null
And ideally it should also be able to evaluate this as well
var childArray = [0,1,2]
var parentArray = [childArray, childArray]
var obj = {key: parentArray}
get(obj, 'key[1][0]'); // 0
get(obj, 'foo[1][0]'); // null
Question:
Is it possible to access an array arr
with a given string-reference like "arr[0]"
(without regex to remove the brackets...)?
Do you know a more elegant solution that achieves the result presented in the examples above?
The easiest way would be to change the path/key you're passing to get()
From
get(obj, 'key[0].myArray[2]');
To
get(obj, 'key.0.myArray.2');
var get = function(obj, key) {
return key.split(".").reduce(function(o, x) {
return (typeof o == "undefined" || o === null) ? o : o[x];
}, obj);
}
var childArray = [0,1,2]
var parentArray = [{myArray: childArray}]
var obj = {key: parentArray}
console.log(get(obj, 'key.0.myArray.2')); // 2
console.log(get(obj, 'key.0.foo.2')); // null
console.log(get(obj, 'key.0.myArray.42')); // null
var childArray2 = [0,1,2]
var parentArray2 = [childArray2, childArray2]
var obj2 = {key: parentArray2}
console.log(get(obj2, 'key.1.0')); // 0
console.log(get(obj2, 'foo.1.0')); // null
这篇关于在JavaScript中访问嵌套数组/属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!