0

When trying to construct a class by passing in an array of strings, the new object string array is empty.

I've tried several different approaches to initialize the new objects string array in the constructor, including, but not limited to the following:

this.newArray = oldarray.slice();

this.newArray = Array.from(oldarray);

this.newArray = oldarray.map( s => { return s; });

iterating through the old array and pushing the elements one at a time in to the new array.

What is interesting here is that the when creating the for loop, I noticed that the string array acts like it has a property called "array". So the for loop wouldn't process because oldarray.length = 0, but oldarray.array.length = 5

I'm able to see this in the debugger. If I attempt to change the code to reference oldarray.array, it underlines and complains about the .array portion and fails to compile under ng serve.

I know this is probably going to end up being something easy / stupid, and its just a matter of me staring at the same code for several hours.

export class Widget {
  public role: string;
  public array: WidgetTuple[];

  constructor(role: string, tuplearray?: WidgetTuple[]) {
    this.role = role;
    this.array = [];
    if (tuplearray != undefined) {
      for (var lcv = 0; lcv < tuplearray.length; lcv++) {
        var tupl = new WidgetTuple(tuplearray[lcv].widgetGrouper);
        this.array.push(tupl);        
      }
    }
  }
}

export class WidgetTuple {
  public widgetGrouper: string[];

  constructor(strvalues?: string[]) {
    this.widgetGrouper = [];
    if (strvalues != undefined) {
      this.widgetGrouper = strvalues.slice();
      //this.widgetGrouper = strvalues.map()
      //for (var lcv = 0; lcv < strvalues.length; lcv++) {
      //  var entry = strvalues[lcv].toString();
      //  this.widgetGrouper.push(entry);
      //}
    }
  }
}

I expect the string elements from the original widgetGrouper string array to be included in the new WidgetTuple object that is added to the Widget array.

All attempts so far, yield empty string array results in the newly created object.

Thank you in advance for your assistance.

This didn't format well in my comment, so here it is, hitting the debug breakpoint in VS and looking at strvalues:

-       strvalues   Array(0) [] Object
+       array   Array(4) ["widgetone", "widgettwo", "someitem", …]  Object
        length  0   number
+       __proto__   Array(0) [, …]  Object
6
  • the string array acts like it has a property called "array" - Are you just referring to the browser console? They typically just say "Array" or "Object" just to make it clear what it is. Commented Jun 7, 2019 at 18:42
  • Sorry, to be more specific, my reference is to debugging from a breakpoint within visual studio. Hitting the breakpoing, strvaluse looks something like this: - strvalues Array(0) [] Object + array Array(4) ["widgetone", "widgettwo", "someitem", …] Object length 0 number + proto Array(0) [, …] Object Commented Jun 7, 2019 at 18:50
  • I'm lost on what the specific problem you're having is. What is your question here? Commented Jun 7, 2019 at 18:57
  • The question is, why is the new object not instantiating correctly? strvalues has 4 entries, and yet the tupl object that is pushed in to the widget array has no strings in its WidgetTuple object. Commented Jun 7, 2019 at 19:02
  • How are you instantiating the object? I suspect that you're passing something other than a WidgetTuple[] in to your constructor. Commented Jun 7, 2019 at 20:12

1 Answer 1

0

Figured it out. I would delete the question if I could, but who knows... this might help somebody else some day.

Basically, the answer lies within the danger of any.

In the component view, I had an input of type any for the widget object. The object was then incorrectly referenced during its use within the view (adding the array property).

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

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.