Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/367.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/user-interface/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript for in-loop in jscript对象显示为文本_Javascript_Arrays_Javascript Objects - Fatal编程技术网

Javascript for in-loop in jscript对象显示为文本

Javascript for in-loop in jscript对象显示为文本,javascript,arrays,javascript-objects,Javascript,Arrays,Javascript Objects,代码应该将数组中的值(QuantitySeld)相加,然后求平均值,但我似乎无法显示循环中的值,当您运行代码时,它只显示为文本,您需要将数组中的所有数字相加,然后除以数组的长度。是一种很好的循环和求和数据的方法: var乘积={ 名称:“Soap”, 品牌:'鸽子', 价格:25.50,, 去中心化:5, 售出数量:[10,5,25,8,4], netPrice:function(){ 返回此.price*(100-此折扣)/100; }, 平均销售额:函数(){ 返回this.quantit

代码应该将数组中的值(QuantitySeld)相加,然后求平均值,但我似乎无法显示循环中的值,当您运行代码时,它只显示为文本,您需要将数组中的所有数字相加,然后除以数组的长度。是一种很好的循环和求和数据的方法:

var乘积={
名称:“Soap”,
品牌:'鸽子',
价格:25.50,,
去中心化:5,
售出数量:[10,5,25,8,4],
netPrice:function(){
返回此.price*(100-此折扣)/100;
},
平均销售额:函数(){
返回this.quantitysell.length&&this.quantitysell.reduce(函数(sum,num){
返回sum+num;
})/this.quantitysell.length;
}
}

console.log(Product.averageSales())您需要将数组中的所有数字相加,然后将它们除以数组的长度。是一种很好的循环和求和数据的方法:

var乘积={
名称:“Soap”,
品牌:'鸽子',
价格:25.50,,
去中心化:5,
售出数量:[10,5,25,8,4],
netPrice:function(){
返回此.price*(100-此折扣)/100;
},
平均销售额:函数(){
返回this.quantitysell.length&&this.quantitysell.reduce(函数(sum,num){
返回sum+num;
})/this.quantitysell.length;
}
}
console.log(Product.averageSales())请尝试以下操作:

        var Product = {
            name : 'Soap',
            brand : 'Dove',
            price : 25.50,
            discountRate : 5,
            quantitySold: [10,5,25,8,4],
            netPrice:function(){return this.price * (100 - this.discountRate) / 100;},
            averageSales:function(){
                for(var sum in this.quantitySold){
                    var i = 0;
                    i++
                    sum += this.quantitySold[i];
                }
                return sum
            }
        }
试试这个:

        var Product = {
            name : 'Soap',
            brand : 'Dove',
            price : 25.50,
            discountRate : 5,
            quantitySold: [10,5,25,8,4],
            netPrice:function(){return this.price * (100 - this.discountRate) / 100;},
            averageSales:function(){
                for(var sum in this.quantitySold){
                    var i = 0;
                    i++
                    sum += this.quantitySold[i];
                }
                return sum
            }
        }

您不应在
循环中使用
for..for,该数组应用于
对象
。您可以使用常规的
for
循环,如下所示

var乘积={
名称:“Soap”,
品牌:'鸽子',
价格:25.50,,
去中心化:5,
售出数量:[10,5,25,8,4],
netPrice:function(){返回this.price*(100-this.discentrate)/100;},
平均销售额:函数(){
var总和=0;
对于(var i=0;iconsole.log(Product.averageSales())
您不应在循环中使用for..和应用于对象的数组。您可以使用常规的
for
循环,如下所示

var乘积={
名称:“Soap”,
品牌:'鸽子',
价格:25.50,,
去中心化:5,
售出数量:[10,5,25,8,4],
netPrice:function(){返回this.price*(100-this.discentrate)/100;},
平均销售额:函数(){
var总和=0;
对于(var i=0;iconsole.log(Product.averageSales())我为。。。在中,将
sum
值设置为
this.quantitysell
properties名称,但执行所需循环的等效方法是为。。。of(在ES4中为…每个)迭代集合。当然,您应该声明
sum
使用
0
初始化,其值不会受到影响。因为我们将使用…of,所以由于语法的原因,我们只能在其参数之外声明
sum
。如果这是一个常见的for,我们可以在内部声明:
for(var…,sum=0;;)


我为。。。在中,将
sum
值设置为
this.quantitysell
properties名称,但执行所需循环的等效方法是为。。。of(在ES4中为…每个)迭代集合。当然,您应该声明
sum
使用
0
初始化,其值不会受到影响。因为我们将使用…of,所以由于语法的原因,我们只能在其参数之外声明
sum
。如果这是一个常见的for,我们可以在内部声明:
for(var…,sum=0;;)

最好创建一个函数,将对象作为第一个参数,并将运行此findAverage代码的函数作为第二个参数。这样,您可以在任何对象上运行它,因此它是可重用的


最好创建一个函数,将对象作为第一个参数,并将运行此findAverage代码的函数作为第二个参数。这样你就可以在任何对象上运行它,这样它就可以重复使用。

如果你在中使用
,那么sum将是数组的索引如果你在中使用
,那么sum将是数组的索引。你应该告诉op的代码中有什么问题,为什么它不工作。只是不要给出你的意见。@Mahi-添加了问题的描述。谢谢。你应该告诉op代码中的问题是什么,为什么它不起作用。只是不要给出你的意见。@Mahi-添加了问题的描述。谢谢。谢谢,我试过那个代码,我认为它不起作用,然后我在显示它时出错了XD,而不是Product.averageSales(),我输入了Product.averageSales。无论如何,感谢您提供的解决方案。谢谢,我尝试了该代码,我认为它不起作用,然后我在显示它时出错了XD,而不是Product.averageSales(),我键入了Product.averageSales。无论如何,谢谢你的解决方案。
sum = 0;

for (let quantity of this.quantitySold)
    sum += quantity;

var sum;
return sum;
var Product = {
    name : 'Soap',
    brand : 'Dove',
    price : 25.50,
    discountRate : 5,
    quantitySold: [10, 5, 25, 8, 4],
    netPrice: function(){ 
        return this.price * (100 - this.discountRate) / 100;
    },
    findAverage: function() {
    // create a var for the quantitySold Array
    var quantity = Product.quantitySold;

    // find the sum of each item in the Array using reduce
    var sum = quantity.reduce(function(a, b) {
        return (a + b);
    });

    // take the average of the sum divided by the number of items in the Array
    return avg = sum / quantity.length;
  }
}

// Run the function to return the avg
Product.findAverage();