我一直在看这个,阅读了一段时间,但似乎无法理解。我确信这是简单的,但我不知道为什么我可以访问收集的数据。我得到了一个错误
测试未定义
如果我在for循环之外移动,我可以很好地访问它。我显然错过了一些简单的东西。任何帮助都会很好。
function whatIsInAName(collection, source) {
// What's in a name?
var arr = [];
// Only change ode below this line
var test = Object.values(source);
for(var i = 0; i < collection.length; i++) {
if (collection[i].test === source[0].test){
arr[i] = collection[i];
}
}
// Only change code above this line
//arr = collection[0].last;
return arr;
}
whatIsInAName([{ first: "Romeo", last: "Montague" }, { first: "Mercutio",
last: null }, { first: "Tybalt", last: "Capulet" }], { last: "Capulet" });发布于 2017-03-07 06:35:23
要传入的对象没有名为test的密钥。如果您试图使用您在这里创建的变量var test = Object.values(source);,这将无法工作,因为test无论如何都是一个数组。
你可以尝试这样的方法:
if (collection[i][test] === source[0][test])但我认为这也行不通,因为根据test的文档,Object.values()的值应该是Capulet
https://stackoverflow.com/questions/42641574
复制相似问题