简体   繁体   English

Object.entries 部分在 REACT 中工作,为什么?

[英]Object.entries is working partially in REACT why?

I'm not able to display only : "Mona", "Paul","Bastian" , I can display : "Mona", "Paul","Bastian","Average" but I don't want to display "Average".我不能只显示: "Mona", "Paul","Bastian" ,我可以显示: "Mona", "Paul","Bastian","Average"但我不想显示“Average” ”。 And display "Average", in my second bloc of radars [{...}, { name: 'Average',dataKey: 'Average'} .并在我的第二组radars [{...}, { name: 'Average',dataKey: 'Average'}中显示“Average”。 What is wrong please??请问有什么问题吗??

const { names, locations } = graphs ?? {}
function RadarNames() {
    const data = Object.entries(graphs.names[0]).map(([key, value]) => ({
        "name": key,
        "age": value,
    }))
    return creacteChart({
        data: data,
        polarAngleAxisOptions: { dataKey: 'name' },
        radars: [
            {
                name: 'name',
                dataKey: 'age',
            
            },
            {
                name: 'Average',
                dataKey: 'Average',
             
            },

        ],
    })
}

I tried to do :我试着做:

const data = Object.entries(graphs.names[0]!=='Average'? graphs.names[0].map(([key, value]) => ({"name": key,"age": value})):'')

But it's not working...但它不工作...

My json from my api is :我的 api 中的 json 是:

{
    "names": [
        {
            "Mona": 63,
            "Paul": 29,
            "Bastian": 31,
            "Average": 75
          }
      
    ],

    "locations": [
        {
            "location": "Dublin",
            "country":"Irland"
        }
    ]
}

Use filter :使用filter

const data = Object.entries(graphs.names[0])
                   .filter(([key]) => key != "Average")
                   .map(([name, age]) => ({name, age}));

An alternative, is to spread the object to extract the Average (and ignore it) and get the rest in a new object:另一种方法是扩展对象以提取平均值(并忽略它)并将其余部分放入新对象中:

const {Average, ...rest} = graphs.names[0];
const data = Object.entries(rest)
                   .map(([name, age]) => ({name, age}));
graphs = {
    "names": [
          {
            "Mona": 63,
            "Paul": 29,
            "Bastian": 31,
            "Average": 75
          }
      
    ],

    "locations": [
        {
            "location": "Dublin",
            "country":"Irland"
        }
    ]

    };

const { names, locations } = graphs ?? {}

function RadarNames() {
    const data = Object.entries(graphs.names[0]).filter(([key]) => key != "Average").map(([key, value]) => ({
        "name": key,
        "age": value,
    }))

    const dataAv = Object.entries(graphs.names[0]).filter(([key]) => key == "Average").map(([key, value]) => ({
        "Average": key,
        "average": value,
    }))

    return ({
        data: data, dataAv,
        polarAngleAxisOptions: { dataKey: 'name' },
        radars: [
            {
                name: 'name',
                dataKey: 'age',
            
            },
            {
                name: 'Average',
                dataKey: 'average',
             
            },

        ],
    })
}

console.log(RadarNames());  

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM