1

Well, when i'm trying to use the array like this let titles = ["Julian","August"] the .sort() works, but when i try to use like that

function showTitles(jsonObj) {
    let filmes = jsonObj.filmes;
    for (let i = 0; i < filmes.length; i++) {
        let titles = [];
        titles.push(filmes[i].title);
        console.log(titles.sort());
    }
}

doesn't work. I have a json of objects with titles and I want to sort this alphabetically.

the json:

{
  "filmes": [
    {
      "id": "01",
      "title": "Os Simpsons O Filme",
      "url": "https://youtu.be/KCpcGsFl90c",
      "image": "sMcsZ7BlOf2LDhTlfPlWb3w6lJM.jpg",
      "sinopse": "Homer Simpson.",
      "time": "1h:26m",
      "quality": "720p",
      "latestAdded": false,
      "problem": false,
      "release": "27/07/2007",
      "version": "Dublado",
      "gender": [
        "Animação",
        "Comédia"
      ]
    }
}

Please if someone knows how to do this works, help me :/

3
  • Is "filmes" an array inside of the jsonObj? Commented Jun 29, 2020 at 2:43
  • i have edited the ask for see the complete json Commented Jun 29, 2020 at 2:47
  • do you want to sort the object by its keys, so do you want to get output like this: "filmes": [ { "a":"01", "b": "lorem", "z": "dummy", } Commented Jun 29, 2020 at 2:52

3 Answers 3

2

let is block scoped. This means it only exists inside the { } it was created in. Each iteration of your for loop is creating a branch new titles variable that is empty. You then sort it with only one value in it.

Here's another way to write it so that you aren't losing the variable each iteration:

function showTitles(jsonObj) {
    let filmes = jsonObj.filmes;
    let titles = [];

    for (let i = 0; i < filmes.length; i++) {
        titles.push(filmes[i].title);
    }

    console.log(titles.sort());
}

There's also a great built in map function that can reduce the amount of code for this. It uses a lot of ES6 syntax, so if you are new to JS I don't recommend using it until you have a good grasp of the fundamentals.

let titles = jsonObj.filmes.map(({ title }) => title);
console.log(titles.sort());
Sign up to request clarification or add additional context in comments.

3 Comments

i'm js begginer, but thansk for tip, and the answer
thank you, but, when i do that, and trying to have just 1 value, i cant, because print all in one
If you only want one, then just console.log(titles[0])
1

You can't initialize the array inside of for loop because it Will reset in every new loop

    function showTitles(jsonObj) {
        let filmes = jsonObj.filmes;
        let titles = [];
        for (let i = 0; i < filmes.length; i++) {
  
         titles.push(filmes[i].title);
        }
     console.log(titles.sort());
    }

1 Comment

thank you, but, when i do that, and trying to have just 1 value, i cant, because print all in one
0

I am not sure whether you posted JSON or a JavaScript object literal; it is neither valid JS nor valid JSON because you can't have an empty value like "id": ,. Did you mean:

{
      "id": '',
      "title": "Ratatouille",
      "url": '',
      "image": '',
      "sinopse": '',
      "time": '',
      "quality": '',
      "latestAdded": '',
      "problem": '',
      "release": '',
      "version": '',
      "gender": [

      ]
}

In that case, your method won't work because there is no property filmes on the object.

Also, your naming of the parameter as jsonObj doesn't make sense -- JSON is parsed into a regular JavaScript object, so did you mean to call it something like filmesObj?

1 Comment

I have, look i will edit the code for you see, wait please

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.