Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

201
Vistas
Adding " / ", "or", and "and" text after joining the arrays

I have an array of nested arrays and need to join them using the " / ", " or " and " and " text.

If within the same array then joined by " and ".

If sibling then joins by " or ".

Otherwise, join by " \ ".

Example 1:-

The output should be:

ARTH 332 / HIST 364 and HIST 365

[
  [
    [
      "ARTH 332"
    ]
  ]
  [
    [
      "HIST 364"
      "HIST 365"
    ]
  ]
]

Example 2:-

The output should be:

BUS 300 / BUS 310 / BUS 330 / MKT 300 / ACCT 301 / MGMT 362 / BUS 345 or BUS 340

[
  [
    [
      "BUS 300"
    ]
  ]
  [
    [
      "BUS 310"
    ]
  ]
  [
    [
      "BUS 330"
    ]
  ]
  [
    [
      "MKT 300"
    ]
  ]
  [
    [
      "ACCT 301"
    ]
  ]
  [
    [
      "MGMT 362"
    ]
  ]
  [
    [
      "BUS 345"
    ]
    [
      "BUS 340"
    ]
  ]
]
about 4 years ago · Santiago Gelvez
1 Respuestas
Responde la pregunta

0

Simply iterate over the array and differentiate between the three cases for and, or and nothing. Finally join the result on /. I have also added , to make the given data a valid array.

let y = [
  [
    ["ARTH 332"]
  ],
  [
    ["HIST 364", "HIST 365"]
  ]
]

let x = [
  [
    ["BUS 300"]
  ],
  [
    ["BUS 310"]
  ],
  [
    ["BUS 330"]
  ],
  [
    ["MKT 300"]
  ],
  [
    ["ACCT 301"]
  ],
  [
    ["MGMT 362"]
  ],
  [
    ["BUS 345"],
    ["BUS 340"]
  ]
];

let z = [
    [
    ["BUS 300"]
  ],
  [
    ["BUS 310"]
  ],
  [
    ["BUS 330"]
  ],
  [
    ["MKT 300"]
  ],
  [
    ["ACCT 301"]
  ],
  [
    ["MGMT 362"]
  ],
  [
    ["BUS 345"],
    ["BUS 340","BUS 340"]
  ]
]
    
function transform(a) {
  let length = a.length;
  let result = [];
  for (let i = 0; i < length; i++) {
    if (a[i].length > 1) {
      result.push(
        a[i].map(e=>e.join(" and ")).join(" or ")
      )
    } else if (a[i][0].length > 1) {
      result.push(a[i][0].join(" and "))
    } else {
      result.push(a[i][0]);
    }
  }
  return result.join(" / ");
}

console.log(transform(y));
console.log(transform(x));
console.log(transform(z));

about 4 years ago · Santiago Gelvez Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda