简体   繁体   English

如何在 typescript 中将一些属性从一个数组添加到另一个数组

[英]How to add some properties from one array to another in typescript

I want to loop through and add properties from an array to another array: Array1:我想循环并将属性从一个数组添加到另一个数组:Array1:

const users = [
   {
      "id":"112",
      "firstName":"a",
      "lastName":"b",
      "address":[
         {
            "apartment":"1",
            "street":"north"
         }
      ]
   },
   {
      "id":"113",
      "firstName":"e",
      "lastName":"f",
      "address":[
         {
            "apartment":"2",
            "street":"north"
         }
      ]
   },
   {
      "id":"114",
      "firstName":"i",
      "lastName":"j",
      "address":[
         {
            "apartment":"3",
            "street":"south"
         }
      ]
   },
   {
      "id":"1151",
      "firstName":"o",
      "lastName":"p",
      "address":[
         {
            "apartment":"4",
            "street":"west"
         }
      ]
   }
]

So, I have an empty array const usersInfo = [] I want to add only id and address in usersInfo from users array所以,我有一个空数组const usersInfo = []我只想在users数组中的usersInfo中添加idaddress

What I am trying to do below:我在下面尝试做的事情:

    const result = this.usersInfo.map(item => {
  users.forEach((element) => {
    item.id = element.id,
    item.address = element.address.map(r => {
      return {
        apartment: r.apartment,
        street: r.street
      };
    }),
  });

});

But this returns an Empty array as a result.但这会返回一个空数组作为结果。

I need output like below我需要 output 如下所示

const usersInfo = [
    {
      id: '112',
      address:[{
        apartment:'1',
        street: 'north'
      }]
},
      {
      id: '113',
      address:[{
        apartment:'2',
        street: 'north'
     }]
    },
    {
      id: '114',
      address:[{
        apartment:'3',
        street: 'south'
      }]
    },
    {
      id: '1151',
      address:[{
        apartment:'4',
        street: 'west'
      }]
   }
]

You can do something like below:您可以执行以下操作:

const usersInfo = users.map(user => ({id: user.id, address: user.address}))

暂无
暂无

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

相关问题 将属性从一个对象数组添加到另一个对象 - Add properties from one array of objects to another 如何将属性从一个 object 复制到另一个 typescript - How to copy properties from one object to another in typescript 如何在不覆盖Javascript的情况下将一个对象的属性添加到另一个对象中 - How to add properties from one object into another without overwriting in Javascript 将对象属性从一个数组映射到另一个数组 - Mapping object properties from one array to another 尝试将属性从一个 object 分配给另一个时出现 TypeScript 错误 - TypeScript errors while trying to assign properties from one object to another 如何构造一个新的 object 数组,其中包含 JavaScript 中另一个对象数组中的一些已删除属性 - How do I construct a new array of object with some deleted properties from another array of objects in JavaScript 如何将值从一个数组添加到另一个数组 - How to add value from one array to another array 如何将对象数组中的某些属性合并到另一个数组中? - How to merge some properties in array of objects into another array? 如何在 typescript 中按照一个数组的顺序构建一个数组并从另一个数组排序? - How to build an array in the order from one array and rank from another in typescript? 角度插值滞后呈现一些值以及如何从打字稿对象数组中删除一项 - Angular interpolation lags rendering some values & how to remove one item from the array of typescript objects
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM