Εμφάνιση Οντοτήτων στο JavaScript
- Προηγούμενη σελίδα Μέθοδοι αντικειμένων JS
- Επόμενη σελίδα Αναγνώστης αντικειμένων JS
Πώς εμφανίζεται το αντικείμενο JavaScript;
Δείχνει ότι η εμφάνιση του JavaScript αντικειμένου θα εκτυπώσει [object Object].
Παράδειγμα
const person = { name: "Bill", age: 19, city: "Seattle" }; document.getElementById("demo").innerHTML = person;
Some common solutions to display JavaScript objects are:
- Display object properties by name
- Loop to display object properties
- Use Object.values() to display the object
- Use JSON.stringify() to display the object
Display object properties
The properties of the object can be displayed as strings:
Παράδειγμα
const person = { name: "Bill", age: 19, city: "Seattle" }; document.getElementById("demo").innerHTML = person.name + "," + person.age + "," + person.city;
Display the object in the loop
You can collect the properties of the object in the loop:
Παράδειγμα
const person = { name: "Bill", age: 19, city: "Seattle" }; let txt = ""; for (let x in person) { txt += person[x] + " "; }; document.getElementById("demo").innerHTML = txt;
You must use person[x] in the loop.
person.x will not work (because x is a variable).
Use Object.values()
By using Object.values()
, any JavaScript object can be converted to an array:
const person = { name: "Bill", age: 19, city: "Seattle" }; const myArray = Object.values(person);
myArray
Now it is a JavaScript array, which can be displayed:
Παράδειγμα
const person = { name: "Bill", age: 19, city: "Seattle" }; const myArray = Object.values(person); document.getElementById("demo").innerHTML = myArray;
Since 2016, all major browsers have supported Object.values()
.
Chrome | IE | Firefox | Safari | Opera |
---|---|---|---|---|
54 (2016) | 14 (2016) | 47 (2016) | 10 (2016) | 41 (2016) |
Use JSON.stringify()
Any JavaScript object can use the JavaScript function JSON.stringify()
Perform stringification (convert to string):
const person = { name: "Bill", age: 19, city: "Seattle" }; let myString = JSON.stringify(person);
myString
Now it is a JavaScript string, which can be displayed:
Παράδειγμα
const person = { name: "Bill", age: 19, city: "Seattle" }; let myString = JSON.stringify(person); document.getElementById("demo").innerHTML = myString;
Οι αποτελέσματα θα είναι μια αλφαβητική αλυσίδα που ακολουθεί το πρότυπο JSON:
{"name":"Bill","age":19,"city":"Seattle"}
JSON.stringify()
Included in JavaScript, all mainstream browsers support it.
Date stringification
JSON.stringify
Convert the date to a string:
Παράδειγμα
const person = { name: "Bill", today: new Date() }; let myString = JSON.stringify(person); document.getElementById("demo").innerHTML = myString;
Μετατροπή συναρτήσεων σε αλφαβητικές
JSON.stringify
Δεν θα μετατραπούν σε αλφαβητική αλυσίδα οι συναρτήσεις:
Παράδειγμα
const person = { name: "Bill", age: function () {return 19;} }; let myString = JSON.stringify(person); document.getElementById("demo").innerHTML = myString;
Αν μετατρέψετε τις συναρτήσεις σε αλφαβητικές πριν από τη μετατροπή σε αλφαβητική αλυσίδα, μπορεί να είναι "σταθερές".
Παράδειγμα
const person = { name: "Bill", age: function () {return 19;} }; person.age = person.age.toString(); let myString = JSON.stringify(person); document.getElementById("demo").innerHTML = myString;
Μετατροπή αλυσίδων σε αλφαβητικές
Μπορείτε επίσης να μετατρέψετε αλυσίδες JavaScript σε αλφαβητικές αλυσίδες:
Παράδειγμα
const arr = ["Bill", "Steve", "Elon", "David"]; let myString = JSON.stringify(arr); document.getElementById("demo").innerHTML = myString;
Οι αποτελέσματα θα είναι μια αλφαβητική αλυσίδα που ακολουθεί το πρότυπο JSON:
["Bill","Steve","Elon","David"]
- Προηγούμενη σελίδα Μέθοδοι αντικειμένων JS
- Επόμενη σελίδα Αναγνώστης αντικειμένων JS