JavaScript vertoon voorwerpe
Hoe om JavaScript-voorwerpe te vertoon?
Deur 'n JavaScript-objek te vertoon, sal [object Object] uitstuur .
Voorbeeld
const person = {
name: "John",
age: 30,
city: "New York"
};
document.getElementById("demo").innerHTML = person;
Sommige algemene oplossings om JavaScript-voorwerpe te vertoon is:
- Vertoon die voorwerpeienskappe op naam
- Vertoon die voorwerpeienskappe in 'n lus
- Vertoon die objek met behulp van Object.values()
- Vertoon die objek met behulp van JSON.stringify()
Vertoon voorwerpeienskappe
Die eienskappe van 'n voorwerp kan as 'n string vertoon word:
Voorbeeld
const person = {
name: "John",
age: 30,
city: "New York"
};
document.getElementById("demo").innerHTML =
person.name + "," + person.age + "," + person.city;
Vertoon die voorwerp in 'n lus
Die eienskappe van 'n voorwerp kan in 'n lus versamel word:
Voorbeeld
const person = {
name: "John",
age: 30,
city: "New York"
};
let txt = "";
for (let x in person) {
txt += person[x] + " ";
};
document.getElementById("demo").innerHTML = txt;
Jy moet persoon[x] in die lus gebruik.
person.x sal nie werk nie (Omdat x 'n veranderlike is).
Gebruik Object.values()
Enige JavaScript-objek kan omgeskakel word na 'n skikking deur gebruik te maak van Object.values()
:
const person = {
name: "John",
age: 30,
city: "New York"
};
const myArray = Object.values(person);
myArray
is nou 'n JavaScript-skikking, gereed om vertoon te word:
Voorbeeld
const person = {
name: "John",
age: 30,
city: "New York"
};
const myArray = Object.values(person);
document.getElementById("demo").innerHTML = myArray;
Object.values()
word sedert 2016 in alle groot blaaiers ondersteun.
54 (2016) | 14 (2016) | 47 (2016) | 10 (2016) | 41 (2016) |
Gebruik JSON.stringify()
Enige JavaScript-objek kan gestring word (omgeskakel na 'n string) met die JavaScript-funksie
JSON.stringify()
:
const person = {
name: "John",
age: 30,
city: "New York"
};
let myString = JSON.stringify(person);
myString
is nou 'n JavaScript-string, gereed om vertoon te word:
Voorbeeld
const person = {
name: "John",
age: 30,
city: "New York"
};
let myString = JSON.stringify(person);
document.getElementById("demo").innerHTML = myString;
Die resultaat sal 'n string wees na die JSON-notasie:
{"name":"John","age":50,"city":"New York"}
JSON.stringify()
is ingesluit in JavaScript en word in alle groot blaaiers ondersteun.
Verbind datums
JSON.stringify
skakel datums om in stringe:
Voorbeeld
const person = {
name: "John",
today: new Date()
};
let myString = JSON.stringify(person);
document.getElementById("demo").innerHTML = myString;
Verbind funksies
JSON.stringify
sal nie funksies stringify nie:
Voorbeeld
const person = {
name: "John",
age: function () {return 30;}
};
let myString = JSON.stringify(person);
document.getElementById("demo").innerHTML = myString;
Dit kan "reggemaak" word as jy die funksies in stringe omskakel voor stringifying.
Voorbeeld
const person = {
name: "John",
age: function () {return 30;}
};
person.age = person.age.toString();
let myString = JSON.stringify(person);
document.getElementById("demo").innerHTML = myString;
Stringify Skikkings
Dit is ook moontlik om JavaScript-skikkings te stringify:
Voorbeeld
const arr = ["John", "Peter", "Sally", "Jane"];
let myString = JSON.stringify(arr);
document.getElementById("demo").innerHTML = myString;
Die resultaat sal 'n string wees na die JSON-notasie:
["John","Peter","Sally","Jane"]