Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

139
Vistas
no-nested-ternary on a nested and complicated array of objects

I have a complicated array of objects and I want to change a property that deeply nested in the array.

I did the following function and it is work, but the problem is that the eslint throw me an error because there is a nested ternery to check if i am on the currect place in an array:

toggleSwitchDetector(state: Array<IOpticalHeadUnit>, { payload }: any) {
            return [
                ...state.map((opticalHeadUnit) =>
                    opticalHeadUnit.name === payload.opticalHeadUnitName
                        ? {
                            ...opticalHeadUnit,
                            detectors: [
                                ...opticalHeadUnit.detectors.map(
                                    (detector, index) => ({
                                        ...detector,
                                        status: {
                                            ...detector.status,
                                            value: //Nested ternery. Bad practice.
                                                index === payload.index
                                                    ? detector.status
                                                        .value === 0
                                                        ? 1
                                                        : 0
                                                    : detector.status.value,
                                        },
                                    }),
                                ),
                            ],
                          }
                        : opticalHeadUnit,
                ),
            ];
        }

is there a simpler way to approach the modification of the deeply nested property?

EDIT:

the code after I modified it with the answers:


const getDetectorStatusValue=(index:number,payload:number,detector:IDetector)=>{
    if(index === payload)
    {
        if(detector.status.value===0)
            return 1
        return 0
    }
    return detector.status.value
    
        
}

toggleSwitchDetector(state: Array<IOpticalHeadUnit>, { payload }: any) {
            return state.map((opticalHeadUnit) =>
                opticalHeadUnit.name === payload.opticalHeadUnitName
                    ? {
                        ...opticalHeadUnit,
                        detectors: [
                            ...opticalHeadUnit.detectors.map(
                                (detector,index) => ({
                                    ...detector,
                                    status: {
                                        ...detector.status,
                                        value:getDetectorStatusValue(index,payload.index,detector)
                                    },
                                }),
                            ),
                        ],
                          }
                    : opticalHeadUnit,
            );
about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

If you want to get rid of the nested ternary operator, you could write slightly more code but it will be more readable.
You can check if index === payload.index with a simple if statement and only in that case go deep in your object to eventually check detector.status.value === 0 ? 1 : 0

Otherwise just return detector as is;

...opticalHeadUnit.detectors.map(
  (detector, index) => ({
    if (index === payload.index) {
      return {
        ...detector,
        status: {
          ...detector.status,
          value: detector.status.value === 0 ? 1 : 0
        },
      }
    } else {
      return detector;
    }
  }),
),
about 4 years ago · Juan Pablo Isaza Denunciar

0

Maybe something like this

toggleSwitchDetector(state: Array<IOpticalHeadUnit>, { payload }: any) {
    return state.map((opticalHeadUnit) => getState(payload, opticalHeadUnit)
}

getStatus(payload, detector) {
  return {
    ...detector.status,
    value: //Nested ternery. Bad practice.
        index === payload.index
            ? detector.status
                .value === 0
                ? 1
                : 0
            : detector.status.value,
  };
}

getDetectors(payload, opticalHeadUnit) {
  return opticalHeadUnit.detectors.map(
        (detector, index) => ({
            ...detector,
            status: getStatus(payload, detector),
        }),
    );
}

getState(payload, opticalHeadUnit) {
    opticalHeadUnit.name === payload.opticalHeadUnitName
        ? {
            ...opticalHeadUnit,
            detectors: getDetectors(payload, opticalHeadUnit);
          }
        : opticalHeadUnit,
}

you don't need [...array], replace it with array

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda