JavaScript Object Properties Additions, deletions, JSON objects and string conversion methods, etc.

Source: Internet
Author: User

1: Adding object properties dynamically

var obj = new Object ();

Console.log (Obj.username);

Obj.username = "haha";

Console.log (Obj.username);

Undefined

haha

denoted by "[]". Written obj["username"] = "haha";

var obj = new Object ();

Console.log (Obj.username);

obj["username"] = "haha";

Console.log (Obj.username);

Undefined

haha

You can also define directly

var obj = {username: "haha", Password: "123"};

Console.log (Obj.username);

Console.log (Obj.password);

haha

123

2: Delete attribute , use Delete

var obj = new Object ();

obj["username"] = "haha";

Console.log (Obj.username);

Delete Obj.username;

Console.log (Obj.username);

haha

//undefined

3: Modify original property, add new attribute

var json = {

"Age": 24,

"Name": "Haha"

};

Modify The age value in Json

Json["age"] = 30;

Console.log (Json.age);

30

increase the sex value in Json

json["sex"] = "w";

Console.log (Json.sex);

W

4:json object and Json string Transfer

Json.parse (JSONSTR); you can convert a json string into a JSON object

var jsobj = {};
Jsobj.testarray = [n/a];
Jsobj.name = ' CSS3 ';
jsobj.date = ' 2017 ';
Console.log (Jsobj)
Console.log (typeof (Jsobj));
var str = json.stringify (jsobj);
var str1 = json.parse (str);
Console.log (str)
Console.log (typeof (str));
Console.log (STR1)
Console.log (typeof (STR1));
Object {testarray:array[3], name: "CSS3", Date: "2017"}
Object
{"Testarray": [[+], "name": "CSS3", "date": "2017"}
String
Object {testarray:array[3], name: "CSS3", Date: "2017"}
Object

Json.stringify (Jsonobj); you can convert a JSON object into a json string

var jsobj = {};
Jsobj.testarray = [n/a];
Jsobj.name = ' CSS3 ';
jsobj.date = ' 2017 ';
Console.log (Jsobj)
Console.log (typeof (Jsobj));
var str = json.stringify (jsobj);
Console.log (str)
Console.log (typeof (str));
Object {testarray:array[3], name: "CSS3", Date: "2017"}
Object
{"Testarray": [[+], "name": "CSS3", "date": "2017"}
String

JavaScript Object Properties Additions, deletions, JSON objects and string conversion methods, etc.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.