访问对象数组中的元素

时间:2019-10-03 14:37:12

标签: javascript arrays

我想将对象数组中的元素分离到另一个数组中

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"
        },
]

我想将fstname和lastname分开

 source = [ "Testname Last" , "Testname1 Last" , "abc abc" ]

1 个答案:

答案 0 :(得分:3)

您可以简单地使用mapemplist数组转换为具有姓和名串联在一起的另一个数组(这里我使用了template literal,但是您可以只使用+ )。

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