【问题标题】:Accessing nested arrays/properties in javascript在 javascript 中访问嵌套数组/属性
【发布时间】:2016-09-14 18:08:27
【问题描述】:

this answer 中的实用函数允许轻松访问对象的嵌套属性,如果父属性之一不存在,则返回 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') // undefined

我真的很想使用它,但我也需要能够使用嵌套数组。

示例:

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,例如"arr[0]"(没有正则表达式来删除括号...)?

您知道实现上述示例中结果的更优雅的解决方案吗?

【问题讨论】:

  • 只需更改“路径”:get(obj, 'key.0.myArray.2') (fiddle)
  • 请将此作为答案,我会赞成并可能接受此作为解决方案。

标签: javascript arrays


【解决方案1】:

最简单的方法是将您传递给get()的路径/键更改为@

来自

get(obj, 'key[0].myArray[2]');

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

【讨论】:

    【解决方案2】:

    通过Object.prototype.getNestedValue() 的发明,您可以通过对象属性和数组索引动态访问深度嵌套的值。您所要做的就是以正确的顺序动态地提供嵌套属性和索引作为参数。

    Object.prototype.getNestedValue = function(...a) {
      return a.length > 1 ? (this[a[0]] !== void 0 && this[a[0]].getNestedValue(...a.slice(1))) : this[a[0]];
    };
    
    var arr = [{fox: [{turn:[857, 432]}]}, {sax: [{pana:[777, 987]}]}, {ton: [{joni:[123, 567]}]}, {piu: [{burn:[666, 37]}]}, {sia: [{foxy:[404, 696]}]}],
      myObj = { foo : 1, bar: { baz : 2 }, bee : 3 },
       arg1 = 3,
       arg2 = "piu",
       arg3 = 0,
       arg4 = "burn",
       arg5 = 1;
    
    document.write(arr.getNestedValue(arg1,arg2,arg3,arg4,arg5));

    【讨论】:

      【解决方案3】:

      我会改为使用属性名称数组,这样可以轻松进行递归。

      但是如果你想回到原来的模式,你仍然可以将你的表达式转换成这个数组。

      看看这个例子:

      var innerget = function(object, proparr) {
        if(proparr.length == 0) 
          return object;
        
        if(typeof object == "undefined" || object === null)
          return object;
        return innerget(object[proparr.splice(0, 1)], proparr);
      };
      
      var get = function(object, expr) {
        var proparr = expr.replace(/\[/g, ".").replace(/\]/g, "").split(".");
        return innerget(object, proparr);
      }
      
      var object = {
         key: [[1, 2, 3], [4, 5, 6]]  
      };
      
      document.write(innerget(object, ['key', 'key2', 2]) + "<br>");
      document.write(innerget(object, ['key', 0, 2]) + "<br>");
      document.write(get(object, "key[0][1]"));

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2020-07-13
        • 2019-08-25
        • 1970-01-01
        • 2019-09-19
        • 2019-09-16
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多