I have an issue where I'm importing an object into another file. Object looks like:
export const MyObject = {
value1: mergeObject({object of data}),
value2: mergeObject({object of data}),
...
};
The object values call a function where I'm merging two objects. The issue is that when I'm importing MyObject
to another file and I'm attempting to access a single value all of the functions for each value fire off.
I anticipated just being able to import the object like normal and access the values like MyObject.value1
, but the function was being called for every value of the object.
CodePudding user response:
Because that's exactly what this syntax does:
{
value1: mergeObject({object of data}),
value2: mergeObject({object of data})
}
To build this object literal, it is evaluating the expressions for its properties. It sounds like you want those properties to be functions, not values. Something like this:
{
value1: () => mergeObject({object of data}),
value2: () => mergeObject({object of data})
}
And then you'd invoke the function when you want to use it:
MyObject.value1()