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

96
Vistas
I want to delete objects that do not have a specific array
console.log(value)

{
    List: [
        {
            id: 1
            title: 'hi',
            content: [
                {
                    id: 1,
                    functionId: 11,
                }
            ]
        }
        {
            id: 2,
            title: 'minsu',
            content: []
        }
    ]
}

I want to delete the object if there is no content array.

I am creating a variable called control and outputting it using the map function inside. If the objects without content array have to be deleted by applying a filter condition, I don't know how to do it.

let control: any[any] = [
    value.map((value: any) => {
        return {
            id: value?.id,
            title: value?.title,
            content: value?.content[0].functionId
        }
    })
] 

But now, when I do console.log(control), an error occurs because the second object does not have content.

I want to delete the second object without content and print it like this.

console.log(control) 
[
    id: 1,
    title: 'hi',
    functionId: 11
]

about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

First filter out the elements with 0 length content array or content array doesn't exists and then map to transform it

const List  = [        {            id: 1,            title: 'hi',            content: [                {                    id: 1,                    functionId: 11,                }            ]        },        {            id: 2,            title: 'minsu',            content: []        }    ]

const control = List
.filter(({content}) => content?.length)
.map(({id,title,content:[{functionId}]}) => ({id,title,functionId}))

console.log(control)

about 4 years ago · Juan Pablo Isaza Denunciar

0

Filter source list the way you need and reapply filtered list as new value

const sourceObject = {
    list: [
        {
            id: 1,
            title: 'hi',
            content: [
                {
                    id: 1,
                    functionId: 11,
                }
            ]
        },
        {
            id: 2,
            title: 'minsu',
            content: []
        }
    ]
};

const filteredList = sourceObject.list.filter(element => (
  typeof(element.content) === 'object' && element.content.length > 0
));

sourceObject.list = filteredList;

console.log(sourceObject);

about 4 years ago · Juan Pablo Isaza Denunciar

0

I usually use reduce to solve problems like this

const items = [
  {
    id: 1,
    title: 'hi',
    content: [
      {
        id: 1,
        functionId: 11,
      }
    ]
  },
  {
    id: 2,
    title: 'minsu',
    content: []
  }
];

const result = items.reduce((rs, item) => {
  const functionId = item?.content?.[0]?.functionId;
  if (functionId) {
    rs.push({
      id: item.id,
      title: item.title,
      functionId
    })
  }
  return rs;
}, [])


console.log(result)

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