简体   繁体   English

如何检索数组的元素作为对象属性

[英]How do I retrieve the element of an array as an object property

My question is why is this function not passing the test that it should return the element at the index of the array at the key of the passed in object . 我的问题是,为什么这个函数没有通过测试,即它should return the element at the index of the array at the key of the passed in object

The problem is: 问题是:

Given an object, a key, and a numerical index, "getElementOfArrayProperty" returns the value of the element at the given index of the array located within the given object at the given key. 给定一个对象,一个键和一个数字索引,“ getElementOfArrayProperty”返回位于给定键的给定对象内的数组在给定索引处元素的值。

Notes: * If the array is empty, it should return undefined. 注意:*如果数组为空,则应返回undefined。

  • If the given index is out of range of the array located at the given key, it should return undefined. 如果给定索引超出给定键所在数组的范围,则它应返回undefined。
  • If the property at the given key is not an array, it should return undefined. 如果给定键处的属性不是数组,则应返回undefined。
  • If there is no property at the key, it should return undefined. 如果键上没有属性,则应返回undefined。

When I pass the sample object: var obj = { key: ['Jamil', 'Albrey'] }; 当我传递示例对象时: var obj = { key: ['Jamil', 'Albrey'] }; it returns "jamil" . 它返回"jamil"

My Code: 我的代码:

     function getElementOfArrayProperty(obj, key, index) {

    if (!obj["key"]) return undefined
      return obj["key"][index]
    }

    getElementOfArrayProperty(obj, "key", 0)  // returns "jamil"

Yet it still fails the test 然而它仍然没有通过测试

I think Nol is right. 我认为Nol是对的。 The problem is that there is a little confusion with key as a property / parameter and "key" as a string literal. 问题在于key作为属性/参数和“ key”作为字符串文字存在一些混淆。 Let's see if this clear the things up: 让我们看看这是否可以解决问题:

 var obj = { foo: ['Jamil', 'Albrey'] }; function getElementOfArrayProperty(obj, key, index) { if (!obj[key]) return undefined; return obj[key][index]; } alert( getElementOfArrayProperty(obj, "foo", 0) ); 

EDIT : And if you are wondering why "foo" is between quotes, that's because it is not a variable, it is a string literal with the name of the object's property. 编辑 :并且,如果您想知道为什么"foo"在引号之间,那是因为它不是变量,它是带有对象属性名称的字符串文字。 If you want to pass a variable as parameter, first you need to create it, like: 如果要将变量作为参数传递,则首先需要创建它,例如:

var bar = obj["foo"]; // or  
var bar = obj.foo;

And then you can use it in the function invocation: 然后可以在函数调用中使用它:

alert( getElementOfArrayProperty(obj, bar, 0) );

But in this case, you don't need to pass the obj variable, because bar already refers to one property of the obj object: 但是在这种情况下,您不需要传递obj变量,因为bar已经引用了obj对象的一个​​属性:

 var obj = { foo: ['Jamil', 'Albrey'] }; var bar = obj.foo; function getElementOfArrayProperty(key, index) { if (!key) return undefined; return key[index]; } alert(getElementOfArrayProperty(bar, 0)); 

I'm not sure if this is a typo, but in your code example, you are passing in the string "key" to obj like obj["key"] instead of using the parameter key from your method definition. 我不确定这是否是错字,但是在您的代码示例中,您将像obj["key"]一样将字符串“ key”传递给obj,而不是使用方法定义中的参数key Using obj[key] might improve your results. 使用obj[key]可能会改善您的结果。

Here's what I meant: 这就是我的意思:

  var obj = { key: ['Jamil', 'Albrey'] }; function getElementOfArrayProperty(obj, key, index) { if (!obj[key]) { return undefined; } return obj[key][index]; } function getArrayByKey(obj, key) { return obj[key]; } alert(getElementOfArrayProperty(obj, "key", 0) + "\\n" + getArrayByKey(obj, "key")); 

You'll see that this doesn't return undefined if you run it, instead it returns Jamil just like your example with the hard-coded "key" . 您将看到,如果您运行它,它不会返回undefined,而是返回Jamil,就像您的示例带有硬编码的"key"

If I look for just obj[key] I still end up getting the whole array object, so I can't seem to reproduce your issue. 如果仅寻找obj[key]我仍然会得到整个数组对象,因此似乎无法重现您的问题。

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

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