最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - how to use map in react to create multiple component? - Stack Overflow

matteradmin7PV0评论

I have this array that has this structure please check the code down below , my end results should look like the following :

  • veg
  • apple
  • carrot

  • meat
  • chicken
  • steak

my current results are

apple carrot chicken steak

since I cannot structure the array other wise and don't want to go in to deep nesting or even nest loops which I doubt it will work in react any idea how to achieve the previous results using map , where I map through group only once to create the group name and to then add the items related to that group inside that group?, food for thought : could conditional rendering be also leveraged here ? I was able to only get either the group multiple times or the items only..

const arr = {
  itmes: [
    { id: 1, group: "veg", item: "apple" },
    { id: 2, group: "veg", item: "carrot" },
    { id: 3, group: "meat", item: "chicken" },
    { id: 4, group: "meat", item: "steak" }
  ]
};

function App() {
  return (
    <div>
      {arr["itmes"].map(
        (item) => item.group
        //item.item
      )}
    </div>
  );
}

I have this array that has this structure please check the code down below , my end results should look like the following :

  • veg
  • apple
  • carrot

  • meat
  • chicken
  • steak

my current results are

apple carrot chicken steak

since I cannot structure the array other wise and don't want to go in to deep nesting or even nest loops which I doubt it will work in react any idea how to achieve the previous results using map , where I map through group only once to create the group name and to then add the items related to that group inside that group?, food for thought : could conditional rendering be also leveraged here ? I was able to only get either the group multiple times or the items only..

const arr = {
  itmes: [
    { id: 1, group: "veg", item: "apple" },
    { id: 2, group: "veg", item: "carrot" },
    { id: 3, group: "meat", item: "chicken" },
    { id: 4, group: "meat", item: "steak" }
  ]
};

function App() {
  return (
    <div>
      {arr["itmes"].map(
        (item) => item.group
        //item.item
      )}
    </div>
  );
}

Codesanadbox

Share Improve this question edited Sep 24, 2021 at 16:24 Richardson asked Sep 24, 2021 at 16:13 RichardsonRichardson 2,3121 gold badge24 silver badges62 bronze badges 2
  • 1 Just do (item) => i<li>{tem.group}</li> See Rendering multiple ponents for explanation – Shivam Jha Commented Sep 24, 2021 at 16:18
  • still this doesn't solve the issue of the array structure that I have. – Richardson Commented Sep 24, 2021 at 16:23
Add a ment  | 

3 Answers 3

Reset to default 3

Im very new to javascript, but like hgb123's answer, something like this inside a render or return block:

<div className="myClass">
  {myList.map((eachItemInList) => (
    <ReactComponent certainProp={eachItemInList} />
   ))}
</div>

works perfectly for a list like this one:

const myList = ['item1', 'item2', 'item3', 'item4', 'item5', 'item6']

hope this helped someone!

You should wrap the items first and render the grouped ones

const groupItems = items =>
  items.reduce((groupedItems, item) => {
    if (!groupedItems[item.group]) {
      groupedItems[item.group] = []
    }

    groupedItems[item.group].push(item)

    return groupedItems
  }, {})

const items = Object.entries(groupItems(arr.items)).map(
  ([groupName, items]) => (
    <React.Fragment>
      <li>{groupName}</li>
      {items.map(item => (
        <li>{item.item}</li>
      ))}
    </React.Fragment>
  )
)

Option 1

First, make sure your array is sorted by Group:

const sorted = arr["itmes"]
    .sort((a, b) => (a.group || '').localeCompare(b.group));

Then you can render and conditionally add another heading element whenever the group name changes:

<ul>
    {data.map((d, id) => (
        <>
            ((id > 0 || d.group !== data[id - 1].group) ? <li key={`${id}-h`}><b>{d.group}</b></li> : undefined)
            <li key={`${id}-v`}>{d.item}</li>
        </>
    ))}
</ul>

Extra: Custom group sorting

If you need to custom sort the array according to another array:

const sortLtu = ['veg', 'apple', 'meat'];
data.sort((a, b) => sortLtu.indexOf(a.group) - sortLtu.indexOf(b.group));

Option 2: Util function

If you end u doing this often you may create a util function:

Array.prototype.groupBy = function(cb) {
    const groups = [];
    this.forEach((d, id, arr) => {
        const g = cb(d, id, arr);
        let group = groups.find(_g => _g.group === g);
        if (!group) {
            group = { group: g, items: [] };
            groups.push(group);
        }
        group.items.push(d);
    })
    return groups;
}

And then use it like

{data.groupBy(i => i.group).map((bundle, ix) => (
    <div key={ix}>
        <b>{bundle.group}</b>
        <ul>
            {bundle.items.map((item, ix) => <li key={ix}>{item.item}</li>)}
        </ul>
    </div>
))}
Post a comment

comment list (0)

  1. No comments so far