0

I have an object with dynamic properties. Each of these properties are removed and added based on some events. I want to have a function or property in this object which can return the array of values but having the same reference all the time. Whats the best way to do it?

For e.g if current state of the object is

var obj = {"410f0ec7bd420d6eafea36bedb716ade" : { 'name' : 'dark'} }
var values = obj.someFunction()

values should be [{ 'name' : 'dark'}]

if current state of obj is

{"410f0ec7bd420d6eafea36bedb716ade" : { 'name' : 'dark'} ,
"f44abc3bb1dad3cd20e97e6a21416830": { 'name' : 'magic'}}

values should be [{ 'name' : 'dark'},{ 'name' : 'magic'}]

The reference of the array and the properties should never change (unless they are deleted).

6
  • What do you mean by "the reference of the array"? Its address in memory? Commented Jun 13, 2014 at 12:12
  • Do you mean values should automatically update when obj is changed? Commented Jun 13, 2014 at 12:12
  • @Bart: Yes, I was doing something like return return Object.keys(obj).map(function (key) { return obj[key] }); But that returns a new array every time Commented Jun 13, 2014 at 12:17
  • @RobbyCornelissen : Yes. Right on. Commented Jun 13, 2014 at 12:20
  • 1
    @PradeepMahdevu You're out of luck unless you want to use Object.observe which has very bad browser support, but there's a polyfill available. Commented Jun 13, 2014 at 12:28

3 Answers 3

1

How about this? It maintains the same array. If you want, you could also mix it in with the object, but would have to add a guard to not also add the function to the values.

var values = someFunction(obj, values);

function someFunction(obj, values) {
    values = values || [];
    values.length = 0;

    for(var key in obj) {
        values.push(obj[key]);
    }

    return values;
}

By the way, clearing the array by setting its length to 0 was gleaned from this post.

Sign up to request clarification or add additional context in comments.

6 Comments

The only thing with this approach is that it changes the reference of the objects.
The objects in the values you mean?
Yes, each of those references are tied back to view, and i would like to not refresh the array. If i do, its a huge refresh.
+1 for the values.length =0 ; but on a general note it is slower.
It's an interesting problem, and at this point I'm not really sure if there's a solution that fits all your requirements. I'll try to look into it some more. About the length = 0 being slower: it seems that looping and popping is indeed the most efficient, but I guess it very much depends on the Javascript engine.
|
1

My might create a 'meta'-object that stores a reference to the original object and can return the values:

var Values = function(obj) {

 this.getValues = function() {

  var values = [];
  for(i in obj)
   values.push(obj[i]);
  return values;
 };

}

var original = {"410f0ec7bd420d6eafea36bedb716ade" : { 'name' : 'dark'} ,
                "f44abc3bb1dad3cd20e97e6a21416830": { 'name' : 'magic'}};

var vals = new Values(original);

var values = vals.getValues();

3 Comments

The only thing with this approach is that it changes the reference of the objects.
@PradeepMahdevu With this approach, changes to 'values' actually are reflected in 'original'.
When you are calling getValues() each time, you are returning a new array. So the array is a new ref every time. you can get away with that, values = values || []; values.length = 0;
0

Given that you seem to be generating the array within "someFunction" (seeing the code of the function and how you attach it to the object would help), you'll need to keep an instance of an array and empty/refill it rather than create a new one. It could be a member of your object (obj.currentItems) or within a closure (depending on how you create it), and it could be updated as you change its properties or on demand within someFunction.

I'll update my answer if you provide more specific code.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.