简体   繁体   English

如何返回对象而不是控制台日志值

[英]How to return an object rather than console log values

I wrote a JavaScript function to find the number of odd integers, number of negative integers, average, and median value. 我编写了一个JavaScript函数来查找奇数整数,负整数数,平均值和中位数。

The code is accomplishing everything that I wanted it to, however, I am wondering if I can rewrite it to get the function to return an object rather than just the console log values. 该代码完成了我想要的一切,但是,我想知道是否可以重写它以获取返回对象的功能,而不仅仅是控制台日志值。 I've also included a link to my JS Bin ( https://jsbin.com/digagozuru/edit?html,css,js,console,output ) 我还包括了指向我的JS Bin的链接( https://jsbin.com/digagozuru/edit?html,css,js,console,output

Any advice or suggestions would be appreciated! 任何意见或建议,将不胜感激! Thanks. 谢谢。

  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);

I'm not sure if I get your question right, but why dont create an object at the beginning of your method and then write the values in the object as soon as you calculated them? 我不确定是否能正确回答您的问题,但是为什么不在方法开始时创建一个对象,然后在计算出值后立即将其写入对象?

 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.

相关问题 如何在 Node.js 的 console.log() 中获取完整的 object,而不是“[Object]”? - How can I get the full object in Node.js's console.log(), rather than '[Object]'? 如何在Winston中将错误对象记录到文件而不是控制台? - How do you log an error object to file rather than console in Winston? 使用 return 关键字而不是仅使用 console.log function 并仅传递参数有什么区别? - What is the difference of using the return keyword rather than using just a console.log function and just passing the parameters? Object 不在控制台日志中显示值 - Object is not displaying values in console log 如何获取查找表以返回字符串而不是 [object Object] - How to get a look up table to return a string rather than [object Object] JavaScript 对象可以返回值和控制台日志吗? - Can a JavaScript object return a value AND console log? 如何在 Three.js 中返回对象而不是将其添加到场景中? (FBXLoader) - How to return an object in Three.js rather than add it to a scene? (FBXLoader) console.log()在控制台以外的对象上调用 - console.log() called on object other than console 如何使Fetch API从JSON文件返回键及其值,而不是在控制台中返回整个文件 - How to make the Fetch API return a key and its value from a JSON file, rather than returning the entire file in console getJSON显示[object Object]而不是实际值 - getJSON displays [object Object] rather than actual values
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM