question

duartedd avatar image
duartedd asked

Cloudscript Javascript -filter api results

Hello I am trying to filter and return a new array with the objects filtered from 2 seperate arrays. the results are from read only data but i dont believe when i parse them they are an actual Array

  var QuestsThatCanBeGiven = TitleQuests.filter(val => !PlayerQuests.includes(val));

basically i am trying to assign quests that haven been already assigned that exist in the title data - titlequest - and compare that to playerquests to make sure it doesnt exist in there - and itll have an array of the titlequests so that i can iterate through them later

the title quests result is retrieve via

  var TitleQuests  =  JSON.parse( TitleQuestsResult.Data["TitleQuests"]);

this does result to an OBJECT OBJECT which i am guessing is the issue - is there an easy way to get this made into an array since the data is pretty much

TitleQuests: {  Quest1:    { Questname: "bar"                    Reward: "salad"  },   Quest2:   { Questname: "brrrr"                    Reward: "cad"  } }

I tried adding [ and ] to the beginning and end respectively to see if that would parse it into an Array of Objects.

have any suggestions or examples that i could go about checking?

the only thing i can think of is iterate through them right now with a for loop add each object to an array then filter.

thanks!

1 comment
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Seth Du avatar image Seth Du ♦ commented ·

We are not sure if the third code part is your JSON string data you stored in the read only data. There can be format issues when you manually craft JSON data. the correct format for your data provide should be like:

{"TitleQuests": 
    {"Quest1":
        {"Questname": "bar","Reward": "salad"  },
        {"Questname": "brrrr","Reward": "cad"  } 
    }
}

Strings need to be wrapped in double quotes and there is lack of comma. You can use online JSON format validator to check your data.

If I was wrong, would you please provide a sample data string which you stored in the read only data so that we can look into it for you.

0 Likes 0 ·
Seth Du avatar image
Seth Du answered

I have tested your code and your JSON data works fine. The things is after you use JSON.parse, DuelQuests is an array that contains only one element which is an Object Data you stored. That is why you always get 1 in the output message. Here is a quick fix:

// To access the Object data, you need to write:
log.debug(DuelQuests[0])

//To print out the actual data length you want, you may have to use :
log.debug(Object.keys(DuelQuests[0]).length)

If you delete the square brackets at the beginning and at the end, you can direct access to that Object, hence:

// this will print out an object
log.debug(DuelQuests)

// this will print out the length
Object.keys(DuelQuests).length

There are many approaches to convert an Object to Array, but most of them are manually crafting. You can work out your own solution in case there are specific needs.

For the JSON data, we noticed that there are blank spaces in your keys and it’s better to replace those special characters in JSON.phase() because right now you have to use DuelQuests[0]["Scholarly Yearning"] to access corresponding data. Without blank spaces, you can flexibly access it via DuelQuests[0].ScholarlyYearning. Even though JSON does not have a strict Naming Conventions, it is good to follow some for development convenience, for example, Google JSON Style.

1 comment
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

duartedd avatar image duartedd commented ·

Awesome thats it! -thankyou and makes sense i figured it had something to do with the arrangement of JSON data and being a massive object but couldnt figure out how to actually get the data according to each of the objects

thank you very much!

0 Likes 0 ·
duartedd avatar image
duartedd answered
[
  {
    "Scholarly Yearning": {
      "QuestName": "Scholarly Yearning",
      "Description": "Win 3 Duels.",
      "stat": "DuelsWon",
      "currentCount": 0,
      "totalCount": 3,
      "Rewards": [
        "GoblinLootPack"
      ]
    },
    "Diligent Disciple": {
      "QuestName": "Diligent Disciple",
      "Description": "Win 5 Duels.",
      "stat": "DamageDone",
      "currentCount": 0,
      "totalCount": 5,
      "Rewards": [
        "GoblinLootPack"
      ]
    },
    "Duelist": {
      "QuestName": "Duelist",
      "Description": "Play 7 Duels.",
      "stat": "DamageDone",
      "currentCount": 0,
      "totalCount": 7,
      "Rewards": [
        "GoblinLootPack"
      ]
    }
  }
]

Hey so here is an example I am trying to get each of objects into an array in cloudscript

I appear to only get as far as one returned in the length - i think its a giant object in the array

    var DuelQuestsResult = server.GetTitleData(getTitleDataRequest);
   
   
  var DuelQuests  =  JSON.parse(DuelQuestsResult.Data["DuelQuests"]);
     
  
   log.debug('[DuelQuests] DuelQuests == ' +   DuelQuests.length);
  

returned:

"Logs": [ { "Level": "Debug", "Message": "[DuelQuests] DuelQuests == 1"

i should get 3 but i only get 1

3

thanks for the help SethDu

10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.