Convert SFSObject to js normal object?

Post here your questions about the HTML5 / JavaScript for SFS2X

Moderators: Lapo, Bax

User avatar
khanhdev
Posts: 17
Joined: 24 Mar 2017, 11:32

Convert SFSObject to js normal object?

Postby khanhdev » 20 Jun 2017, 09:39

In version 1.7.2, how to convert sfsobject to normal jsobject. I want it to be easier to see than when logging on the browser.
Sorry for my English :)
User avatar
Lapo
Site Admin
Posts: 20049
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: Convert SFSObject to js normal object?

Postby Lapo » 20 Jun 2017, 09:49

Hi,
there's no need to convert an SFSObject. You can dump it's content to the console, via:

Code: Select all

trace(sfsobject.getDump())


This will show all the content, including nested SFSObjects/Arrays

cheers
Lapo
--
gotoAndPlay()
...addicted to flash games
User avatar
khanhdev
Posts: 17
Joined: 24 Mar 2017, 11:32

Re: Convert SFSObject to js normal object?

Postby khanhdev » 20 Jun 2017, 10:19

Lapo wrote:Hi,
there's no need to convert an SFSObject. You can dump it's content to the console, via:

Code: Select all

trace(sfsobject.getDump())


This will show all the content, including nested SFSObjects/Arrays

cheers


thank Lapo :D
User avatar
marsoups
Posts: 157
Joined: 14 Apr 2008, 03:30

Re: Convert SFSObject to js normal object?

Postby marsoups » 03 Apr 2018, 22:01

There is one situation where this would be helpful to be able to dump to a JS object - and that is using a JS library such as angular JS, where the SFSArray and SFSObject type don't work well when looping and accessing properties.

I've tried to make a few functions to loop through on each object in an array, but I'm a little bit stuck on how I can tell what type of property and how to unravel it - i.e. if this is an SFSArray, we need to loop through it to access each SFSObject in it, if it is an array of any type, then unravel it accordingly. My question is : is it possible to access the type of element in a get ?

Thanks!
User avatar
Lapo
Site Admin
Posts: 20049
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: Convert SFSObject to js normal object?

Postby Lapo » 04 Apr 2018, 08:50

marsoups wrote:There is one situation where this would be helpful to be able to dump to a JS object - and that is using a JS library such as angular JS, where the SFSArray and SFSObject type don't work well when looping and accessing properties.

Hi,
what do you mean by "dump to a JS object" exactly?
Converting the SFSObject into a regular JS Object?

I think it would be possible but it also sounds like a waste of resources, especially because SFSObject/Arrays are usually nested and thus you would need to recursively traverse the whole structure to recreate it in another format.
I am not sure what would be the advantage of that extra work?

In general I would not recommend it.
Lapo

--

gotoAndPlay()

...addicted to flash games
User avatar
marsoups
Posts: 157
Joined: 14 Apr 2008, 03:30

Re: Convert SFSObject to js normal object?

Postby marsoups » 04 Apr 2018, 16:23

Hey Lapo,

In Angular JS, you can for example produce a table by simply pointing a row to a JS array. Angular JS will handle the display part of updating the content. So in this situation it's efficient to produce native JS objects.

Here is a bit of code that I wrote that has helped me with this tasked (tested only minimally):

Code: Select all

var convertSFSArrayToArray = function (SFSArray) {
            var newArray = [];
            for(var i = 0 ; i < SFSArray.size(); i++) {
                var obj = convertSFSObjectToObject(SFSArray.get(i));
                newArray.push(obj);
            }
            return newArray;
        }
        var convertSFSObjectToObject = function (SFSObject) {
            var obj = {}
            var keyArray = SFSObject.getKeysArray();
            for(var j = 0; j < keyArray.length; j++) {
                if(typeof SFSObject.get(keyArray[j]) == "object") {
                    obj[keyArray[j]] = convertSFSArrayToArray(SFSObject.get(keyArray[j]));
                } else {
                    obj[keyArray[j]] = SFSObject.get(keyArray[j]);
                }
            }
            return obj;
        }
User avatar
Lapo
Site Admin
Posts: 20049
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: Convert SFSObject to js normal object?

Postby Lapo » 05 Apr 2018, 07:49

The code seems correct.
If you want to improve the type detection instead of using "typeof" you could use "instanceof"

Example:

Code: Select all

if (SFSObject.get(keyArray[j]) instanceof SFS2X.SFSObject)
{
    obj[keyArray[j]] = convertSFSArrayToArray(SFSObject.get(keyArray[j]));
} else {
    obj[keyArray[j]] = SFSObject.get(keyArray[j]);
}


hope it helps
Lapo

--

gotoAndPlay()

...addicted to flash games

Return to “SFS2X HTML5 / JavaScript API”

Who is online

Users browsing this forum: No registered users and 1 guest