How to remove the function type elements in an object-JSON object learning, function-json

Source: Internet
Author: User
Tags tojson

How to remove the function type elements in an object-JSON object learning, function-json

[Problem] when an object contains a function and wants to remove it, how can it be simple?

[Example]

For example, if an object is:

var  o = {    a: 1,    b: 'xx',    c: function(){},    d: true}

What should I do if I want to remove the object of Type o. c ~~

[Solution]

The general idea is to use the for-in loop and remove type as function. In fact, a simpler method is to use JSON. stringify (o), so that you can directly remove the function and then use JSON. parse () converts the object back. The instance code is as follows:

JSON.parse(JSON.stringify(o))
As a result, you can try it on your own.


[Deep Learning]

JSON object:

Object Detection: If (! Window. JSON)

Browser support: IE:> = supported in standard ie8 mode, not supported in hybrid mode or <ie8 browser, supported by mainstream modern browsers

Method:

1. JSON. stringify (value [, replacer,Space])

@ Param {Object | Array} value is required. The JavaScript value to be converted (usually an Object or Array ).

@ Param {Function} replacer (optional) Function or array used for conversion results

@ Param {String | Number} space (optional) add indentation, spaces, and line breaks to the returned JSON text to make it easier to read

Note:

If the value has the toJSON method, the JSON. stringify function uses the return value of this method.

If the return value of the toJSON method is undefined, no member is converted.

This allows the object to determine its own j son representation.

Values that do not have a JSON representation, such as undefined, will not be converted. These values are discarded in the object. In the array, these values are replaced with null.

Execution sequence:

During the serialization process, if the value parameter pair should have the toJSON method, JSON. stringify will first call the toJSON method.

If this method does not exist, the original value is used.

Next, if the replacer parameter is provided, the value (original value or toJSON return value) will be replaced with the return value of the replacer parameter.

Finally, add spaces to the value based ON the optional space parameter to generate the final js on text.


2. JSON. parse (text [, receiver])

@ Param {String} text jsonString

@ Param {Function} extends Er (optional. A function of the conversion result.

This function will be called for each member of the object. If a member contains a nested object, the nested object is converted before the parent object.

For each member, the following conditions are generated:

If reviver returns a valid value, the member value is replaced with the converted value.

If reviver returns the same value it receives, the member value is not modified.

If reviver returns null or undefined, the Member is deleted.


Unsupported alternatives:

1. The window. eval method parses jsonString, that is, it replaces the json. parse method.

2. Use json2.js for conversion


[References]

Http://msdn.microsoft.com/zh-cn/library/cc836458 (v = vs.94). aspx

Http://msdn.microsoft.com/zh-cn/library/cc836459 (v = vs.94). aspx

Http://msdn.microsoft.com/zh-cn/library/cc836466 (v = vs.94). aspx



What is the options parameter of function (options) in the init attribute of the json object methods? No real parameters found from the entire page

Your understanding is wrong. This is JS Code. In fact, the built-in JS object is very small. Before you learn a programming method for a framework, measure the test taker's knowledge about the programming framework and JS features. You need to better understand the above Code under the object's JS.
(1) $. Extended (option, customoptions), from the Code itself, see, $. Extension is not defined here, so you cannot answer what it actually does. But the commonly used programming framework, such as jQuery, is mainly used to implement the attributes and methods of objects from customoptions to options objects. What you call "expansion ".

(2) JS type is weak or has no language restrictions. Therefore, this parameter can be an object of any type. Generally, it extends the composite object, including attributes and functions, it is normal for you to see that it is an object of the "extended" function or other features of "extended.
(3) select your code to define the object (when the object is defined by {} object), rather than JSON, JSON refers to the string expression of the object, and there is no string JSON format, you need to learn about the different definitions of JS Code and program compiling JSON strings. Therefore, through the definition of this function, but there is no JSON here.

How to delete elements in json data

Delete aa. bb. prop;

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.