to sort an array of objects within an array of objects

Asked

Viewed 35 times

-1

i have the following data:

[
  {
    "titulo": "Livro",
    "descricao": "descricao do livro 1",
    "info": [
        {
            "nome": "nome do livro 1",
            "paginas": 223,
            "lido": true,
        }
    ],
  },
  {
    "titulo": "Trilogia 1",
    "descricao": "descricao da trilogia 1",
    "info": [
        {
            "nome": "livro 1"
            "paginas": 219,
            "lido": true,
        },
        {
            "nome": "livro 2"
            "paginas": 242,
            "lido": false,
        },
        {
            "nome": "livro 3"
            "paginas": 331,
            "lido": false,
        }
    ],
  }
]

What I’m trying to do and I couldn’t do is: sort the titles according to the book read, if any book info has read === false, this unread book has to be at the top, so in this case, the Book has to come last and to Trilogy first, because she has false information in info, and in info sort tbm in the same way, the objects of read == false always at the top

I tried to use reduce, map + filter but could not get a result of type, this part of sort so specifically confused me

  • 1

    Why did you use map and filter? These functions do not do this. The correct function is sort, and this allows you to use callback.

  • is that in case I needed to iterate through the array info of each object to find if it exists read === false and sort to the top of the list

  • please include your code that you attempt to sort. I also recommend that you read the manual on how NOT to ask questions, especially the section Thinking we’ll do all your work for free

1 answer

0


You can use a callback in the method Array.sort. This accepts a callback. In that callback we can return 1, 0 or -1. -1 changes the position of the item to the first, 1 for the final, and 0 holds in the same position.

See a short example:

livros.sort(function (livro) {
    livro.info.sort(function (item) {
        return item.lido ? 1 : -1;
    })
    
})

var livros = [
  {
    "titulo": "Livro",
    "descricao": "descricao do livro 1",
    "info": [
        {
            "nome": "nome do livro 1",
            "paginas": 223,
            "lido": true,
        }
    ],
  },
  {
    "titulo": "Trilogia 1",
    "descricao": "descricao da trilogia 1",
    "info": [
        {
            "nome": "livro 1",
            "paginas": 219,
            "lido": true,
        },
        {
            "nome": "livro 2",
            "paginas": 242,
            "lido": false,
        },
        {
            "nome": "livro 3",
            "paginas": 331,
            "lido": false,
        }
    ],
  }
];


livros.sort(function (livro) {
    livro.info.sort(function (item) {
       return item.lido ? 1 : -1;
    })
   
})

console.log(livros)

Like the sort is part of the Array of origin, so I applied the sort in the Array intern.

The question also says that with the second item has unread, so you need to apply the sort also in the Main Array.

Behold:

livros.sort(function (livro) {
    livro.info.sort(function (item) {
        return item.lido ? 1 : -1;
    })
    
    return livro.info.some(function (item) {
        return item.lido === false;
    }) ? -1 : 1;
    
})

var livros = [
  {
    "titulo": "Livro",
    "descricao": "descricao do livro 1",
    "info": [
        {
            "nome": "nome do livro 1",
            "paginas": 223,
            "lido": true,
        }
    ],
  },
  {
    "titulo": "Trilogia 1",
    "descricao": "descricao da trilogia 1",
    "info": [
        {
            "nome": "livro 1",
            "paginas": 219,
            "lido": true,
        },
        {
            "nome": "livro 2",
            "paginas": 242,
            "lido": false,
        },
        {
            "nome": "livro 3",
            "paginas": 331,
            "lido": false,
        }
    ],
  }
];


livros.sort(function (livro) {
    livro.info.sort(function (item) {
       return item.lido ? 1 : -1;
    })
    
    return livro.info.some(function (item) {
        return item.lido === false;
    }) ? -1 : 1;
   
})

console.log(livros)

The function some returns true if any item from array is assessed as true. In this case, if one of the item.lido is assessed as false, we apply a -1.

  • aaaa was just that, I got caught up in it, it was worth too much now I understood better this function!!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.