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

128
Views
¿Cómo aplicar la combinación solo para una sección particular de una matriz?

Obtengo una matriz y la estrujo en esta variable de matriz:

 let arrayStr = oldArr.join(', ');

Mi matriz se ve así en la consola:

 (a), text1, (b), text2, (c), text3, (d), text3

Lo que quiero es que quede así:

 (a) - text1, (b) - text2, (c) - text3, (d) - text3

Sé que agregué ', ' después de todas las cadenas, pero ¿cómo puedo aplicarlo como arriba?

Gracias.

about 4 years ago · Santiago Trujillo
3 answers
Answer question

0

Puede lograr esto recorriendo la matriz y accediendo al elemento actual y también al siguiente por su índice y luego agregando ambos al mismo elemento de una nueva matriz, algo como esto:

 var oldArr = ['(a)', 'text1', '(b)', 'text2', '(c)', 'text3', '(d)', 'text3']; var newArr = []; for (var i = 0; i < oldArr.length; i++) { newArr.push(oldArr[i] + ' - ' + oldArr[++i]); } console.log(newArr.join(', '));

about 4 years ago · Santiago Trujillo Report

0

Podría usar una solución de tres pasos, primero obtenga todos los elementos que están conectados en una sola matriz secundaria, asigne las matrices secundarias unidas y una la matriz externa.

 var array = ['(a)', 'text1', '(b)', 'text2', '(c)', 'text3', '(d)', 'text3'], text = array .reduce(function (r, a, i) { i % 2 ? r[r.length - 1].push(a) : r.push([a]) ; return r; }, []) .map(function (a) { return a.join(' - '); }) .join(', '); console.log(text);

about 4 years ago · Santiago Trujillo Report

0

Puede recorrer todos los elementos de la matriz y utilizar el operador % (módulo) para combinar los valores que necesite:

 var oldArr = ['(a)', 'text1', '(b)', 'text2', '(c)', 'text3', '(d)', 'text4']; var arrayStr = []; for (var i = 0; i < oldArr.length; i++) { if (i % 2 != 0) { oldArr[i] = oldArr[i - 1] + ' - ' + oldArr[i] ; arrayStr.push(oldArr[i]); } } console.log(arrayStr.join(', '));

about 4 years ago · Santiago Trujillo 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!