Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

273
Views
string remover of the last value of an array
 let produtos = [...Produtos, data]
           produtos= produtos.sort((a,b) => a.numero_volume - b.numero_volume)
              setFiltro('')
              var volume = data.volumes

              var list = [];

              for (var i = 1; i <= volume; i++) {
                
                const p = produtos.find(p => p.numero_volume == i)
            
                if(!p) {
                
                    list.push(`${i},`);
                  
                }
                setLista(list) 
              }
              setProdutos(produtos)

What returns me from this is

enter image description here

I need to remove the comma from the last string, remembering that the final number can be another, but it will always be the last number in ascending order

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

write the comma before the number

let produtos = [...Produtos, data]
           produtos= produtos.sort((a,b) => a.numero_volume - b.numero_volume)
              setFiltro('')
              var volume = data.volumes

              var list = [];

              for (var i = 1; i <= volume; i++) {
                
                const p = produtos.find(p => p.numero_volume == i)
            
                if(!p) {
                
                   if(list.length === 0) list.push(`${i}`);
                   else list.push(`,${i}`)
                  
                }
                setLista(list) 
              }
              setProdutos(produtos)
about 4 years ago · Juan Pablo Isaza Report

0

Remove the last comma:

setProdutos(produtos.replace(/,(?=\s*$)/, ''))
about 4 years ago · Juan Pablo Isaza Report

0

Para lo que vale, creo es mejor solo acumular las entradas q faltan en una lista y indica la lista con sintaxis en traduccion al tipo string, como dijo pilchard.

let produtos = [...Produtos, data]
  .sort((a,b) => a.numero_volume - b.numero_volume)
const list = produtos
  .filter(p => Number(p.numero_volume) > 0 && Number(p.numero_volume) <= data.volumes)
const comatador = (p,i) => `${p.numero_volume}${i < list.length - 1 ? ',': ''}`

setFiltro('')
setLista(list.map(comatador))
setProdutos(produtos)
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!