程序问答   发布时间:2022-06-02  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了在 Object 中查找最大值 - React Hook大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

如何解决在 Object 中查找最大值 - React Hook?

开发过程中遇到在 Object 中查找最大值 - React Hook的问题如何解决?下面主要结合日常开发的经验,给出你关于在 Object 中查找最大值 - React Hook的解决方法建议,希望对你解决在 Object 中查找最大值 - React Hook有所启发或帮助;

我在 React 中有一个包含这些值的状态:

import React from "react";
import { menuprops,MenuItemlink } from "react-admin";

const SIDeMenu: React.ComponentType<menuprops> = ({ dense = false }) => {
    return (
        <div
            classname={CSS`
                margin-top: "1vw";
            `}
        >
            <br />
            <MenuItemlink
                classname={CSS`
                    color: ${Colors.WHITE}; // priMaryText color
                `}
                to="/"
                priMaryText={"dashbord"}
                leftIcon={<Image wIDth={ICON_WIDTH} src={"/img/dashboard-w.svg"} prevIEw={falsE} />}
                exact
                dense={densE}
            />
            <br />
            <MenuItemlink
                classname={CSS`
                    color: ${Colors.WHITE};
                `}
                to="/users"
                priMaryText={"user List"}
                leftIcon={<Image wIDth={ICON_WIDTH} src={"/img/user-group-w.svg"} prevIEw={falsE} />}
                exact
                dense={densE}
            />
        </div>
    );
};

...

  <Layout
            {...props}
            menu={SIDeMenu}
        />

这个状态扩展,客户可以在状态中添加或删除行,所以我需要以动态的方式找到Object中值较高的键/值。

我试过了,但它只检索第一个键/值:

{
   income 1 : 500,income 2 : 300,income 3 : 1000
}

有什么想法吗?

解决方法

看看这个。沙盒 https://codesandbox.io/s/elegant-forest-ik420?file=/src/App.js

您可以使用 for in 循环遍历对象

 const salaries = {
    income1: 1000,income2: 2000,income3: 3000
  };


 const getMax = (data) => {
    let valuesList = []; //The list we're gonna push the values to and then get the maximum out of it
    for (const key in data) { //Looping through
      if (data.hasOwnProperty(key)) { //check if key exists in the object (optional)
        const element = data[key]; //Get the value of that key and assign it to a variable
        valuesList.push(element); //Push that to the array
      }
    }
    const max = Math.max(...valuesList); //Find the maximum value in the array(array will contain only values of the object)
    return max; //Returning it
  };

这是你如何称呼它

 getMax(salaries)
,

它返回第一个值的原因是因为您没有任何条件逻辑阻止第一次返回。

要解决这个问题,您需要首先找到具有最大值的记录并返回该记录,并且仅在最后返回。

export const maxIncome = (userWalleTincomes) => {
    let maxValue = 0;
    let maxKey = '';
    for (const [key,value] of Object.entries(userWalleTincomes)) {
        if(value > maxvalue) {
          maxValue = value;
          maxKey = key
        }
    }
    return `${maxKey} : ${maxvalue}`
}
@H_262_48@ @H_262_48@

上面的代码可以进一步简化,但它应该清楚地表明您需要在返回之前先找到最大值。

,

在这里,您没有在返回之前提供任何条件,而是在没有任何比较的情况下对单个值实施 Math.max。

此解决方案对您有用

export const maxIncome = (userWalleTincomes) => {
    for (const [key,value] of Object.entries(userWalleTincomes)) {
     if(Math.max.apply(null,Object.values(userWalleTincomes)) === value){
        return `${key} : ${value}`
        }
    }
}
@H_262_48@ @H_262_48@

@H_262_48@

@H_262_48@

大佬总结

以上是大佬教程为你收集整理的在 Object 中查找最大值 - React Hook全部内容,希望文章能够帮你解决在 Object 中查找最大值 - React Hook所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。