H.Husain
H.Husain

Reputation: 463

Alphabetically order with header in reactjs

I have a list of items fetched through API which is sorted in alphabetical order, but I want to add header above the list items. For example like below HTML Structure

<h4> A</h4 >
<ul>
    <li>Apple</li>
    <li>Ant</li>
</ul>
<h4>B</h4>
<ul>
    <li>Babel</li>
    <li>Ball</li>
</ul>

So far I have mapped the list of items like below in function:

filterItems = (itemList) => {
    result = result.map((item, index) => (
        <li className="brand-item" key={index}><a href="#">
            <img onError={this.nobrandimage} src={item.thumbnail} className="img-responsive" /></a>
        </li>
    ))
    return result;
}

Below is my render function:

render(){
    //code to get the letterkey, I am not sure how to put this in map function to render**
    var letters = '', groups = {};
    for (var i = 0, len; i < len; i++) {
        var letterKey = brands[i].name.charAt(0).toLowerCase(); // get the first letter
        if (letters.indexOf(letterKey) === -1) {
            letters += letterKey;
            groups[letterKey] = [brands[i]];
        } else {
            groups[letterKey].push([brands[i]]);
        }
    };

    console.log(letters);

    let brands = this.props.brands.all_brands
    if (brands) brands.sort(this.dynamicSort("name"));
    if (brands) len = brands.length
    const filteredList = this.filterItems(brands, letters);
    return (
        <ul>
            {filteredList}
        </ul>
    );
}

Note: I need help to map the first letter to my list in heading tag.

Sample App Url to have a better view: https://stackblitz.com/edit/react-pfzlvo

Upvotes: 0

Views: 1295

Answers (3)

Jem
Jem

Reputation: 1

Object.keys(groups).map(letter, key => (   

    <div key={key}>
        <h4>{letter}</h4>
        <ul>
            {
                groups[letter].map((word, index) => (
                    <li key={index}>{word}</li>
                ))
            }
        </ul>
    </div>


))

Upvotes: 0

Drew Reese
Drew Reese

Reputation: 202618

Preprocess your brands into a map {letterKey: [brands]} to use in render function.

const groups = brands.reduce((groups, brand) => {
  const letterKey = brand.name.charAt(0).toLowerCase();
  (groups[letterKey] || (groups[letterKey] = [])).push(brand);
  return groups;
}, {});

Map the entries of [key, brands] to your unordered list

Object.entries(groups).sort().map(([letterKey, brands]) => (
  <div key={letterKey}>
    <h4>{letterKey}</h4>
    <ul>
      { brands.map(brand => <li key={brand}>{brand}</li>) }
    </ul>
</div>
));

const brands = ['cca', 'ccb', 'ccc', 'bba', 'bbb', 'bbc', 'aaa', 'aab', 'aac'];

const groups = brands.reduce((groups, brand) => {
  const letterKey = brand.charAt(0).toLowerCase();
  (groups[letterKey] || (groups[letterKey] = [])).push(brand);
  return groups;
}, {});


Object.entries(groups).sort().map(([letterKey, brands]) => {
  console.log('KEY', letterKey);
  brands.map(brand => console.log('\tbrand', brand));
});

Upvotes: 3

aseferov
aseferov

Reputation: 6393

After you grouped brands groups = {a: [apple, Ant], b: [Babel, Ball], ...} you can iterate groups like

Object.keys(groups).sort().map(letter, key => (   

    <div key={key}>
        <h4>{letter}</h4>
        <ul>
            {
                groups[letter].map((word, index) => (
                    <li key={index}>{word}</li>
                ))
            }
        </ul>
    </div>


))

Upvotes: 0

Related Questions