skip to Main Content

I have an array which looks like this after I console.log it:

[
  {
    Country: 'US',
    CityName: 'Seattle',
    Region: 'WA',
    PostalCode: '99301',
    StreetName: '3512 Alaska Street'
  },
  to_EmailAddress: { results: [ [Object] ] }
]

Funny thing is, the length of this array is 1, not 2. So, when I try console.log(arr[0]), I get:

{
  Country: 'US',
  CityName: 'Seattle',
  Region: 'WA',
  PostalCode: '99301',
  StreetName: '3512 Alaska Street'
}

When I try to console.log(arr[1]) I get undefined.

How do I get the to_EmailAddress attribute? Even better, how do I make to_EmailAddress: { results: [ [object] ] } part of the 1st element in the array. Basically, I want:

[
  {
    Country: 'US',
    CityName: 'Seattle',
    Region: 'WA',
    PostalCode: '99301',
    StreetName: '3512 Alaska Street',
    to_EmailAddress: { results: [ [Object] ] }
  },
]

But I am not sure how to create that when I cannot even access to_EmailAddress attribute

2

Answers


  1. Whatever you console.log (maybe you should put that part here as well) is not the valid javascript array.

    If you have array, each item has a value (i.e. reference to object or value of primitive type). You cant have "named variable" like to_EmailAddress: { results: [ [Object] ] } as part of the array.

    The valid array is for example if you have object that has this variable {to_EmailAddress: { results: [ [Object] ] }}

    Login or Signup to reply.
  2. Its a monkey patched property to_EmailAddress.

    const arr = [{
        Country: 'US',
        CityName: 'Seattle',
        Region: 'WA',
        PostalCode: '99301',
        StreetName: '3512 Alaska Street'
    }];
    
    arr.to_EmailAddress = { 
        results: []
    }
    
    
    console.log(arr);
    

    Gives the output:

    [
      {
        Country: 'US',
        CityName: 'Seattle',
        Region: 'WA',
        PostalCode: '99301',
        StreetName: '3512 Alaska Street'
      },
      to_EmailAddress: { results: [] }
    ]
    

    Just acces it like any other property:

    console.log(arr.to_EmailAddress);
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search