Если вы хотите отфильтровать результат по определенной круизной линии, самым простым решением будет метод Array.prototype.filter
.
// Copied from seeds/ships.js
const ships = [
{
id: 1,
cruise_line: "Royal Caribbean",
ship_name: "Symphony of the Seas",
img: "/images/Symphone-of-the-Seas-heading-left-OU2.jpg",
Year: 2018,
Gross_Tonnage: 228081,
Passenger_Full_Capacity: 6680,
Double_Occupancy_Passenger_Capacity: 5518,
Length: 362.1,
Beam: 47.448,
Draft: 9.322,
Height: 72.5,
Loaded_Displacement: 120000,
Deadweight: 18095,
Review: ''
},
{
id: 5,
cruise_line: "Fred Olsen",
ship_name: "Boudicca",
img: "/imagwes/Boudicca_at_Funchal_2016_(bigger).jpg",
Year: 1973,
Gross_Tonnage: 28372,
Passenger_Full_Capacity: 900,
Double_Occupancy_Passenger_Capacity: 880,
Length: 206.96,
Beam: 25.22,
Draft: 7.55,
Height: 45,
Loaded_Displacement: 21156,
Deadweight: 5956,
Review: ''
},
{
id: 6,
cruise_line: "Fred Olsen",
ship_name: "Black Watch",
img: '',
Year: 1972,
Gross_Tonnage: 28613,
Passenger_Full_Capacity: 868,
Double_Occupancy_Passenger_Capacity: 804,
Length: 205.47,
Beam: 25.20,
Draft: 7.55,
Height: 45,
Loaded_Displacement: 20704,
Deadweight: 5656,
Review: ''
},
];
// "Royal Caribbean" only
const shipsOne = ships.filter(ship => ship.cruise_line === 'Royal Caribbean');
// "Fred Olsed" only
const shipsTwo = ships.filter(ship => ship.cruise_line === 'Fred Olsen');
console.log(shipsOne); // Ship with ID 1
console.log(shipsTwo); // Ships with ID 5, 6
Если вы хотите сгруппировать корабли по круизным линиям, ваш лучший выбор - метод Array.prototype.reduce
.
// Copied from seeds/ships.js
const ships = [
{
id: 1,
cruise_line: "Royal Caribbean",
ship_name: "Symphony of the Seas",
img: "/images/Symphone-of-the-Seas-heading-left-OU2.jpg",
Year: 2018,
Gross_Tonnage: 228081,
Passenger_Full_Capacity: 6680,
Double_Occupancy_Passenger_Capacity: 5518,
Length: 362.1,
Beam: 47.448,
Draft: 9.322,
Height: 72.5,
Loaded_Displacement: 120000,
Deadweight: 18095,
Review: ''
},
{
id: 5,
cruise_line: "Fred Olsen",
ship_name: "Boudicca",
img: "/imagwes/Boudicca_at_Funchal_2016_(bigger).jpg",
Year: 1973,
Gross_Tonnage: 28372,
Passenger_Full_Capacity: 900,
Double_Occupancy_Passenger_Capacity: 880,
Length: 206.96,
Beam: 25.22,
Draft: 7.55,
Height: 45,
Loaded_Displacement: 21156,
Deadweight: 5956,
Review: ''
},
{
id: 6,
cruise_line: "Fred Olsen",
ship_name: "Black Watch",
img: '',
Year: 1972,
Gross_Tonnage: 28613,
Passenger_Full_Capacity: 868,
Double_Occupancy_Passenger_Capacity: 804,
Length: 205.47,
Beam: 25.20,
Draft: 7.55,
Height: 45,
Loaded_Displacement: 20704,
Deadweight: 5656,
Review: ''
},
];
const groupedShips = ships.reduce((acc, cur) => {
const currentCruiseLine = cur.cruise_line;
if (acc[currentCruiseLine]) {
return {
...acc,
[currentCruiseLine]: [...acc[currentCruiseLine], cur],
};
}
return {
...acc,
[currentCruiseLine]: [cur],
};
}, {});
console.log(groupedShips);