I have a wrapper function where I use a variable dataObject. I have an action to trigger some outside functions within the wrapper function.
function wrapper() {
var dataObject;
var jsonPath = "dataObject[0]['Set1'][0]['Attribute1']";
eval('outsideFunction(dataObject, jsonPath)');
}
function outsideFunction(dataObject, jsonPath) {
dataObject[0]['Set1'][0]['Attribute1'] = 'asde'; //This sets the value to dataObject in the wapper
var attrVal = '123';
eval("jsonPath = attrVal"); //This doesn't set value to dataObject in the wrapper but in the local dataObject
}
Why is there a difference in the action of direct assign and assigning using eval?
According to your structure of data[0]['Set1'][0]['Attribute1'], which can be written as data[0].Set1[0].Attribute1, here is code, but I think you don't quite understand how many sets you were asking for.
var wrapper, outsideFunction;
wrapper = function(){
someOb = {};
var data = [
{
Set1: [
{
Attribute1: null, // we will change null to 'asdf' below
},
],
},
];
outsideFunction(data, someOb);
console.log( someOb.newProp, someOb.dumb );
// outputs 'hehehehe', undefined
};
outsideFunction = function(data, blah) {
data[0].Set1[0].Attribute1 = 'asdf';
//blah is a reference to someOb
// we can change a part of blah and it will look at the reference and change someOb
blah.newProp = 'hehehehe';
// but if we do `blah =`, then we reference a new object
// This won't affect someOb, blah will just be a different object talking about something else
blah = { dumb: false };
};
So, like I was saying, your data object is a numbered set, ( you have [0] ), then a named set (Set1), then a numbered set ( [0] ), I don't think you mean to nest so much.
numberedSet = [
{
name: 'dan',
likes: [
'coding', 'girls', 'food',
],
},
{
name: 'Sreekesh',
},
]
namedSet = {
dan: {
isPerson: true,
likes: [
'coding', 'girls', 'food',
],
},
Sreekesh: {
isPerson: true,
askedQuestion: function(){
return true;
},
}
};
numberedSet[0].name == dan; // true
numberedSet[0].likes[1] == 'girls'; // true
namedSet.dan.isPerson == true; // true
namedSet.Sreekesh.askedQuestion(); // true
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With