简体   繁体   English

使用对象映射数组

[英]Mapping throught an array with Objects

new to React Native, how would I go about getting values from this data Object. React Native 的新手,我将如何 go 从该数据 Object 中获取值。 In JSX return statement, I'm mapping through the whole array and getting access to data.x and data.y, but how would I map through the "z" object to get the values for each key?在 JSX return 语句中,我正在映射整个数组并访问 data.x 和 data.y,但是我如何通过“z”object map 来获取每个键的值? For example "nimi": "Chest Fly" ("Chest Fly" would be the one needed in this case).例如“nimi”:“Chest Fly”(在这种情况下需要“Chest Fly”)。 Thank you in advance!先感谢您!

Array [
  Object {
    "x": "7. toukokuuta 2021",
    "y": "rintatreeni",
    "z": Object {
      "Chest Fly": Object {
        "nimi": "Chest Fly",
        "sarjat": 3,
        "toistot": 10,
      },
      "Dipit": Object {
        "nimi": "Dipit",
        "sarjat": 3,
        "toistot": "10-15",
      },
      "Penkkipunnerrus": Object {
        "nimi": "Penkkipunnerrus",
        "sarjat": 3,
        "toistot": 10,
      },
      "Punnerrukset": Object {
        "nimi": "Punnerrukset",
        "sarjat": 3,
        "toistot": "5-10",
      },
      "Pystypunnerrus": Object {
        "nimi": "Pystypunnerrus",
        "sarjat": 3,
        "toistot": "8-10",
      },
      "Tricep Pushdown": Object {
        "nimi": "Tricep Pushdown",
        "sarjat": 3,
        "toistot": 10,
      },
      "Vipunosto": Object {
        "nimi": "Vipunosto",
        "sarjat": 3,
        "toistot": 10,
      },
    },
  },
]

Here is my current code:这是我当前的代码:

 <List.Section title="Tehdyt Treenit">
                   {
                       treenit.map((item, index) => {
                        
                           return(
                            
                        <List.Accordion key={index}
                        title={`${item.x} - ${item.y}`}
                        left={props => <List.Icon {...props} icon="folder" />}
                        >
                       {Object.keys(item.z).map(key,idx => {
                            return(
                            <List.Item key={idx} title={treeniData[key]} />
                            )
                        })} 
                     
                    </List.Accordion>
                               
                              
                           )
                       })
                   }
                   </List.Section>

"treenit" is an array state. “treenit”是一个数组 state。

EDIT:编辑:

According to List.Item API https://callstack.github.io/react-native-paper/list-item.html I would write something like: According to List.Item API https://callstack.github.io/react-native-paper/list-item.html I would write something like:

{Object.values(item.z).map(zItem => {
  let desc = `Sarjat: ${zItem.sarjat}, Toistot: ${zItem.toistot}`;
  return(
    <List.Item key={zItem.nimi} title={zItem.nimi} description={desc} />
  )
})}

ORIGINAL ANSWER:原始答案:

You can use Object.values() to loop over the z object, eg您可以使用 Object.values() 来循环 z object,例如

yourArray.map(item => {
  return Object.values(item.z).find(zItem => zItem.nimi === "Chest Fly");
})

Result:结果:

[{
    "nimi": "Chest Fly",
    "sarjat": 3,
    "toistot": 10
}]

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

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