0

I have a json in the format:

"json": {
    "schema": {
      "type": "object",
      "title": "My Table",
      "properties": {
        "table-1659555454697": {
          "type": "array",
          "items": {
            "type": "object",
            "properties": {
              "inputTest": {
                "type": "string"
              },
              "columnTwo": {
                "type": "string"
              }
            },
            "required": [
              "inputTest",
              "columnTwo"
            ]
          }
        }
      },
    },

I can obtain a reference to the "table-1659555454697" object with:

console.log(this.jsf.schema.properties);

What I'm struggling with is how to obtain a reference from that element's children to the "required" array's length. Assuming I cannot reference "table-1659555454697" by name since its dynamically generated.

I've tried:

 console.log(this.jsf.schema.properties[0].items[0].required.length);

But I get undefined error

js-newb
  • 421
  • 6
  • 16
  • 1
    `properties` is an object, not an array so you can't access it by index. You would need to access the Object.values if you don't know the property name. `Object.values(this.jsf.schema.properties)[0].required.length` – pilchard Aug 04 '22 at 22:50
  • You have the object; you access its properties like any other object. Are you instead asking how to iterate over the keys and/or values of the “properties” object? – Dave Newton Aug 04 '22 at 22:52
  • I need to determine if the object represented by "table-1659555454697" contains a "required" array – js-newb Aug 04 '22 at 22:56
  • @DaveNewton, essentially yes. – js-newb Aug 04 '22 at 22:57

2 Answers2

0

You probably want Object.keys, Object.values, or Object.entries. They provide an array of, respectively, the object's keys, the object's values, and the object's key-value pairs (as an array of [key, value] arrays).

const propertyNames = Object.keys(json.schema.properties) // ["table-1659555454697"]
const propertyValues = Object.values(json.schema.properties) // [{ type: "array", items: { ... }]
const propertyEntries = Object.entries(json.schema.properties)
// [ ["table-1659555454697", [{ type: "array", items: { ... }] ]

So we could do what you want like this:

const requiredLength = Object.values(json.schema.properties)[0].items.required.length
RJ Felix
  • 138
  • 1
  • 5
0

You can reference table-1659555454697 by doing

Object.keys(this.jsf.schema.properties)[0]
Gussa
  • 21
  • 2