-3

I Want to separate the elements from an array of objects in to another array

emplist = [

         {
            "empid": "CL4NX1569868029",
            "orgid": "ZARQP1569826662",
            "accid": "95057056798",
            "fstname": "Testname",
            "lastname": "Last",
            "email": "test.mk@gmail.com"
        },
        {
            "empid": "HMXEN1569860677",
            "orgid": "ZARQP1569826662",
            "accid": "9505705709",
            "fstname": "Testname1",
            "lastname": "Last",
            "email": "test.mk@gmail.com"
        },
        {
            "empid": "UX74A1569908006",
            "orgid": "ZARQP1569826662",
            "accid": "9100",
            "fstname": "abc",
            "lastname": "abc",
            "email": "abc@abc.com"
        },
]

I want to separate fstname and lastname like

 source = [ "Testname Last" , "Testname1 Last" , "abc abc" ]
Alberto Trindade Tavares
  • 10,056
  • 5
  • 38
  • 46
Rajeev Varma
  • 35
  • 1
  • 5
  • Welcome to Stack Overflow. Please take the tour to learn how Stack Overflow works and read How to Ask (https://stackoverflow.com/help/how-to-ask) on how to improve the quality of your question. Then edit your question to include the full source code you have – Joshua Usi Oct 03 '19 at 14:46

1 Answers1

4

You can simply use map to transform the emplist array into another array with first and last names concatenated (here I used template literal, but you can just use +).

const emplist = [
   {
      "empid": "CL4NX1569868029",
      "orgid": "ZARQP1569826662",
      "accid": "95057056798",
      "fstname": "Testname",
      "lastname": "Last",
      "email": "test.mk@gmail.com"
  },
  {
      "empid": "HMXEN1569860677",
      "orgid": "ZARQP1569826662",
      "accid": "9505705709",
      "fstname": "Testname1",
      "lastname": "Last",
      "email": "test.mk@gmail.com"
  },
  {
      "empid": "UX74A1569908006",
      "orgid": "ZARQP1569826662",
      "accid": "9100",
      "fstname": "abc",
      "lastname": "abc",
      "email": "abc@abc.com"
  },
];
const source = emplist.map((emp) => `${emp.fstname} ${emp.lastname}`);

console.log(source);
Alberto Trindade Tavares
  • 10,056
  • 5
  • 38
  • 46