js遍历对象,获取对象数字属性的最大值

傷城~ 2023-01-15 08:10 247阅读 0赞
  1. methods: {
  2. getMax(rsp) {
  3. if (typeof rsp === 'undefined' || rsp == null) return 10;
  4. console.log('120' + JSON.stringify(rsp))
  5. let max = this.forEachObj(rsp);
  6. if (typeof max === 'undefined' || max == null) return 10;
  7. // 将数字转字符串
  8. if (typeof max === 'number') max = (max).toString();
  9. console.log('124:' + max)
  10. max = max.split('.')[0];
  11. if (max.length === 1) return 10;
  12. if (max.length === 2) return 100;
  13. let length = max.length,
  14. start = max.substr(0,1);
  15. start = parseInt(start) + 1;
  16. max = '' + start;
  17. for (let i = 0; i < length - 1; i ++)
  18. {
  19. max = max + '0';
  20. }
  21. // console.log(max)
  22. return max;
  23. },
  24. forEachObj(obj) { // 遍历对象获取最大值
  25. let max = 0,
  26. that = this;
  27. for (let key in obj) {
  28. if (Object.prototype.toString.call(obj[key]) === '[object Object]') {
  29. let result = that.forEachObj(obj[key]);
  30. if (result > max) max = result;
  31. } else if (Object.prototype.toString.call(obj[key]) === '[object Array]') {
  32. that.forEachArr(obj[key])
  33. } else {
  34. if ('totalTransactions' !== key && 'totalFlow' !== key) {
  35. if (typeof obj[key] === 'string' && /^[1-9][0-9]*([\\.][0-9]{1,2})?$/.test(obj[key])) {
  36. if (obj[key] > max) max = obj[key];
  37. } else if (typeof obj[key] === 'number') {
  38. if (obj[key] > max) max = obj[key];
  39. }
  40. }
  41. }
  42. }
  43. return max;
  44. },
  45. forEachArr(arr) {
  46. let that = this;
  47. if (Object.prototype.toString.call(arr) === '[object Array]') {
  48. arr.forEach(item => {
  49. that.forEachObj(item);
  50. })
  51. }
  52. },
  53. }

发表评论

表情:
评论列表 (有 0 条评论,247人围观)

还没有评论,来说两句吧...

相关阅读

    相关 js(jsjson对象)

    遍历是什么意思啊??? 在数据结构中,遍历就是指沿着某条搜索路线,依次对路线中每个结点均做一次并且只做一次访问 ![js遍历(js遍历json对象)\_js遍历(js遍