Hola, tengo la siguiente estructura para la interfaz de usuario:
export interface IUser { EMPLOYEE_NAME :string, EMPLOYEE_PID : string }En un punto de un código, recibo una matriz de IUser - IUser[] con varios nombres de empleados y sus pid.
P.ej.
{EMPLOYEE_NAME:'XYZ',EMPLOYEE_PID :'A123'}, {EMPLOYEE_NAME:'ABC',EMPLOYEE_PID :'B123'},Quiero obtener PID separados por comas: 'A123', 'B123'
Probé con map y foreach pero no pude hacer un bucle correctamente como su interfaz.
Puede usar Array.prototype.map y Array.prototype.join para lograrlo.
let data = [{EMPLOYEE_NAME:'XYZ',EMPLOYEE_PID :'A123'}, {EMPLOYEE_NAME:'ABC',EMPLOYEE_PID :'B123'}]; let result = data.map(x => x.EMPLOYEE_PID).join(','); console.log(result);Si desea envolver las identificaciones en coma
let data = [ {EMPLOYEE_NAME:'XYZ',EMPLOYEE_PID :'A123'}, {EMPLOYEE_NAME:'ABC',EMPLOYEE_PID :'B123'} ]; let result = data.map(x => `'${x.EMPLOYEE_PID}'`).join(','); console.log(result);También puede usar Array.prototype.map.call para lograrlo.
const data = [ {EMPLOYEE_NAME:'XYZ',EMPLOYEE_PID :'A123'}, {EMPLOYEE_NAME:'ABC',EMPLOYEE_PID :'B123'} ]; Array.prototype.map.call(data, function(item) { return item['EMPLOYEE_PID']; } ).join(",");Salida - 'A123, B123'