0

I was going through Google Documentations of Google's App Script for some web App. Basically I was trying to store simple data in key-value pairs scoped to one script, while I did manage to store and retrieve data but while i was trying to get only the keys, I got an array of keys which is not in order. Here's the code and its logs.

var scriptProperties = PropertiesService.getScriptProperties();
 scriptProperties.setProperties({
   'cow': 'moo',
   'sheep': 'baa',
   'chicken': 'cluck'
 });
 var keys = scriptProperties.getKeys();
 Logger.log('Animals known:');
 for (var i = 0; i < keys.length; i++) {
   Logger.log(keys[i]);
 }

Logs:

 Animals known:
 chicken
 cow
 sheep

Why Ohh why?

Rubén
  • 34,714
  • 9
  • 70
  • 166
Rupesh Bhandari
  • 76
  • 1
  • 12
  • 2
    A single Properties Service property can hold about 18k of content. If your data is not going to be larger than that, then you can put you data into one property instead of many. The value in a property must always be a string data type, so use JSON.stringify(data) before putting it into properties service, and use an array of inner objects to maintain the original order. – Alan Wells Jan 28 '18 at 14:16

1 Answers1

2

I think javascript objects in your example are expected to act that way, no promise of order. Arrays like myArray = [1,2,3] can be expected to be in order using your for loop method. But, not the object you're currently using.

Credits to @bpierre for the reference.

4.3.3 Object An object is a member of the type Object. It is an unordered collection of properties each of which contains a primitive value, object, or function. A function stored in a property of an object is called a method.

A Map iterates its elements in insertion order, whereas iteration order is not specified for Objects.

ReyAnthonyRenacia
  • 17,219
  • 5
  • 37
  • 56