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

108
Vistas
How to use _sortBy in an array of objects that contains nested array of objects

I have a list of array of objects that contains a nested array of objects like so:

const mainList = [
{
    id:'001',
    category: 'A',
    content: [ 
        {
            title: 'Apples',
            language: 'en'
        },
        {
            title: '苹果',
            language: 'zh-cn'
        },
        {
            title: '苹果HK',
            language: 'zh-hk'
        },
    ],
},
{
    id:'002',
    category: 'B',
    content: [
        {
            title: 'Grapes',
            language: 'en'
        },
        {
            title: '葡萄',
            language: 'zh-cn'
        },
        {
            title: '葡萄HK',
            language: 'zh-hk'
        },
    ],
},
{
    id:'003',
    category: 'C',
    content: [
        {
            title: 'Bananas',
            language: 'en'
        },
        {
            title: '香蕉',
            language: 'zh-cn'
        },
        {
            title: '香蕉HK',
            language: 'zh-hk'
        },
    ]
}
]

I want to display this list using lodash _sortBy, by its title in alphabetical order like this:

Apples, Bananas, Grapes

My approach:

console.log(_sortBy(mainList.content.title))

But the result comes back with undefined at index 0

Thanks in advance!

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

0

the way you used sortBy is incorrect, you can use sortBy like this:

console.log(_.sortBy(mainList, "content.title"));

but the list does not have content.title, So you need to specify the index of content you want to sort by content[0].title

so the correct way to sort by title is like this:

console.log(_.sortBy(mainList, "content[0].title"));
about 4 years ago · Juan Pablo Isaza Denunciar

0

You ndon't really need lowdash to do the sorting:

const list = [{id:'001', category: 'A', 
                   content: [{title: 'Apples',language: 'en'},
                             {title: '苹果',language: 'zh-cn'},
                             {title: '苹果HK', language: 'zh-hk'}]},
                  {id:'002', category: 'B',
                   content: [{title: 'Grapes',language: 'en'},
                             {title: '葡萄',language: 'zh-cn'},
                             {title: '葡萄HK',language: 'zh-hk'}]},
                  {id:'003', category: 'C',
                   content: [{title: 'Bananas',language: 'en'},
                             {title: '香蕉',language: 'zh-cn'},
                             {title: '香蕉HK',language: 'zh-hk'}]}
                 ];

const srtv=o=>o.content[0].title;
console.log(list.sort((a,b)=>srtv(a).localeCompare(srtv(b))))

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