Make json from array of objects javascript

user2997779

I have a javascript object with a lot of attributes and methods, I want it to be sent to a php file. For this, I want to transform it to Json data.

But I just can`t understand how should I use json.stringify to do this, because of the complex object's class.

The objects looks like this. I have an array of objects that I have to sent over ajax.

Also, this class has array of other objects as attributes, and a bunch of other methods.

var PhotoFile = function(clientFileHandle){
     PhotoFile.count = PhotoFile.count  + 1;
        this.specificClass = "no-" + PhotoFile.count;
        this.checkbox = null;
        this.attributes = [];
        this.file = clientFileHandle;
        this.fileExtension = null;
        //meta data
        this.meta = null;
        this.orientation = null;
        this.oDateTime = null;
        this.maxWidth = 150;
        this.maxHeight = 100;
        //raw data
        this.imgData = null;
        this.imgDataWidth = null;
        this.imgDataHeight = null;
        this.checkSum1 = null;
        this.checkSum2 = null;
        //DOM stuff
        this.domElement = null;
        this.imgElement = null;
        this.loadProgressBar = null;
        this.uploadProgressBar = null;
        this.imageContainer = null;
        this.attributeContainer = null;
        this.indexInGlobalArray = -1;
        //flags
        this.metaLoaded = false;
        this.startedLoading = false;
        this.finishedLoading = false;
        this.needsUploading = true;

        this.imageDisplayed = false;
        //listeners
        this.onFinishedLoading = function () {};
        this.onFinishedUploading = function () {console.log('Called default end '+this.file.name)};
    ..... plus other methods.
    }
plalx

You could create a function on your object that returns a serializable representation of your object.

E.g.

function SomeObject() {
    this.serializeThis = 'serializeThis';
    this.dontSerializeThis = 'dontSerializeThis';
}

SomeObject.prototype.toSerializable = function () {
    //You can use a generic solution like below
    return subsetOf(this, ['serializeThis']);

    //Or a hard-coded version
    // return { serializeThis: this.serializeThis };
};

//The generic property extraction algorithm would need to be more complex
//to deep-filter objects.
function subsetOf(obj, props) {
    return (props || []).reduce(function (subset, prop) {
        subset[prop] = obj[prop];
        return subset;
    }, {});
}


var o = new SomeObject();

JSON.stringify(o.toSerializable()); //{"serializeThis":"serializeThis"}

Note that using a generic property extractor algorithm would force you to leak implementation details and therefore, violate encapsulation so although it might be shorter to implement a solution using this method, it might not be the best way in some cases.

However, one thing that can usually be done to limit internals leakage is to implement property getters.

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

Pluck properties from JSON into array of objects in JavaScript

分類Dev

How to change json data format from array to array of objects with javascript?

分類Dev

Combining same objects in json array javascript

分類Dev

How to remove duplicates objects from array in javascript?

分類Dev

Javascript: extract objects from inside an array of objects, and put them into a new array

分類Dev

javascript return property value from nested array of objects based on condition

分類Dev

javascript array of objects - redistribute

分類Dev

Javascript, Array of Objects?

分類Dev

transform an array of json objects to array

分類Dev

How to get json value from json array using javascript

分類Dev

How to return a value from a function , use that value to make a math formula and push the solution(key/value) to an array of objects?

分類Dev

how would you make it so that it grabs a random number and uses it to grab objects from an array

分類Dev

Cartesian product of array of objects in javascript

分類Dev

Not able to transform array of objects in JavaScript

分類Dev

filter two array of objects in javascript

分類Dev

How to create an array of objects in JavaScript?

分類Dev

Sort by multiple objects in a array in Javascript

分類Dev

Destruct nested array of objects javascript

分類Dev

How to delegate to an array of objects in JavaScript

分類Dev

How to make array of inherited objects - Java

分類Dev

How to make IBOutlets out of an array of objects?

分類Dev

How to make an array of multiple nested objects?

分類Dev

Android: How to parse JSON Array of Array of objects

分類Dev

Accessing Array elements in array of Objects - JavaScript

分類Dev

Getting array of objects from CodableAlamofire

分類Dev

Check meter from array of objects

分類Dev

Removing an object from an array of objects

分類Dev

how to structure a filesystem-like JSON from an array of objects with an absolute path

分類Dev

Is there any way in MariaDB to search for less than value from array of json objects

Related 関連記事

  1. 1

    Pluck properties from JSON into array of objects in JavaScript

  2. 2

    How to change json data format from array to array of objects with javascript?

  3. 3

    Combining same objects in json array javascript

  4. 4

    How to remove duplicates objects from array in javascript?

  5. 5

    Javascript: extract objects from inside an array of objects, and put them into a new array

  6. 6

    javascript return property value from nested array of objects based on condition

  7. 7

    javascript array of objects - redistribute

  8. 8

    Javascript, Array of Objects?

  9. 9

    transform an array of json objects to array

  10. 10

    How to get json value from json array using javascript

  11. 11

    How to return a value from a function , use that value to make a math formula and push the solution(key/value) to an array of objects?

  12. 12

    how would you make it so that it grabs a random number and uses it to grab objects from an array

  13. 13

    Cartesian product of array of objects in javascript

  14. 14

    Not able to transform array of objects in JavaScript

  15. 15

    filter two array of objects in javascript

  16. 16

    How to create an array of objects in JavaScript?

  17. 17

    Sort by multiple objects in a array in Javascript

  18. 18

    Destruct nested array of objects javascript

  19. 19

    How to delegate to an array of objects in JavaScript

  20. 20

    How to make array of inherited objects - Java

  21. 21

    How to make IBOutlets out of an array of objects?

  22. 22

    How to make an array of multiple nested objects?

  23. 23

    Android: How to parse JSON Array of Array of objects

  24. 24

    Accessing Array elements in array of Objects - JavaScript

  25. 25

    Getting array of objects from CodableAlamofire

  26. 26

    Check meter from array of objects

  27. 27

    Removing an object from an array of objects

  28. 28

    how to structure a filesystem-like JSON from an array of objects with an absolute path

  29. 29

    Is there any way in MariaDB to search for less than value from array of json objects

ホットタグ

アーカイブ