0

I am trying to clean up my code as well as make it more generic for reuse. And would like to know if what I am attempting is possible.

I have a what will be a large array of objects which subsequently have more object arrays inside.

These arrays are easily navigated to as seen in the example, but I wanted to abbreviate the generic part into a variable to reuse.

jsFiddle

var widgetObject = [];

thisWidget = "widgetObject.xReport";

widgetObject["xReport"] = {
    id: "xReport",

    widgetTitleHeading: "x Report Heading",

    widgetFilters: {
        startDate: "01/01/2018",
        endDate: "01/01/2018"
    },
    widgetAttributes: {
        xPos: "0",
        yPos: "0",
        width: "5",
        height: "5"
    }
};

console.log(widgetObject.xReport.widgetFilters.startDate);

//this will not work.
console.log(thisWidget.widgetFilters.startDate);

My question here is, is it possible to create an abbreviated string like I am trying to with 'thisWidget' in the example somehow?

akinuri
  • 10,690
  • 10
  • 65
  • 102
  • Your `widgetObject` is an array. Why are you assigning properties to it? – akinuri Feb 11 '19 at 13:23
  • a generic way to achieve what you need is to traverse the `widgetObject` by keys (after splitiing given key on dot `"."`) and get the final object back as reference for futher use. if you want I can provide answer along these lines, but several questions on SO deal with problem, eg https://stackoverflow.com/questions/4244896/dynamically-access-object-property-using-variable – Nikos M. Feb 11 '19 at 13:29
  • Could you clarify what you want `thisWidget` to accomplish? Should it always refer to `widgetObject.xReport` at the time `thisWidget` was assigned? Should it refer to whatever `widgetObject.xReport` is even if `widgetObject` later changes? Something else? – twhb Feb 11 '19 at 13:31
  • I left a reply at the bottom, thanks for the comments. – Ross Summerell Feb 11 '19 at 13:43

4 Answers4

1

If you want to make the assignemnt dynamic you have to access it like this:

var thisWidget = "xReport";
console.log(widgetObject[thisWidget].widgetFilters.startDate);
Albondi
  • 1,141
  • 1
  • 8
  • 19
  • @twhb I think I misunderstood the question at first, I edited my answer. `"widgetObject.xReport"` is not equivalent to `widgetObject["xReport"]` or `widgetObject.xReport` since it is a string, not an object – Albondi Feb 11 '19 at 13:22
  • Got it. I think I misunderstood your answer, too. – twhb Feb 11 '19 at 13:33
1
var widgetObject = [];

//thisWidget = "widgetObject.xReport";// Don't do this  <<<<<<<<<<<<<<

widgetObject["xReport"] = {
    id: "xReport",

    widgetTitleHeading: "x Report Heading",

    widgetFilters: {
        startDate: "01/01/2018",
        endDate: "01/01/2018"
    },
    widgetAttributes: {
        xPos: "0",
        yPos: "0",
        width: "5",
        height: "5"
    }
};

console.log(widgetObject.xReport.widgetFilters.startDate);

var thisWidget = widgetObject.xReport; // Just add this line   <<<<<<<<<<<<<

//this will work.
console.log(thisWidget.widgetFilters.startDate);
evilReiko
  • 19,501
  • 24
  • 86
  • 102
1

 var widgetObject = [];
 widgetObject["xReport"] ={
            id: "xReport",

            widgetTitleHeading: "x Report Heading",

            widgetFilters: {
                startDate: "01/01/2018",
                endDate: "01/01/2018"
            },
            widgetAttributes: {
                xPos: "0",
                yPos: "0",
                width: "5",
                height: "5"
            }

        };
        thisWidget = widgetObject.xReport;

        console.log(widgetObject.xReport.widgetFilters.startDate);
        console.log(thisWidget.widgetFilters.startDate);

Its working fine.

Durgpal Singh
  • 11,481
  • 4
  • 37
  • 49
0

Thank you for the replies, the answer was simple I was just a little off by trying to declare my variable before pushing it to the array.