0

I'm tying to display the first letter of every array element only once along with the main element title. I have listed the example below where you can see the list along with it's respected first letter. I want the list to appear as it is, but don't want to get their first letter repeated. https://jsfiddle.net/z42u1d9o/

My code:

var cars = ["Brown", "Blue", "Bingo", "Vietnam", "America", "India", "Volvo", "Saab", "Ford", "Fiat", "Audi"];
var text = "";
cars = cars.sort();
return cars.map(item => {
    var firstLetter = item.split('')[0];
  text += item + '-' +firstLetter + "<br>";
  document.getElementById("demo").innerHTML = text;
})
6
  • 1
    Please could you mention, what problem are you facing? what is expected output? Commented Oct 29, 2019 at 7:01
  • It's nice, but your text does not include a problem, nor an actual question. I could now start guessing what the problem is, but why should i? Do you want each first letter to only appear once, as in, e.g. no multiple "A"? Commented Oct 29, 2019 at 7:01
  • I have asked in the very first sentence that I want to display the first letter only once, so how to avoid displaying it every time Commented Oct 29, 2019 at 7:26
  • 1
    So you want it to be ["Brown-B", "Blue", "Bingo"] for the first three items? Commented Oct 29, 2019 at 8:42
  • 1
    Yes correct @Ric Commented Oct 29, 2019 at 8:43

4 Answers 4

1

I would use an object to store every added letter and to check against while looping through the array.

var cars = ["Brown", "Blue", "Bingo", "Vietnam", "America", "India", "Volvo", "Saab", "Ford", "Fiat", "Audi"],
    text = "",
    alreadyAdded = {},
    firstLetter = "";
  
  cars.sort();

  for (car of cars) {
     firstLetter = car[0];
     
     if (alreadyAdded[firstLetter]) {
        text += car;
     } else {
        alreadyAdded[firstLetter] = true;
        text += car + "-" + firstLetter;
     }

     text += " ";
  }


console.log(text);

Sign up to request clarification or add additional context in comments.

Comments

1

You can try following approach -

let result = ["Brown", "Blue", "Bingo", "Vietnam", "America", "India", "Volvo", "Saab", "Ford", "Fiat", "Audi"].sort().map((d, i, a) => (i == 0 || d[0] != a[i-1][0]) ? d + '-' + d[0] : d)


console.log(result)

1 Comment

Ah! Without Output it becomes difficult what is wanted. Done the edit @ShoebMirza. Does it look good now?
0

You would probably be able to use filter with a filter function.

The code would become something like this

var cars = ["Brown", "Blue", "Bingo", "Vietnam", "America", "India", "Volvo", "Saab", "Ford", "Fiat", "Audi"];
var text = "";
cars = cars.sort();
var firstLetters = cars.map(item=>item.split('')[0]).filter((x,y,z)=>z.indexOf(x) === y);

firstLetters.map(item => {
	text += item + "<br>";
  document.getElementById("demo").innerHTML = text;
})
<html>
<body>
<div id="demo">

</div>
</body>
</html>

1 Comment

I want to have the list display as it is and besides that I want it's first letter but if it has repeated before it should not get repeat again
0

I assume you want to have a List in the end, with the Letters as section headers. So I'd suggest using an object approach.

function getList(){
  let cars = ["Brown", "Blue", "Bingo", "Vietnam", "America", "India", "Volvo", "Saab", "Ford", "Fiat", "Audi"];
  let list = {};
  cars.sort();
  cars.forEach((car) => {
    let section = car.charAt(0);
    if (list[section] == null){
      list[section] = [];
    }
    list[section].push(car);
  });
  return list;
}

This will create a simple object with the section headers and an array.

{ A: [ 'America', 'Audi' ],
  B: [ 'Bingo', 'Blue', 'Brown' ],
  F: [ 'Fiat', 'Ford' ],
  I: [ 'India' ],
  S: [ 'Saab' ],
  V: [ 'Vietnam', 'Volvo' ] }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.