![](/img/trans.png)
[英]How can I get the full object in Node.js's console.log(), rather than '[Object]'?
[英]How to return an object rather than console log values
我编写了一个JavaScript函数来查找奇数整数,负整数数,平均值和中位数。
该代码完成了我想要的一切,但是,我想知道是否可以重写它以获取返回对象的功能,而不仅仅是控制台日志值。 我还包括了指向我的JS Bin的链接( https://jsbin.com/digagozuru/edit?html,css,js,console,output )
任何意见或建议,将不胜感激! 谢谢。
var arrayAnalyzer = function(myArray) {
var odds = 0;
var negatives = 0;
var avg;
var median;
for (var i = 0; i < myArray.length; i++) {
if (myArray[i] % 2 !== 0) {
odds += 1;
}
if (myArray[i] < 0) {
negatives += 1;
}
}
console.log("There are " + odds + " odd numbers.");
console.log("There are " + negatives + " negative numbers.");
var sum = myArray.reduce(function(previousValue, currentValue) {
return previousValue + currentValue;
});
avg = sum / myArray.length;
console.log("The average is " + avg.toFixed(2));
var orderedArray = myArray.sort(function(a, b) {
return a - b;
});
if (orderedArray.length % 2 === 0) {
var position1 = orderedArray.length / 2;
var position2 = position1 - 1;
median = (orderedArray[position1] + orderedArray[position2]) / 2;
} else {
var position = Math.floor(orderedArray.length / 2);
median = orderedArray[position];
}
console.log("The median is " + median);
};
arrayAnalyzer([7, -3, 0, 12, 44, -5, 3]);
var arrayAnalyzer = function(myArray) {
var odds = 0;
var negatives = 0;
var avg;
var median;
for (var i = 0; i < myArray.length; i++) {
if (myArray[i] % 2 !== 0) {
odds += 1;
}
if (myArray[i] < 0) {
negatives += 1;
}
}
console.log("There are " + odds + " odd numbers.");
console.log("There are " + negatives + " negative numbers.");
var sum = myArray.reduce(function(previousValue, currentValue) {
return previousValue + currentValue;
});
avg = sum / myArray.length;
console.log("The average is " + avg.toFixed(2));
var orderedArray = myArray.sort(function(a, b) {
return a - b;
});
if (orderedArray.length % 2 === 0) {
var position1 = orderedArray.length / 2;
var position2 = position1 - 1;
median = (orderedArray[position1] + orderedArray[position2]) / 2;
} else {
var position = Math.floor(orderedArray.length / 2);
median = orderedArray[position];
}
console.log("The median is " + median);
// Returns an object with named attributes
return {
odds:odds,
negatives:negatives,
avg:avg,
median:median
};
};
var myArray = arrayAnalyzer([7, -3, 0, 12, 44, -5, 3]);
console.log("Odds: " + myArray.odds +
"\nNegatives: " + myArray.negatives +
"\nAverage:" + myArray.avg +
"\nMedian: " + myArray.median);
我不确定是否能正确回答您的问题,但是为什么不在方法开始时创建一个对象,然后在计算出值后立即将其写入对象?
var arrayAnalyzer = function(myArray) { var odds = 0; var negatives = 0; var avg; var median; var result = {}; for (var i = 0; i < myArray.length; i++) { if (myArray[i] % 2 !== 0) { odds += 1; } if (myArray[i] < 0) { negatives += 1; } } result.negatives = negatives; result.odds = odds; var sum = myArray.reduce(function(previousValue, currentValue) { return previousValue + currentValue; }); avg = sum / myArray.length; result.avg = avg; var orderedArray = myArray.sort(function(a, b) { return a - b; }); if (orderedArray.length % 2 === 0) { var position1 = orderedArray.length / 2; var position2 = position1 - 1; median = (orderedArray[position1] + orderedArray[position2]) / 2; } else { var position = Math.floor(orderedArray.length / 2); median = orderedArray[position]; } result.median = median; return result; }; console.log(arrayAnalyzer([7, -3, 0, 12, 44, -5, 3]));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.