如何使用现有数组的元素创建新数组?

问题描述 投票:0回答:1

我在一个对象中包含许多键值对记录的JSON数组中,但是我只需要几个键记录。如何使用键创建新数组?

array = [
{
 airlineName: "Airline 1",
 hotelName: "Hotel 1 ", 
 airportId: "456",
 checkInDate: "17 SEP 1998",
 bookingStatus: "B"
},
{
airlineName: "Airline 2",
 hotelName: "Hotel 1", 
 airportId: "123",
 checkInDate: "7 AUG 1998",
 bookingStatus: "P"
 }
]

我想要这样的数组进行某些操作:

array = [
{
 airlineName: "Airline 1",
 hotelName: "Hotel 1 ", 
 bookingStatus: "B"
},
{
airlineName: "Airline 2",
 hotelName: "Hotel 1", 
 bookingStatus: "P"
 }
]
javascript angular
1个回答
0
投票

使用地图运算符:

const newArray = array.map(element => {
    element.airlineName, 
    element.hotelName, 
    element.bookingStatus
})


0
投票

尝试这样:

var result = [];
this.array.forEach(item => {
  result.push({
    airlineName: item.airlineName,
    hotelName: item.hotelName,
    bookingStatus: item.bookingStatus
  });
});

Working Demo

© www.soinside.com 2019 - 2024. All rights reserved.