0

I have a nested object with nested array objects. I need to extract each object separately. This is a sample of the object:

  records =  [{"keys":["voters","groups"],"length":2,"_fields":[{"identity":{"low":14,"high":0},"labels":["Voter"],"properties":{"name_key":"AINSWORTHLLOYDCECIL","occupation":"CARPENTER","gender":"MR","street":"12 ST JAMES ROAD","name":"LLOYD AINSWORTH","last_name":"AINSWORTH","middle_name":"CECIL","postal_code":"KGN 02","first_name":"LLOYD"}},{"identity":{"low":23666,"high":0},"labels":["Group"],"properties":{"name":"IMGroup","description":"Master group for individual members","since":{"low":-661343024,"high":374},"status":"active"}}],"_fieldLookup":{"voters":0,"groups":1}},{"keys":["voters","groups"],"length":2,"_fields":[{"identity":{"low":15,"high":0},"labels":["Voter"],"properties":{"name_key":"ALEXANDERDWAYNEBARRINGTON","occupation":"AIR CON TECH","gender":"MR","street":"3 1/2 JOHNSON TERRACE","name":"DWAYNE ALEXANDER","last_name":"ALEXANDER","middle_name":"BARRINGTON","postal_code":"KGN 02","first_name":"DWAYNE"}},{"identity":{"low":23666,"high":0},"labels":["Group"],"properties":{"name":"IMGroup","description":"Master group for individual members","since":{"low":-661343024,"high":374},"status":"active"}}],"_fieldLookup":{"voters":0,"groups":1}},{"keys":["voters","groups"],"length":2,"_fields":[{"identity":{"low":16,"high":0},"labels":["Voter"],"properties":{"name_key":"ALLENANNETTEUNA","occupation":"COSMETOLOGIST","gender":"MISS","street":"28 CARNARVAN STREET","name":"ANNETTE ALLEN","last_name":"ALLEN","middle_name":"UNA","postal_code":"KGN 02","first_name":"ANNETTE"}}........]

I am accustomed to using

records.map(obj => obj.get(0).properties ? obj.get(0).properties : obj.get(0));

or

records[0].get('voters').properties

for some reason I am getting

obj.get is not a function

I am not sure what I a missing here...would appreciate some help.

Here is the reproduction:

records = [{
  "keys": ["voters", "groups"],
  "length": 2,
  "_fields": [{
    "identity": {
      "low": 14,
      "high": 0
    },
    "labels": ["Voter"],
    "properties": {
      "name_key": "AINSWORTHLLOYDCECIL",
      "occupation": "CARPENTER",
      "gender": "MR",
      "street": "12 ST JAMES ROAD",
      "name": "LLOYD AINSWORTH",
      "last_name": "AINSWORTH",
      "middle_name": "CECIL",
      "postal_code": "KGN 02",
      "first_name": "LLOYD"
    }
  }, {
    "identity": {
      "low": 23666,
      "high": 0
    },
    "labels": ["Group"],
    "properties": {
      "name": "IMGroup",
      "description": "Master group for individual members",
      "since": {
        "low": -661343024,
        "high": 374
      },
      "status": "active"
    }
  }],
  "_fieldLookup": {
    "voters": 0,
    "groups": 1
  }
}, {
  "keys": ["voters", "groups"],
  "length": 2,
  "_fields": [{
    "identity": {
      "low": 15,
      "high": 0
    },
    "labels": ["Voter"],
    "properties": {
      "name_key": "ALEXANDERDWAYNEBARRINGTON",
      "occupation": "AIR CON TECH",
      "gender": "MR",
      "street": "3 1/2 JOHNSON TERRACE",
      "name": "DWAYNE ALEXANDER",
      "last_name": "ALEXANDER",
      "middle_name": "BARRINGTON",
      "postal_code": "KGN 02",
      "first_name": "DWAYNE"
    }
  }, {
    "identity": {
      "low": 23666,
      "high": 0
    },
    "labels": ["Group"],
    "properties": {
      "name": "IMGroup",
      "description": "Master group for individual members",
      "since": {
        "low": -661343024,
        "high": 374
      },
      "status": "active"
    }
  }],
  "_fieldLookup": {
    "voters": 0,
    "groups": 1
  }
}, {
  "keys": ["voters", "groups"],
  "length": 2,
  "_fields": [{
    "identity": {
      "low": 16,
      "high": 0
    },
    "labels": ["Voter"],
    "properties": {
      "name_key": "ALLENANNETTEUNA",
      "occupation": "COSMETOLOGIST",
      "gender": "MISS",
      "street": "28 CARNARVAN STREET",
      "name": "ANNETTE ALLEN",
      "last_name": "ALLEN",
      "middle_name": "UNA",
      "postal_code": "KGN 02",
      "first_name": "ANNETTE"
    }
  }]
}]

console.log(records[0].get('voters').properties)
jfriend00
  • 683,504
  • 96
  • 985
  • 979
MichaelE
  • 757
  • 14
  • 42
  • 1
    please show the expected output! – Mulan Jan 12 '21 at 00:56
  • `.get()` is not a method on JS objects and arrays. See [this](https://stackoverflow.com/questions/4244896/dynamically-access-object-property-using-variable) for objects and [this](https://stackoverflow.com/questions/8238456/how-to-get-value-at-a-specific-index-of-array-in-javascript) for arrays. – Zac Anger Jan 12 '21 at 01:02

1 Answers1

0

From the looks of it you might be familiar with Python, who's dictionary objects include a .get method. However, JavaScript objects do not have a .get method; their properties can be accessed in a number of ways, but most commonly using the dot notation:

records[0].voters.properties

In python the .get operator is often used to guard against errors when the key you are trying to access isn't in the dictionary. In JavaScript, no error will be thrown for accessing keys that are not a part of the object. Instead, trying to access a key not present in the object just results in the value undefined.

If you need to guard against accessing a property of a possibly undefined object (eg: records[0].voters.properties), you may want to use the optional chaining operator ?. instead of the usual access via . (eg: records[0].voters?.properties).


records = [{
  "keys": ["voters", "groups"],
  "length": 2,
  "_fields": [{
    "identity": {
      "low": 14,
      "high": 0
    },
    "labels": ["Voter"],
    "properties": {
      "name_key": "AINSWORTHLLOYDCECIL",
      "occupation": "CARPENTER",
      "gender": "MR",
      "street": "12 ST JAMES ROAD",
      "name": "LLOYD AINSWORTH",
      "last_name": "AINSWORTH",
      "middle_name": "CECIL",
      "postal_code": "KGN 02",
      "first_name": "LLOYD"
    }
  }, {
    "identity": {
      "low": 23666,
      "high": 0
    },
    "labels": ["Group"],
    "properties": {
      "name": "IMGroup",
      "description": "Master group for individual members",
      "since": {
        "low": -661343024,
        "high": 374
      },
      "status": "active"
    }
  }],
  "_fieldLookup": {
    "voters": 0,
    "groups": 1
  }
}, {
  "keys": ["voters", "groups"],
  "length": 2,
  "_fields": [{
    "identity": {
      "low": 15,
      "high": 0
    },
    "labels": ["Voter"],
    "properties": {
      "name_key": "ALEXANDERDWAYNEBARRINGTON",
      "occupation": "AIR CON TECH",
      "gender": "MR",
      "street": "3 1/2 JOHNSON TERRACE",
      "name": "DWAYNE ALEXANDER",
      "last_name": "ALEXANDER",
      "middle_name": "BARRINGTON",
      "postal_code": "KGN 02",
      "first_name": "DWAYNE"
    }
  }, {
    "identity": {
      "low": 23666,
      "high": 0
    },
    "labels": ["Group"],
    "properties": {
      "name": "IMGroup",
      "description": "Master group for individual members",
      "since": {
        "low": -661343024,
        "high": 374
      },
      "status": "active"
    }
  }],
  "_fieldLookup": {
    "voters": 0,
    "groups": 1
  }
}, {
  "keys": ["voters", "groups"],
  "length": 2,
  "_fields": [{
    "identity": {
      "low": 16,
      "high": 0
    },
    "labels": ["Voter"],
    "properties": {
      "name_key": "ALLENANNETTEUNA",
      "occupation": "COSMETOLOGIST",
      "gender": "MISS",
      "street": "28 CARNARVAN STREET",
      "name": "ANNETTE ALLEN",
      "last_name": "ALLEN",
      "middle_name": "UNA",
      "postal_code": "KGN 02",
      "first_name": "ANNETTE"
    }
  }]
}]

console.log(records[0].voters?.properties)
CRice
  • 29,968
  • 4
  • 57
  • 70
  • ..On the contrary I am not familiar with python at all. I use Javascript. Your solution gave me a TypeError: Cannot read property 'voters' of undefined error. – MichaelE Jan 12 '21 at 01:42