Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

125
Visualizações
Truncated the whole text instead of just letters

What I'm trying to achieve. Image sample here

Hi Guys I'm still learning and trying to truncate a long text but with few conditions. Let's say I have an array of 5 words, if the text is too long, it will need to truncate the text to accommodate the width of the container. But I wanted it to cut the whole word instead of just the letters. As example object of [one, two, three, four, five]

One, two, three, ... 2 (counter) I want this

One, two, three, fou ... 1 (counter) I don't want this

Below is what I have tried but I'm not sure where to go next also to make the counter dynamic based on truncated text number instead of an array length.

function App() {
  const numbers = ["one", "two", "three", "four", "five"];

  const testStyle = {
    display: "flex",
    justifyContent: "space-between",
    background: "#D0D0D0",
    padding: "6px 8px",
    width: "150px",
    whiteSpace: "nowrap",
  };

  const truncatedStyle = {
    textOverflow: "ellipsis",
    overflow: "hidden",
  };

  return (
    <div className="App">
      <div style={testStyle}>
        <div style={truncatedStyle}>
          {numbers.map((n, i) => (
            <span>
              {n}
              {i === 4 ? "" : ", "}
            </span>
          ))}
        </div>
        <div
          style={{ background: "black", color: "white", padding: "2px 4px" }}
        >
          {numbers.length}
        </div>
      </div>
    </div>
  );
}
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

One thing you can do is you can use useRef and get those spans width, then check it in a variable if it is greater than 150px. if no the add the span, if yes then add , ... then add the span width to the variable.

about 4 years ago · Juan Pablo Isaza Relatório

0

You can modify **function App()** using Vanilla Javascript you can achieve the same result in the form of String and then through String Interpolation you can render it on the HTML as going by Best Coding Practices all the logic part must be concluded in JS and HTML should be used only for view/rendering the data.

Example -

function App() {
  var s = "";
  var c = 0;
  const numbers = ["one", "two", "three", "four", "five"];
  for(var i =0 ; i< numbers.length;i++){
    i>2 ? c++ : s = s+numbers[i]+", ";
  }
  return numbers.length > 3 ? s+".." + c : s;
}
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda