【问题标题】:Nested mapping reactjs嵌套映射 reactjs
【发布时间】:2020-03-06 10:49:26
【问题描述】:

我正在我的 react 应用中映射对象列表,如下所示

(countrydata !== null) ? Object.keys(countrydata).map((item, key) => {
  return (
    <img src={countrydata[item].image_location}/>
  )
})

我还有一个数组,它的对象数量与我上面映射的对象列表中的对象数量完全相同。我想显示数组对象中的某些数据,我尝试做这样的事情

(countrydata !== null) ? Object.keys(countrydata).map((item, key) => {
  arrayOfObjects.map((arrayItem,key)=>{
    return (
      <div>
        <img src={countrydata[item].image_location}/>
        <span>{arrayItem.name}</span>
      </div>
    )
  })            
})

但无法达到我想要的结果。如何在对象列表的映射中映射对象数组?

编辑:

我的对象列表如下所示(国家数据)

place_1:{description:'',image_location:'',location:''}
place_2:{description:'',image_location:'',location:''}
place_3:{description:'',image_location:'',location:''}

我的对象数组看起来像这样 (arrayOfObjects)

0: {distance: {…}, duration: {…}, status: "OK"}
1: {distance: {…}, duration: {…}, status: "OK"}
2: {distance: {…}, duration: {…}, status: "OK"}

【问题讨论】:

  • 能否请您发布一个对象countrydataarrayOfObjects 的小示例?
  • @ibrahimmahrir 我已经在问题中更新了它
  • 第一个对象中的键是否总是像place_N?会不会有像place_1, place_2, place_5 这样的缺失号码?第一个对象实际上应该是一个对象数组,这会让事情变得更容易
  • 不,它总是按升序排列。不会有任何遗漏的号码
  • @ibrahimmahrir 我已经正确完成了对象映射部分。我只想有序地显示我的数组中的数据

标签: javascript reactjs nested-map


【解决方案1】:

您不需要另一个嵌套的map。您可以只使用一个map 同时map 他们两个,您将使用提供给回调的索引来访问另一个数组/对象中的项目。

顺便说一句,由于对象键的顺序不可靠,我建议您在数组arrayOfObjects 上使用map 并使用索引生成用于从countrydata 访问匹配对象的键:

arrayOfObjects.map((arrayItem, index) => {
  let key = "place_" + (index + 1);                      // generate the key to access an object from 'countrydata'. If we are at the first object (index == 0), the key will be "place_1"
  return (
    <div>
      <img src={countrydata[key].image_location}/>       // access the matching object from 'countrydata' and use it
      <span>{arrayItem.name}</span>                      // access the current array item from 'arrayOfObjects'
    </div>
  );
})

【讨论】:

    【解决方案2】:

    您可以合并两个数组并合并第二个数组中的值。

    const data = {
    place_1: { name: 'One'},
    place_2: { name: 'Two'},
    place_3: { name: 'Three'},
    };
    
    const countrydata = [];
    
    const locations = [{distance: {}, duration: {}, status: "OK"},
    {distance: {}, duration: {}, status: "OK"},
    {distance: {}, duration: {}, status: "OK"}]
    
    Object.keys(data).forEach((key, index) => countrydata.push({ [key]: { ...data[key], distance: locations[index].distance, duration: locations[index].duration }}))
    
    console.log(countrydata);

    然后像这样渲染数组

    countrydata.map((item, key) => {
     return (
      <div>
        <img src={countrydata['place_' + key].image_location}/>
        <span>{countrydata['place_' + key].name}</span>
      </div>
     )
    })        
    

    【讨论】:

    • 如何访问place_N?这里的 item 表示对象键对吗?
    • item = 对象{ "place_1": { "name": "One", "distance": {}, "duration": {} } }
    • 我的 arrayOfObjects 有每个对象的键。我已经包括了我的数组的样子。请看一下。因此,我无法访问值
    • 例如:0:{距离:{…},持续时间:{…},状态:“OK”}
    • 0 是钥匙吗?
    猜你喜欢
    • 2021-08-05
    • 2017-05-14
    • 2021-02-14
    • 2020-01-27
    • 1970-01-01
    • 2020-07-17
    • 2023-03-29
    • 2014-04-12
    • 1970-01-01
    相关资源
    最近更新 更多