Javascript 为什么我要让NaN使用此阵列?
我正试图通过这本书来改进我的JS。我正在研究解决问题的方法,希望能解释一下为什么它是错误的,然后返回NaN。我的解决办法是:Javascript 为什么我要让NaN使用此阵列?,javascript,arrays,Javascript,Arrays,我正试图通过这本书来改进我的JS。我正在研究解决问题的方法,希望能解释一下为什么它是错误的,然后返回NaN。我的解决办法是: function average(array) { function plus(a, b) { return a + b; } return array.reduce(plus) / array.length; } var byName = {}; ancestry.forEach(function(person) { byName[person.name]
function average(array) {
function plus(a, b) { return a + b; }
return array.reduce(plus) / array.length;
}
var byName = {};
ancestry.forEach(function(person) {
byName[person.name] = person;
});
var knownMothers = ancestry.filter(function(person){
return person.mother != null;
});
var ageDifferences = knownMothers.map(function(person){
return person.born - [person.mother].born
});
console.log(knownMothers);
console.log(ageDifferences);
console.log(average(ageDifferences));
最后两个console.log statemets返回NaN
祖先是一个如下格式的数组:
[0: {
name: "Carolus Haverbeke"
sex: "m"
born: 1832
died: 1905
father: "Carel Haverbeke"
mother: "Maria van Brussel"
}
1: {
name: "Emma de Milliano"
sex: "f"
born: 1876
died: 1956
father: "Petrus de Milliano"
mother: "Sophia van Damme"
}]
此练习的解决方案位于上面的链接中。它在代码中使用byName对象,因此绕过了在输出中获取NaN。有人能解释一下为什么我需要返回byName对象中的元素吗?我知道这是缺失的部分,但不知道为什么,我真的很想理解。谢谢大家!
我复制了下面我不太明白的答案以供参考:
var differences = ancestry.filter(function(person) {
return byName[person.mother] != null;
}).map(function(person) {
return person.born - byName[person.mother].born;
});
console.log(average(differences));
我相信你对年龄差异的定义中没有提到名字。还有一个安全的防范措施,防止名字[个人.母亲]没有设定。问题就在这里
return person.born - [person.mother].born
person.born是一个数字&
根据您的定义,[person.mother]应该是从[]开始的数组。和[person.mother]。根据要求,born也应该是一个数字。因此,您不能使用[person.mother].born,您应该在对象中找到person.mother键的值
当你这样做的时候
console.log(average(ageDifferences));
方法“average”用于将数字数组作为参数
因为,年龄差异不是以数字为元素,所以它的给出是错误的
之所以使用NaN,是因为您试图在第二个操作数甚至不存在的地方减去person.born-person.mother.born。所以它是数字-未定义。所以你得到了NaN
对于knownMothers,您将其等同于null,这可能不符合所有条件。您还需要更新knownMothers筛选器。您需要检查此人是否有母亲,以及母亲的名字是否存在于byName对象中。如下图所示:
var knownMothers = ancestry.filter(function(person){
return person.mother && byName[person.mother];
});
一旦你这样做了,你就可以得到年龄差异和平均值
var ancestry = [{
name: "Carolus Haverbeke",
sex: "m",
born: 1832,
died: 1905,
father: "Carel Haverbeke",
mother: "Maria van Brussel"
}, {
name: "Emma de Milliano",
sex: "f",
born: 1876,
died: 1956,
father: "Petrus de Milliano"
}, {
name : "Maria van Brussel",
sex: "m",
born: 1782,
died: 1905,
father: "Carel Haverbeke",
mother: "Maria van"
}];
function average(array) {
function plus(a, b) { return a + b; }
return array.reduce(plus) / array.length;
}
var byName = {};
ancestry.forEach(function(person) {
byName[person.name] = person;
});
var knownMothers = ancestry.filter(function(person){
return person.mother && byName[person.mother];
});
var ageDifferences = knownMothers.map(function(person){
return person.born - byName[person.mother].born;
// assumption: you are trying to get mother's born year from the byName object
});
console.log(knownMothers);
console.log(ageDifferences);
console.log(average(ageDifferences));
您希望[person.mother].生来做什么?我知道这是错误的,但我希望它访问[person.mother]的密钥,然后获取相应母亲的.born值。现在我明白了,我不一定能够通过这种方式访问这些值,我需要通过byName对象来获取出生年份。谢谢你的帮助!!!