Skip to content
Advertisement

how to build object JS

I have an array of booking and types. From these two arrays I need to build an object. Everything works great, except for the types. Types return an array in each object (same). How can you return the correct object?

const booking = [{row: 1, num: 2, level:3}]
const types = [1,2,3,4,5]

export const selectResult = createSelector([selectBooking, selectTypes], (booking, types) => {
    return booking.map((book) => {
        return {
            row: book.row,
            num: book.num,
            levelId: book.level,
            discount: types
        }
    })
})

Advertisement

Answer

found a solution to my problem. It was enough to add indexes

export const selectResult = createSelector(
    [selectBooking, selectTypes, selectPrices],
    (booking, types) => {
        return booking.map((book, idx) => {
            return {
                row: book.row,
                num: book.num,
                levelId: book.level,
                type: types[idx]
            }
        })
    }
)
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement