0

I know how to set the defaults of an object:

store.currentUser = {
  username: ''
}

And set new values to it:

store.getCurrentUser = () => {
  const currentUser = Parse.User.current()
    store.currentUser.username = currentUser.username
  }
}

But I can't do that if I have an array:

store.buildings = [
  // how to set the defaults here?
]

Because the number of objects that the array can contain is unknown:

store.findBuildings = () => {
  const query = new Parse.Query(Building)
  return query.find({
    success: (buildings) => {
      // _.map(buildings, (building) => building.toJSON())
      // -> [ {name: 'Name 1'}, {name: 'Name 2'}, etc... ]
      // how to give the new values to store.buildings?
    },
    error: (buildings, error) => {
      console.log('Error:', error.message)
    }
  })
}

Is here any way to accomplish this?

Note: I can't just do buildings = [] because I need the keys to have defaults in order for the reactivity of my app to work.

alex
  • 7,111
  • 15
  • 50
  • 77
  • @Sandip Nirmal I think I still can't set the defaults values if I do this? – alex Feb 25 '16 at 06:44
  • 1
    What about setting a template variable for `buildings`, and then cloning it, if the key length is 0 or so? – Jeremy Rajan Feb 25 '16 at 06:48
  • I am not 100% sure what you want. Do you want to have default values for properties of buildings getting pulled from Parse in case the data that comes from Parse doesn't have them? Or do you want to have a sample building or two that could be overridden by the Parse data? or... – Amadan Feb 25 '16 at 06:48
  • Why would `[]` not be a valid default? An array can have any number of elements, so also 0 elements is a possibility your application should be able to deal with. I think your problem is not with the default, but how you deal with that array further on. – trincot Feb 25 '16 at 06:55

1 Answers1

1

Check this answer

Array.prototype.repeat= function(what, L){
 while(L) this[--L]= what;
 return this;
}

var A= [].repeat(0, 24);

Or using second answer

var a = Array.apply(null, Array(24)).map(function() { return /your object here/ });
// or:
var a = Array.apply(null, Array(5)).map(Boolean).map(Number);
Community
  • 1
  • 1
Gor
  • 2,808
  • 6
  • 25
  • 46