用另一个数组创建对象数组

时间:2020-06-12 18:28:05

标签: javascript arrays angular typescript object

我有一个对象数组:

let data = [{
    createdDate: "2222"
    email: "test5@test.com"
    histories: []
    meta: {
      profilePic: "test"
    }
    name: {
      first: "Vikash",
      last: "Grv"
    }
    role: {
      id: "123",
      name: "test role 2"
    }
    specialities: []
    status: "active"
    __v: 0
    _id: "123"
  },
  {
    createdDate: "2222"
    email: "test5@test.com"
    histories: []
    meta: {
      profilePic: "test"
    }
    name: {
      first: "Vikash",
      last: "Grv"
    }
    role: {
      id: "123",
      name: "test role 2"
    }
    specialities: []
    status: "active"
    __v: 0
    _id: "123"
  },
  {
    createdDate: "2222"
    email: "test5@test.com"
    histories: []
    meta: {
      profilePic: "test"
    }
    name: {
      first: "Vikash",
      last: "Grv"
    }
    role: {
      id: "123",
      name: "test role 2"
    }
    specialities: []
    status: "active"
    __v: 0
    _id: "123"
  },
]

我想创建另一个对象数组,例如:

const data = [{
    teamMember: 'Vikas Grv',
    email: 'test5@test.com',
    role: 'test role 2',
    assignedOn: null
  }, {
    teamMember: 'Vikas Grv',
    email: 'test5@test.com',
    role: 'test role 2',
    assignedOn: null
  },
  {
    teamMember: 'Vikas Grv',
    email: 'test5@test.com',
    role: 'test role 2',
    assignedOn: null
  }
]

团队成员中的数据是姓名(第一个和最后一个键值)的污染,电子邮件是直接的,角色来自角色->名称键值且分配为空

以及有关操作方法的想法

2 个答案:

答案 0 :(得分:3)

您可以使用map

var data = [{createdDate: "2222",email: "test5@test.com",histories: [],meta: {profilePic: "test"},name: {first: "Vikash", last: "Grv"},role: {id: "123", name: "test role 2"},specialities: [],status: "active",__v: 0,_id: "123"}, {createdDate: "2222",email: "test5@test.com",histories: [],meta: {profilePic: "test"},name: {first: "Vikash", last: "Grv"},role: {id: "123", name: "test role 2"},specialities: [],status: "active",__v: 0,_id: "123"},{createdDate: "2222",email: "test5@test.com",histories: [],meta: {profilePic: "test"},name: {first: "Vikash", last: "Grv"},role: {id: "123", name: "test role 2"},specialities: [],status: "active",__v: 0,_id: "123"},];

var result = data.map(({name, role, email})=>({teamMember:Object.values(name).join(" "), email,role:role?.name, assignOn:null}));

console.log(result);

答案 1 :(得分:0)

您可以这样做:

import { Component } from '@angular/core';


@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {
  data = [
    {
      createdDate: "2222",
      email: "test5@test.com",
      histories: [],
      meta: { profilePic: "test" },
      name: { first: "Vikash", last: "Grv" },
      role: { id: "123", name: "test role 2" },
      specialities: [],
      status: "active",
      __v: 0,
      _id: "123"
    },
    {
      createdDate: "3333",
      email: "test3@test.com",
      histories: [],
      meta: { profilePic: "test" },
      name: { first: "jhon", last: "doe" },
      role: { id: "123", name: "test role 3" },
      specialities: [],
      status: "active",
      __v: 0,
      _id: "1234"
    },
    {
      createdDate: "4444",
      email: "test5@test.com",
      histories: [],
      meta: { profilePic: "test" },
      name: { first: "nathy", last: "gyr" },
      role: { id: "123", name: "test role 4" },
      specialities: [],
      status: "active",
      __v: 0,
      _id: "1234"
    },
  ]

  constructor() {

    let result = this.data.map((item) => {
      return {
        teamMember: item?.name?.first + item?.name?.last,
        email: item?.email,
        role: item?.role?.name,
        assignedOn: null
      }
    }

    );
    console.log(result)
  }

}