A brief talk on the key value of JS to key and value

Source: Internet
Author: User

> Last night inadvertently saw a code similar to the following structure of the value of the problem, aroused my interest, took a little time to write a demo to share with you ...

var obj = [
{"*": {"name": "Jyjin", "Age": 20}},
{"+": {"name": "Jyjin", "Age": 21}}
];

var obj1 = {
"": {"name": "Jyjin", "Age": 22},
"All": {"name": "Jyjin", "Age": 23}
};


> Careful observation of the structure above, you will find that the above two structures and the traditional JSON or array data structure some differences, 2011, 2012, 2013, 2014 of these key pairs in the "key" place also stores data, this way may greatly reduce the file storage size. So I'm excited about the value, so let's talk about how it's going to look.

> #1. Key-value pair understanding techniques
> Regardless of the above data structure, arrays or objects, to distinguish between key value pairs, my technique is to look for a colon first. * The colon to the left is key, the right is value, no colon the default key is incremented from 0, and the display value is value*.
> So it's not hard to understand

Obj's key value
0 {"*": {"name": "Jyjin", "Age": 20}}
1 {"": {"name": "Jyjin", "Age": 21}}

Obj1 's key value
{"Name": "Jyjin", "Age": 22}
"Name": "Jyjin", "Age": 23}

> clearly found that obj's value subdivision key and value is the OBJ1 key, value partition structure

> #2. Value of key-value pairs
1.for-in--------------------------------
The correct way to understand for-in is for (var key in obj), where obj is a JS object or an array, and I use key to make it clearer that it iterates over the key value instead of the value. So the value of traversing the array with for-in is an array subscript, and the result of traversing the JS object is the object property name.

for (var key in obj) {
Console.log (key);
}
Console output:
0
1

for (var key in obj1) {
Console.log (key);
}
Console output:
2013
2014

2.$.each ()--------------------------------
The $.each () method in jquery is a good way to take a key value pair, which can be understood as a code $.each (Obj,function (key,value) {...}), where obj can be a JS object or an array, and the key and value correspond to each phase Should be the name value, key value. Here is a very bad habit is written into function (I,data) Here is easy to mistakenly understand that I represents the array subscript, data element value, in fact, this understanding is completely wrong!

$.each (Obj,function (key,value) {
Console.log (key+ ":" +value);
});
Console output:
0:[object Object]
1:[object Object]

    

$.each (Obj1,function (key,value) {
Console.log (key+ ":" +value);
});
Console output:
2013:[object Object]
2014:[object Object]

It can be seen that this is not only taken out of the key for-in can be removed, but also the value out, but need to further the value of the key value of values ...

3. Use for-in and $-each to remove important data information from obj: year, name, age
$.each (Obj,function (objkey,objvalue) {
$.each (Objvalue,function (key,value) {
Console.log (key+ ":" +value.name+ ":" +value.age);
});
});
$.each (Obj1,function (key,value) {
Console.log (key+ ":" +value.name+ ":" +value.age);
});

Console output:
2011:jyjin:20
2012:jyjin:21
2013:jyjin:22
2014:jyjin:23


for (var objkey in obj) {
$.each (Obj[objkey],function (key,value) {
Console.log (key+ ":" +value.name+ ":" +value.age);
});
}
for (Var obj1key in obj1) {
Console.log (Obj1key)
$.each (Obj1[obj1key],function (key,value) {
Console.log (":" +value);
});
}

Console output:
2011:jyjin:20
2012:jyjin:21
2013

: Jyjin

: 22
2014
: Jyjin
: 23

A brief talk on the key value of JS to key and value

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.