Display Object of JavaScript

如何显示 JavaScript 对象?

显示 JavaScript 对象将输出 [object Object].

Example

const person = {
  name: "Bill",
  age: 19,
  city: "Seattle"
};
document.getElementById("demo").innerHTML = person;

Try it yourself

sabonin hanciwa jine jine JavaScript ne za a yin girmuwa:

  • girmuwa atiranci jine jine a kan sunan
  • girmuwa atiranci jine jine a kan kwarin
  • sake Object.values() girmuwa jine jine
  • sake JSON.stringify() girmuwa jine jine

girmuwa atiranci jine jine

atiranci jine jine za a yin girmuwa za a yin girmuwa:

Example

const person = {
  name: "Bill",
  age: 19,
  city: "Seattle"
};
document.getElementById("demo").innerHTML =
person.name + "," + person.age + "," + person.city;

Try it yourself

girmuwa jine atiranci jine a kan kwarin

za a girmuwa atiranci jine jine a kan kwarin:

Example

const person = {
  name: "Bill",
  age: 19,
  city: "Seattle"
};
let txt = "";
for (let x in person) {
txt += person[x] + " ";
};
document.getElementById("demo").innerHTML = txt;

Try it yourself

za a kari ka wuce person[x] a kan kwarin.

person.x za a kariwa (wannan ne sabonin kuma x ne dattiya).

sake Object.values()

sake Object.values(), ko'ina jine JavaScript ne za a samu dattiya gamu:

const person = {
  name: "Bill",
  age: 19,
  city: "Seattle"
};
const myArray = Object.values(person);

myArray wannan ne dattiya JavaScript, za a yin girmuwa:

Example

const person = {
  name: "Bill",
  age: 19,
  city: "Seattle"
};
const myArray = Object.values(person);
document.getElementById("demo").innerHTML = myArray;

Try it yourself

daga 2016, dukuna kware fada kawancen suna a samu Object.values().

Chrome IE Firefox Safari Opera
54 (2016) 14 (2016) 47 (2016) 10 (2016) 41 (2016)

sake JSON.stringify() gamu

ko'ina jine JavaScript ne za a samu dattiya gamu jine funtaction JSON.stringify() girmuwa dattiya (sake a hanyar dattiya):

const person = {
  name: "Bill",
  age: 19,
  city: "Seattle"
};
let myString = JSON.stringify(person);

myString wannan ne dattiya JavaScript, za a yin girmuwa:

Example

const person = {
  name: "Bill",
  age: 19,
  city: "Seattle"
};
let myString = JSON.stringify(person);
document.getElementById("demo").innerHTML = myString;

Try it yourself

The result will be a string that follows the JSON notation:

{"name":"Bill","age":19,"city":"Seattle"}

JSON.stringify() wani a bace JavaScript, dukuna kware fada kawancen

girmuwa dattiya dattiya

JSON.stringify sake girmuwa dattiya a hanyar kalmu

Example

const person = {
  name: "Bill",
  today: new Date()
};
let myString = JSON.stringify(person);
document.getElementById("demo").innerHTML = myString;

Try it yourself

Function stringification

JSON.stringify Functions will not be stringified:

Example

const person = {
  name: "Bill",
  age: function () {return 19;}
};
let myString = JSON.stringify(person);
document.getElementById("demo").innerHTML = myString;

Try it yourself

If the function is converted to a string before stringification, it can be "fixed".

Example

const person = {
  name: "Bill",
  age: function () {return 19;}
};
person.age = person.age.toString();
let myString = JSON.stringify(person);
document.getElementById("demo").innerHTML = myString;

Try it yourself

Array stringification

You can also stringify a JavaScript array:

Example

const arr = ["Bill", "Steve", "Elon", "David"];
let myString = JSON.stringify(arr);
document.getElementById("demo").innerHTML = myString;

Try it yourself

The result will be a string that follows the JSON notation:

["Bill","Steve","Elon","David"]