0

the code

terminal

I can't think of anything

I know this is a array but node js is recognize it as object in innarflightInfo fn data supposed to be array but it is a object why?

You can see I

console.log(typeof data)
console.log(data)

IN THE IMAGE.

const innarflightInfo = (data) => {
    const flightd = []
    console.log(typeof data)
    console.log(data)
    data?.map((fdata,i) => {    // GIVING ERROR
        const flight = {}

        flight.companyId = fdata?.flightInformation?.companyId?.marketingCarrier || ''
        flight.operatingCarrier = fdata?.flightInformation?.companyId?.operatingCarrier || ''
        flight.dateOfDeparture = fdata?.flightInformation?.productDateTime?.dateOfDeparture || ''
        flight.timeOfDeparture = fdata?.flightInformation?.productDateTime?.timeOfDeparture || ''
        flight.dateOfArrival = fdata?.flightInformation?.productDateTime?.dateOfArrival || ''
        flight.timeOfArrival = fdata?.flightInformation?.productDateTime?.timeOfArrival || ''
        flight.from = fdata?.flightInformation?.location[0]?.locationId || ''
        flight.fromTerminal = fdata?.flightInformation?.location[0]?.terminal || ''
        flight.to = fdata?.flightInformation?.location[1]?.locationId || ''
        flight.toTerminal = fdata?.flightInformation?.location[1]?.terminal || ''
        flight.flightOrtrainNumber = fdata?.flightInformation?.flightOrtrainNumber || ''
        flight.equipmentType = fdata?.flightInformation?.productDetail?.equipmentType || ''
        flight.electronicTicketing = fdata?.flightInformation?.addProductDetail?.electronicTicketing || ''

        flightd.push(flight)
    })
    return flightd
}


const flightInfo = (data) => {
    const flights = {}
    data?.map((fdata,i) => {
        flights.FlightNo = fdata?.propFlightGrDetail?.flightProposal[0]?.ref || ''
        flights.flightDetails = innarflightInfo(fdata?.flightDetails)
    })
    console.log(flights)
    return flights
}
Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
Blank
  • 9
  • 2
  • 6
    Arrays are "objects." Use `Array.isArray`. [See also this](https://stackoverflow.com/questions/767486/how-do-i-check-if-a-variable-is-an-array-in-javascript), or any of the many other similar questions about `typeof someArray` – Zac Anger Jul 23 '23 at 18:11
  • 2
    You are misusing `.map()`. The `.map()` function *makes a new array for you*; there's no need to declare one of your own. If you just want to iterate through an array, use `.forEach()`. – Pointy Jul 23 '23 at 18:12
  • Re: Pointy's comment (and disclaimer this is my blog) I wrote this up because it's exhausting explaining common array methods over and over again: https://zacanger.com/blog/posts/common-array-methods/ – Zac Anger Jul 23 '23 at 18:33
  • Saved you some code: https://jsfiddle.net/mplungjan/etqk3wdg/ – mplungjan Jul 23 '23 at 18:50

1 Answers1

0

// Ans 1

// data I know highest number of array item

const innarflightInfo = (data) => {
let flightd = []
const gatData = (data,i) => {
    const flight = {}
    flight.companyId = data[i]?.flightInformation?.companyId?.marketingCarrier || ''
    flight.operatingCarrier = data[i]?.flightInformation?.companyId?.operatingCarrier || ''
    flight.dateOfDeparture = data[i]?.flightInformation?.productDateTime?.dateOfDeparture || ''
    flight.timeOfDeparture = data[i]?.flightInformation?.productDateTime?.timeOfDeparture || ''
    flight.dateOfArrival = data[i]?.flightInformation?.productDateTime?.dateOfArrival || ''
    flight.timeOfArrival = data[i]?.flightInformation?.productDateTime?.timeOfArrival || ''
    flight.from = data[i]?.flightInformation?.location[0]?.locationId || ''
    flight.fromTerminal = data[i]?.flightInformation?.location[0]?.terminal || ''
    flight.to = data[i]?.flightInformation?.location[1]?.locationId || ''
    flight.toTerminal = data[i]?.flightInformation?.location[1]?.terminal || ''
    flight.flightOrtrainNumber = data[i]?.flightInformation?.flightOrtrainNumber || ''
    flight.equipmentType = data[i]?.flightInformation?.productDetail?.equipmentType || ''
    flight.electronicTicketing = data[i]?.flightInformation?.addProductDetail?.electronicTicketing || ''
    
    return flight
}

const p4 = data[4]?.flightInformation?.companyId?.marketingCarrier || ''
const p3 = data[3]?.flightInformation?.companyId?.marketingCarrier || ''
const p2 = data[2]?.flightInformation?.companyId?.marketingCarrier || ''
const p1 = data[1]?.flightInformation?.companyId?.marketingCarrier || ''
const p = data[0]?.flightInformation?.companyId?.marketingCarrier || ''
const loop = p4 != '' ? 5 : p3 != '' ? 4 : p2 != '' ? 3 : p1 != '' ? 2 : p != '' ? 1 : 0
for (let i = 0; i < loop; i++) {
    const d = gatData(data,i)
    flightd.push(d)
}

return flightd

}

// Ans 2

// data I know highest number of array item

const innarflightInfo = (data) => {
let flightd = []
const p4 = data[4]?.flightInformation?.companyId?.marketingCarrier || ''
const p3 = data[3]?.flightInformation?.companyId?.marketingCarrier || ''
const p2 = data[2]?.flightInformation?.companyId?.marketingCarrier || ''
const p1 = data[1]?.flightInformation?.companyId?.marketingCarrier || ''
const p = data[0]?.flightInformation?.companyId?.marketingCarrier || ''
const loop = p4 != '' ? 5 : p3 != '' ? 4 : p2 != '' ? 3 : p1 != '' ? 2 : p != '' ? 1 : 0

let fdt = []
for (let i = 0; i < loop; i++) {
    fdt.push(data[i])
}
fdt?.map((parflight,i) => {
    const addData = {
        companyId:parflight?.flightInformation?.companyId?.marketingCarrier || '',
        operatingCarrier:parflight?.flightInformation?.companyId?.operatingCarrier || '',
        dateOfDeparture:parflight?.flightInformation?.productDateTime?.dateOfDeparture || '',
        timeOfDeparture:parflight?.flightInformation?.productDateTime?.timeOfDeparture || '',
        dateOfArrival:parflight?.flightInformation?.productDateTime?.dateOfArrival || '',
        timeOfArrival:parflight?.flightInformation?.productDateTime?.timeOfArrival || '',
        from:parflight?.flightInformation?.location[0]?.locationId || '',
        fromTerminal:parflight?.flightInformation?.location[0]?.terminal || '',
        to:parflight?.flightInformation?.location[1]?.locationId || '',
        toTerminal:parflight?.flightInformation?.location[1]?.terminal || '',
        flightOrtrainNumber:parflight?.flightInformation?.flightOrtrainNumber || '',
        equipmentType:parflight?.flightInformation?.productDetail?.equipmentType || '',
        electronicTicketing:parflight?.flightInformation?.addProductDetail?.electronicTicketing || '',
    }
    flightd.push(addData)
})

return flightd

}

Blank
  • 9
  • 2
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jul 27 '23 at 11:01