0

Hi have below structure for user interface -

export interface IUser {
  EMPLOYEE_NAME :string,
  EMPLOYEE_PID : string
}

At a point in a code , I am receiving array of IUser - IUser[] with multiple employeenames and their pids.

Eg.

{EMPLOYEE_NAME:'XYZ',EMPLOYEE_PID :'A123'},
{EMPLOYEE_NAME:'ABC',EMPLOYEE_PID :'B123'},

I want to fetch comma seperated PIDs - 'A123','B123'

I tried with map and foreach but not able to loop properly as its interface.

1
  • "I tried with" - can you include your attempt in a snippet? See minimal reproducible example. Commented Oct 26, 2021 at 11:06

2 Answers 2

2

You can use Array.prototype.map and Array.prototype.join to achieve.

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);

If you want to wrap the ids in comma

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);

Sign up to request clarification or add additional context in comments.

Comments

1

You can also use Array.prototype.map.call to achieve.

    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(",");

Output - 'A123,B123'

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.