英文:
How to get the values in an object if it matches input - JS
问题
Here's the translated code snippet:
export const flightsData = [
{
id: 12,
start: '马尼拉 (MNL)',
destination: '曼谷 (DMK)',
price: 10436,
time: 210
},
{
id: 9,
start: '马尼拉 (MNL)',
destination: '首尔 (ICN)',
price: 3809,
time: 145,
connect: [
{
id: 77,
start: '马尼拉 (MNL) 飞往首尔 (ICN)',
destination: '曼谷 (DMK)',
price: 6284,
time: 250
},
{
id: 78,
start: '马尼拉 (MNL) 飞往首尔 (ICN) 飞往新加坡 (SG)',
destination: '曼谷 (DMK)',
price: 6284,
time: 250
}
]
}
];
const connectingFlights = flightsData.filter(flight =>
flight.start === from && flight.connect && flight.connect.destination === to
);
This is the translated code with the requested parts converted to Chinese.
英文:
export const flightsData = [ {
id: 12,
start: 'Manila (MNL)',
destination: "Bangkok (DMK)",
price: 10436,
time: 210
},
{
id: 9,
start:'Manila (MNL)',
destination: "Seoul (ICN)",
price: 3809,
time: 145,
connect: [ { id: 77,
start: 'Manila (MNL) to Seoul (ICN) ',
destination: 'Bangkok (DMK)',
price: 6284,
time: 250 },
{ id: 78,
start: 'Manila (MNL) to Seoul (ICN) to Singapore (SG)',
destination: 'Bangkok (DMK)',
price: 6284,
time: 250 } ]
},
]
This is a sample of an object that has a property that has another list of objects with it, now if the user set the destination from Manila to Bangkok it should show the direct flight MNL to DMK and the all the objects in the connect property
const connectingFlights = flightsData.filter(flight =>
flight.start === from && flight.connect && flight.connect.destination === to
);
The code above only works if the connect is not an array i dont know how to display it all any help is apprecited
答案1
得分: 1
以下是翻译好的部分:
关于您提供的数据和您想要的预期结果,
以下是实现相同目标的代码片段
let start = 'MNL';
let to = 'DMK';
const connectingFlights = flightsData.connect
.filter(flight =>
flightsData.start.includes(start) &&
flight.start.includes(start) &&
flight.destination.includes(to)
);
输出:
参考此 jsfiddle,我已为您创建。
英文:
With reference to the data you provided and expected result you want,
Here is the code snippet to achieve the same
let start = 'MNL'
let to = 'DMK'
const connectingFlights = flightsData.connect
.filter(flight =>
flightsData.start.includes(start) &&
flight.start.includes(start) &&
flight.destination.includes(to)
);
Output:
Refer this jsfiddle which I have created for you.
答案2
得分: 0
I used a slightly different approach from the answer given by @er-sho, I have my code and result below.
Code:
const connectingFlights = flightsData.connect.filter(
(flight) =>
flightsData.start.match("MNL") &&
flight.start.match("MNL") &&
flight.destination.match("DMK")
);
console.log(connectingFlights)
Result:
[ { id: 13,
start: 'Manila (MNL) to Seoul (ICN) ',
destination: 'Bangkok (DMK)',
price: 6284,
time: 250 },
{ id: 14,
start: 'Manila (MNL) to Seoul (ICN) to Singapore (SG)',
destination: 'Bangkok (DMK)',
price: 6284,
time: 250 } ]
英文:
I used a slightly different approach from the answer given by @er-sho
, I have my code and result below.
Code:
const connectingFlights = flightsData.connect.filter(
(flight) =>
flightsData.start.match("MNL") &&
flight.start.match("MNL") &&
flight.destination.match("DMK")
);
console.log(connectingFlights)
Result:
[ { id: 13,
start: 'Manila (MNL) to Seoul (ICN) ',
destination: 'Bangkok (DMK)',
price: 6284,
time: 250 },
{ id: 14,
start: 'Manila (MNL) to Seoul (ICN) to Singapore (SG)',
destination: 'Bangkok (DMK)',
price: 6284,
time: 250 } ]
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论