我不是真正的初学者,但这可能是一个初学者的问题。我试图增加ES6 JavaScript对象的count属性。这个最小代码示例中最成功的两个(如他们给我错误的信息,但没有错误)版本返回" NaN"或" 1"对于每个水果计数属性。这似乎应该可以工作,但显然增加一个数字对象属性并不是一个直观的概念。
所以,这就是我想要做的事情:
创建水果对象作为关键字以及它们出现的次数。结束对象看起来像这样:
fruitsObj[fruit].name
fruitsObj[fruit].count
对于" apple":
fruitsObj.apple.name // apple
fruitsObj.apple.count // 4
但是,我收到的输出是:
{apple: {…}, pear: {…}, banana: {…}, orange: {…}, kumquat: {…}}
apple:{name: "apple", count: NaN}
banana:{name: "banana", count: NaN}
kumquat:{name: "kumquat", count: NaN}
orange:{name: "orange", count: NaN}
pear:{name: "pear", count: NaN}
__proto__: Object
在版本2中,我尝试进行存在和类型检查,但无济于事。我得到" 1"作为每个计数的唯一值:
{apple: {…}, pear: {…}, banana: {…}, orange: {…}, kumquat: {…}}
apple:{name: "apple", count: 1}
banana:{name: "banana", count: 1}
kumquat:{name: "kumquat", count: 1}
orange:{name: "orange", count: 1}
pear:{name: "pear", count: 1}
__proto__: Object
我不确定我在这里做错了什么。
// Declare an array of fruits
var fruitsArr = ['apple', 'pear', 'apple', 'banana', 'orange', 'kumquat', 'orange', 'apple', 'apple', 'orange'];
var fruitsObj = {};
// Loop through each fruit storing name and count.
fruitsArr.forEach(function(fruit, i) {
// Create each named-fruit property in object as a sub-object.
fruitsObj[fruit] = {};
fruitsObj[fruit].name = fruit;
// FAILS with NaN!
fruitsObj[fruit].count = ++fruitsObj[fruit].count;
});
console.log(fruitsObj);

// Declare an array of fruits
var fruitsArr = ['apple', 'pear', 'apple', 'banana', 'orange', 'kumquat', 'orange', 'apple', 'apple', 'orange'];
var fruitsObj = {};
// Loop through each fruit storing name and count.
fruitsArr.forEach(function(fruit, i) {
// Create each named-fruit property in object as a sub-object.
fruitsObj[fruit] = {};
fruitsObj[fruit].name = fruit;
// if fruit count exists and is greater than zero
if (fruitsObj[fruit].count != undefined && fruitsObj[fruit].count > 0) {
// increment it
fruitsObj[fruit].count++;
} else {
// set it to one
fruitsObj[fruit].count = 1;
}
});
console.log(fruitsObj);

我查看了一堆帖子和链接,这些帖子和链接让我陷入了一个兔子洞。即便如此,他们都没有清楚地解决我能说出来的问题。
答案 0 :(得分:3)
为什么我的计数器没有增加,而是返回NaN'?
因为您尝试在第一种情况下增加未定义的属性。在行
fruitsObj[fruit].count = ++fruitsObj[fruit].count;
fruitsObj[fruit].count
未定义,因为您从未为其分配任何数字值。您在第二个代码块中更正了此问题。
在理解这项工作的过程中,我缺少什么?我怎么能够 增加我的对象计数属性?
第二个代码的问题是这一行
// Create each named-fruit property in object as a sub-object.
fruitsObj[fruit] = {};
您在每次迭代中分配一个新对象。因此,即使这个水果第二次出现,新对象在分配新对象时会覆盖先前的值。您可以通过检查此对象是否已存在来消除此问题
// Declare an array of fruits
var fruitsArr = ['apple', 'pear', 'apple', 'banana', 'orange', 'kumquat', 'orange', 'apple', 'apple', 'orange'];
var fruitsObj = {};
// Loop through each fruit storing name and count.
fruitsArr.forEach(function(fruit, i) {
// Create a fruit object only if it does not exist already.
if (!fruitsObj[fruit]) {
fruitsObj[fruit] = {};
}
fruitsObj[fruit].name = fruit;
// if fruit count exists and is greater than zero
if (fruitsObj[fruit].count != undefined && fruitsObj[fruit].count > 0)
{
// increment it
fruitsObj[fruit].count++;
} else {
// set it to one
fruitsObj[fruit].count = 1;
}
});
console.log(fruitsObj);

答案 1 :(得分:2)
使用count: 0
初始化水果的对象,当它不存在时,然后为每个具有相同名称的水果将计数递增1:
// Declare an array of fruits
var fruitsArr = ['apple', 'pear', 'apple', 'banana', 'orange', 'kumquat', 'orange', 'apple', 'apple', 'orange'];
var fruitsObj = {};
// Loop through each fruit storing name and count.
fruitsArr.forEach(function(fruit) {
// if the object lacks a [fruit] property
if(!fruitsObj[fruit]) {
// create a new [fruit] object, with the name and count: 0
fruitsObj[fruit] = {
name: fruit,
count: 0
};
}
// increment the count of the [fruit] object
fruitsObj[fruit].count++;
});
console.log(fruitsObj);
答案 2 :(得分:1)
在forEach循环中,只尝试以下两行:
if(fruitObj[fruit]) fruitObj[fruit].count+=1;
else fruitObj[fruit] = {name: fruit, count: 1};
这是实例化或递增,具体取决于先前的实例化。您无法增加未实现的值,并且在第二个版本中,您每次都(无条件地)实例化新的空对象。