0

I don't have much experience in Javascript, so I ask for help from you. I have array of multidimensional objects represented like this:

arr = {
    "Ob1": {
        "id": 1,
        "name": "Ob1",
        "properties": {
            "attName": "A1",
            "attType": "string",
            "attOccurance": "minOccurs="1""
        },
        "Ob2": {
            "id": 101,
            "name": "Ob2",
            "properties": {
                "attName": "B1",
                "attType": "string",
                "attOccurance": "minOccurs="1""
                },
            "Ob3": {
                "id": 10001,
                "name": "Ob3",
                "properties": {
                    "attName": "C1",
                    "attType": "string",
                    "attOccurance": "minOccurs="1""
                    },
            },
            "Ob4": {
                "id": 10002,
                "name": "Ob4",
                "properties": {
                    "attName": "C2",
                    "attType": "string",
                    "attOccurance": "minOccurs="1""
                    },
            },
        },
        "Ob5": {
            "id": 102,
            "name": "Ob5",
            "properties": {
                "attName": "B2",
                "attType": "string",
                "attOccurance": "minOccurs="1""
            },
            "Ob6": {
                "id": 10003,
                "name": "Ob6",
                "properties": {
                    "attName": "C3",
                    "attType": "string",
                    "attOccurance": "minOccurs="1""
                },
            },
        },
    }
    "Ob7": {
        "id": 2,
        "name": "Ob7",
        "properties": {
            "attName": "A2",
            "attType": "string",
            "attOccurance": "minOccurs="1""
        },
    },
}

How can I recursively loop through this array and display results while keeping track of the parent object in case I have to return back 2 levels, such as in this case with "Ob7"?

The results have to be displayed in xml schema way(If an object has children is complexType, if not, simpleType:

<xs:complexType name="A1" type="string" minOccurs="1">
   <xs:complexType name="B1" type="string" minOccurs="1">
      <xs:simpleType name="C1" type="string" minOccurs="1"/>
      <xs:simpleType name="C2" type="string" minOccurs="1"/>
   </complexType>
   <xs:complexType name="B2" type="string" minOccurs="1">
      <xs:simpleType name="C3" type="string" minOccurs="1"/>
   </complexType>
</complexType>
<xs:simpleType name="A2" type="string" minOccurs="1"/>

Also, I can't use already existing libraries for xml schema, the mapping has to be done in the code.

I already tried something like this for the recursion, but It's not working:

function looping(arr, key) {
    if( typeof(arr[key]) == "object" && Object.keys(arr).length >= 1 ) {
        val = arr[key];
        for( k in value ) {
            looping(arr[key], k);
        }
    } else {
        var temp = arr[key];
    }
}

for(key in arr) {
    looping( arr, key);
}
unknownDev
  • 31
  • 7
  • how do you get an array literal with properties? – Nina Scholz Nov 07 '18 at 12:23
  • This is just part of the array, but it's enough to make a point. The array is filled with objects that are exported from BPM tool diagram. – unknownDev Nov 07 '18 at 12:27
  • `arr` has invalid syntax – barbsan Nov 07 '18 at 12:47
  • arr is not either an array nor an object, you either have an array of objects or an object with multiple properties (or a map which is pretty much the same). – Bargros Nov 07 '18 at 13:45
  • It's not an array, it's an object. There are some invalid unescaped double quotes in strings, and some extra trailing commas (which may or may not be valid, depending on the version of ECMAScript in use). You should look at the answers to https://stackoverflow.com/questions/11922383/access-process-nested-objects-arrays-or-json – Heretic Monkey Nov 07 '18 at 14:18
  • Please add what about your code is "not working". In other words, what's the output you wanted and and what you got instead. For instance, in the code sample you've shown, you're not returning anything, so not much will happen. You're also acting on undeclared variables, which should show up as errors in your console. – Heretic Monkey Nov 07 '18 at 14:20

1 Answers1

1

Assuming that arr is a array of objects, which looks like this:

let arr = [
    {...},
    {...}
]

you need to write a function which will iterate through each object in the array and call a recursive function which will check if object is a complex object or a simple object.

If it's complex object than you should call that function again, recursively for the object, if not return simple object.

You don't need to write some additional code to get back 2 levels or something, recursive process will get there naturally.

I'm not sure what the output you want, either a string which contains valid xml or an actual xml object, so here's the jsfiddle which takes an array of objects from you example, converts it to string and then to xml object using DOMParser (assuming you want to run this code in a browser):

https://jsfiddle.net/tara5/qk79Lcaz/

Check out the browser console to see the output.

You can modify functions to achieve a desired effect.

SciFiThief
  • 586
  • 5
  • 9
  • Thank you for the instructions. I'm using BPM tool(ARIS) to run the script. When I execute the code, I get an errir message "Cannot find function reduce in object". Do you know what might be? – unknownDev Nov 07 '18 at 15:24
  • Objects do not have reduce method. That means that you actually have an object as a root and not array of objects. You need to modify code a bit so it would work with objects, not arrays. – SciFiThief Nov 07 '18 at 16:00
  • https://jsfiddle.net/tara5/qk79Lcaz/59/ this version works with objects. As I said, you may need to modify functions to return what BPM tool API accepts (string, xml object, something else). The goal of code example is to give you an idea how you can traverse the object. You can modify it now to achieve what you need. – SciFiThief Nov 07 '18 at 16:15
  • Thank you very much. But can I just ask you for the last part of the code, for (var extkey in object) { } Here the idea is to fill variable result with the xsd lines? – unknownDev Nov 08 '18 at 07:26
  • Oh, I shared an incomplete jsfiddle by accident. Here's the valid one https://jsfiddle.net/tara5/qk79Lcaz/60. And yes you're correct, the idea is to fill the result variable with xsd lines. – SciFiThief Nov 08 '18 at 09:55
  • Thanks, I will implement this and test in the tool – unknownDev Nov 08 '18 at 13:28
  • Can I ask you, what does it mean "return (key !== "properties")" in the code? – unknownDev Nov 09 '18 at 08:46
  • It means that we need to run a recursive function only for keys that are not a "property" key (like Ob1, Ob2, ...). We need to skip it because it doesn't contain complex or simple objects which should transformed and then included in output xml. – SciFiThief Nov 09 '18 at 11:05
  • The code start executing and I see that "str" variable is filling with the structure, but at one point I receive an error "Cannot find function hasOwnProperty in object..." Do you know what might be the issue or have suggestion? – unknownDev Nov 09 '18 at 14:56