-1

So here's an example of some code, using an array of objects, for simple arbitrary data.

const function at the bottom to find my flight number represented as flightNu

var flights = [{
        "id": 1234,
        "airline": "Delta",
        "depart": "11/23/2016 10:25 AM",
        "arrive": "11/23/2016 11:35 AM",
        "from": "TPA",
        "to": "MEM"
    }, {
        "id": 4321,
        "airline": "AA",
        "depart": "11/23/2016 12:25 PM",
        "arrive": "11/23/2016 2:35 PM",
        "from": "DWF",
        "to": "SEA"
    }]

var flightNu = 4321; 

const flightDetails = (a) => 
a.map(f => (f.id == flightNu) ? f : null )


console.log(flightDetails(flights))

So my question lies with properly defining this const function

const flightDetails = (a) => 
a.map(f => (f.id == flightNu) ? f : null )

This is returning [null, Object] Object is the correct one, with flightNu. Found it, cool, but let's say I add 100 objects, that'd be 99 nulls returned. That's 99 problems but a Matching Flight Number ain't one. (sorry)

Can someone shed some light on a good way to use this? Maybe I shouldn't be using ternary operator. If I could return the proper f[index] that'd be great.

Preferably without assigning Object to another variable

var MatchingFlight;

const flightDetails = (a) => 
 a.map(f => {
     if (f.id === flightNu){
              MatchingFlight = f;
     }
 }) ? MatchingFlight : null   //returns Object {}  close but meh

This snippet works in the proposed goal sense, but defining MatchingFlight outside of scope doesn't seem right for const.

Community
  • 1
  • 1
jtrdev
  • 915
  • 1
  • 9
  • 25
  • 2
    If you want the first match, use `.find()` instead of `.map()`. –  Nov 28 '16 at 18:48
  • 4
    instead of `map` use `filter`. – abhishekkannojia Nov 28 '16 at 18:49
  • @torazaburo well i added the update for one scenario that works, There's other snippets that work, but the true answer is SLaks with 3 possible methods, which isn't a snippet, so I just wanted to update with a possible snippet for completeness – jtrdev Nov 28 '16 at 19:31

2 Answers2

4

map() transforms every element to something else. That's not what you want.

You want filter() (which returns only matching elements) or find() (which returns the first match), not map().

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
  • `find()` would be good in certain cases, but since `filter()` returns an array and let's say mutliple flight numbers are found for arguments sake, then that works pretty well in this case. – jtrdev Nov 28 '16 at 19:00
0

You can use filter in JavaScript which returns the elements that match the filter something like this

const flightDetails = (a) => 
a.filter(f => (f.id == flightNu))
Ajaykumar
  • 416
  • 3
  • 16
  • I played around with filter and came back with same thing pretty much except I didn't use ternary operator this time. Should I or does it matter? – jtrdev Nov 28 '16 at 18:59
  • It depends, sometimes if object is undefined then we can use ternary operator because if it fails we are assigning it to null. Otherwise it's not necessary to use ternary operator. Hope this helps. Answered your question? – Ajaykumar Nov 28 '16 at 19:08