简体   繁体   English

使用数组作为嵌套数组的键?

[英]Use array as keys for nested array?

I have a nested array like shown below.我有一个如下所示的嵌套数组。

var arr1 = ['a', 'b', ['c', 'd'], ['e', 'f']];

Is there a way to use another, un-nested array as the key(s) for arr1 ?有没有办法使用另一个非嵌套数组作为arr1的键?

var arr1 = ['a', 'b', ['c', 'd'], ['e', 'f']];
var arr2 = [3, 0];
var arr3 = [4, 1];

console.log(arr1[arr2]); // should return 'c' like arr1[3][0]
console.log(arr1[arr3]); // instead of arr1[4][1]; should return 'f'

Is there a function that allows me to do this?是否有 function 允许我这样做?

You can make your own function that implements this behavior like so:您可以制作自己的 function 来实现此行为,如下所示:

 function nested_access(arr1, arr2) { let ret = arr1; arr2.forEach((index) => { ret = ret[index]; }); return ret; } const arr1 = ['a', 'b', ['c', 'd'], ['e', 'f']]; const arr2 = [2, 0]; const arr3 = [3, 1]; console.log(nested_access(arr1, arr2)); // should return 'c' console.log(nested_access(arr1, arr3)); // should return 'f'

Just a simple method with reduce to walk the tree.只是一种使用 reduce 来遍历树的简单方法。 (I changed your example indexes since they were off) (我更改了您的示例索引,因为它们已关闭)

 const lookUp = (arr, indexes) => indexes.reduce( (acc, index) => acc[index], arr); var arr1 = ['a', 'b', ['c', 'd'], ['e', 'f'] ]; var arr2 = [2, 0]; var arr3 = [3, 1]; console.log(lookUp(arr1, arr2)); console.log(lookUp(arr1, arr3));

You can find the value by iteratively accessing properties using an array of property accessors :您可以通过使用属性访问器数组迭代访问属性来找到该值:

 function findValue (obj, propertyAccessors) { try { let result = obj; for (const key of propertyAccessors) result = result[key]; return result; } catch { return undefined; } } const arr1 = ['a', 'b', ['c', 'd'], ['e', 'f']]; const arr2 = [2, 0]; const arr3 = [3, 1]; console.log(findValue(arr1, arr2)); // "c" console.log(findValue(arr1, arr3)); // "f" console.log(findValue(arr1, [2, 2, 3])); // undefined

If you attempt to access a property which doesn't exist, the result will be undefined .如果您尝试访问不存在的属性,结果将是undefined If you continue to attempt to access another property on undefined , an exception will be thrown.如果您继续尝试访问undefined上的另一个属性,将抛出异常。 By using try...catch , you can catch such an error and return undefined .通过使用try...catch ,您可以捕获此类错误并返回undefined

Try this:尝试这个:

 function getValue(arr, arrKeys) { return arrKeys.reduce( (acum, current) => acum?.[current], arr ) } var arr1 = ['a', 'b', ['c', 'd'], ['e', 'f']]; var arr2 = [2, 0]; var arr3 = [3, 1]; console.log(getValue(arr1, arr2)) console.log(getValue(arr1, arr3))

You can create an array method Array#getNested as follows:您可以创建一个数组方法Array#getNested如下:

 Array.prototype.getNested = function(index) { let out = this; index.forEach(i => out = out[i]); return out; } const arr1 = ['a', 'b', ['c', 'd'], ['e', 'f'], ['g',['h',['i', 'j']]]]; const arr2 = [2, 0]; const arr3 = [3, 1]; const arr4 = [4, 1, 1, 0]; console.log( arr1.getNested(arr2) ); console.log( arr1.getNested(arr3) ); console.log( arr1.getNested(arr4) );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM