简体   繁体   English

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

[英]Create array of objects with another array

I have an array of object:我有一个 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"
  },
]

I want to create another array of an object like:我想创建另一个 object 数组,例如:

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

the data in team member is the contamination of name (first and last key value), email is direct, role is from role --> name key value and assigned is null团队成员中的数据是名称的污染(第一个和最后一个键值),email 是直接的,角色是来自角色-> 名称键值并分配是 null

and idea on how to do this以及如何做到这一点的想法

You can make use of map :您可以使用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);

You can do this:你可以这样做:

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

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM