简体   繁体   English

如何使用 object 中的键过滤 arrya 并创建新的 object

[英]How to filter arrya with keys in object and create new object

How can I filter the rooms array with elements in the object and if it matches to object id in the array of objects with item type "Room" add count key with count to the main array?如何使用 object 中的元素过滤房间数组,如果它与项目类型为“Room”的对象数组中的 object id 匹配,则向主数组添加计数键?

The First Array that I get from API我从 API 得到的第一个数组

[ 
  {
    rooms: [ 'f03e6e40-870d-11eb-88a2-0149a30c02c5' ],
    itemType: 'Item',
    id: '8c6a6070-80c0-11eb-9753-bb149505e85f',
    itemName: 'Item 1’,
  },
  {
    rooms: [ 'eef60fc0-870d-11eb-88a2-0149a30c02c5' ],
    itemType: 'Item',
    id: '3aede320-870f-11eb-9e58-9d6fb0d8b5a8',
    itemName: 'Item 2'
  },
  {
    rooms: [ 'f03e6e40-870d-11eb-88a2-0149a30c02c5' ],
    itemType: 'Item',
    id: '3ad52b00-870f-11eb-8f84-0782ebd81006',
    itemName: ‘Item3’ 
  },
  { 
    itemType: 'Room',
    id: 'f03e6e40-870d-11eb-88a2-0149a30c02c5',
    itemName: 'Bedroom’
  },
  { 
    itemType: 'Room',
    id: 'eef60fc0-870d-11eb-88a2-0149a30c02c5',
    itemName: 'Bedroom 2' 
  },
 ]

I want to filter the rooms array with elements in the object and if it matches to object id in the array of objects with itemtType "Room" add count key with count to main array as below.我想用 object 中的元素过滤房间数组,如果它与 itemtType "Room" 的对象数组中的 object id 匹配,则向主数组添加计数键,如下所示。

[ 
{
    rooms: [ 'f03e6e40-870d-11eb-88a2-0149a30c02c5' ],
    itemType: 'Item',
    id: '8c6a6070-80c0-11eb-9753-bb149505e85f',
    itemName: 'Item 1’,
},
  {
    rooms: [ 'eef60fc0-870d-11eb-88a2-0149a30c02c5' ],
    itemType: 'Item',
    id: '3aede320-870f-11eb-9e58-9d6fb0d8b5a8',
    itemName: 'Item 2'
 },
  {
    rooms: [ 'f03e6e40-870d-11eb-88a2-0149a30c02c5' ],
    itemType: 'Item',
    id: '3ad52b00-870f-11eb-8f84-0782ebd81006',
    itemName: ‘Item3’ 
},
  { 
    itemType: 'Room',
    id: 'f03e6e40-870d-11eb-88a2-0149a30c02c5',
    itemName: 'Bedroom’,
    count: 2
 },
  { 
    itemType: 'Room',
    id: 'eef60fc0-870d-11eb-88a2-0149a30c02c5',
    itemName: 'Bedroom 2',
    count: 1
},
 ]

You can use Array.prototype.map to transform the input array and add the count property to the elements with an itemType of 'Room' .您可以使用Array.prototype.map转换输入数组并将count属性添加到itemType'Room'的元素。 You can use Array.prototype.filter and Array.prototype.includes to count the number of rooms.您可以使用Array.prototype.filterArray.prototype.includes来计算房间的数量。

 const arr = [ { rooms: ['f03e6e40-870d-11eb-88a2-0149a30c02c5'], itemType: 'Item', id: '8c6a6070-80c0-11eb-9753-bb149505e85f', itemName: 'Item 1', }, { rooms: ['eef60fc0-870d-11eb-88a2-0149a30c02c5'], itemType: 'Item', id: '3aede320-870f-11eb-9e58-9d6fb0d8b5a8', itemName: 'Item 2', }, { rooms: ['f03e6e40-870d-11eb-88a2-0149a30c02c5'], itemType: 'Item', id: '3ad52b00-870f-11eb-8f84-0782ebd81006', itemName: 'Item3', }, { itemType: 'Room', id: 'f03e6e40-870d-11eb-88a2-0149a30c02c5', itemName: 'Bedroom', }, { itemType: 'Room', id: 'eef60fc0-870d-11eb-88a2-0149a30c02c5', itemName: 'Bedroom 2', }, ] const result = arr.map((element) => { if (element.itemType.== 'Room') { return element } return {..,element: count. arr.filter( (i) => i.itemType === 'Item' && i.rooms.includes(element.id) ),length. } }) console.log(result)

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

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