-1

I have some data in that i want to store in a hash. But don't have a key for each one of them ... Was thinking about generating a random key (string) and adding the value ... But I am not sure about how to generate those, neither if this is a good solution. I will be pleased to have some ideas. I am using javascript. For example, I have this:

   {
        mo: 'hi',
        to: 'how are you?',
        de: 'I am good',
        re: 'Okay'
   }

And then I want to add the value "What about you?" in the data above. I will have to add a key(any key). I saw THIS post which looks cool, but I would like something like a HASH.insertValue(value) Thanks ...

Community
  • 1
  • 1
Amon Bazongo
  • 315
  • 4
  • 10

1 Answers1

0

I'll say again that it's ludicrous to want to use a hash/object, when really you have no keys and need to bend over backwards to pull some out of thin air. But if that's really what you want to do, the sanest solution is probably a simple counter:

var counter = 1;
myWeirdHash['key' + counter++] = 'Foo';
myWeirdHash['key' + counter++] = 'Bar';
myWeirdHash['key' + counter++] = 'Baz';
deceze
  • 510,633
  • 85
  • 743
  • 889
  • this solution is cool ... I will try it, didn't think about that. But to be more explicit, let's say you have a http request to get data and what you get is a "WeirdHash". That's not what you want, but you still get it because you dont have the other code with you. You hope you understand what I am talking about ... – Amon Bazongo Aug 18 '15 at 09:38
  • I understand that you may be in a less than ideal situation there, yes. So my first approach would be to rectify that to a more optimal situation by fixing the weirdness of the hash. I understand that that's sometimes not possible, so here's the hack-around. – deceze Aug 18 '15 at 09:46