获取对象中与输入匹配的值 – JS

huangapple go评论57阅读模式
英文:

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)
                                       );

输出:

获取对象中与输入匹配的值 – JS

参考 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:

获取对象中与输入匹配的值 – JS

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 } ]

huangapple
  • 本文由 发表于 2023年5月11日 19:29:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/76227142.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定