


Methods for adding, deleting and sorting data in json format_javascript skills
The examples in this article describe the adding, deleting and sorting methods of json format data. Share it with everyone for your reference, the details are as follows:
JS data format and json data format each have their own uses. Personally speaking, json is more useful, as js itself has more restrictions on arrays and objects.
Take js array as an example:
var a = ['1']; a[5] = 52; a.length //这儿的结果是6,也就是说,中间的key会自动补全,而值呢,是undefined
1. Add and delete
1. One-dimensional array
test = {}; //空json对像 test['firstname'] = "tank"; //添加二个元素 test['lastname'] = "zhang"; console.log(test); //查看 delete test['lastname']; //删除json中的某个元素 console.log(test);
2. Two-dimensional array
test1 = [{"name":"tank","total":"100"},{"name":"zhang","total":"23"},{"name":"hao","total":"325"}]; add = {"name":"may"}; test1.push(add); //添加一个元素 console.log(test1); delete test1[2]; //删除一个元素 console.log(test1);
2. Sorting
1. One-dimensional array
test = ["100","23","325"]; //定义个数组 function sortNumber(a,b) //定义排序方法 { return a - b } test1_sort=test.sort(sortNumber); console.log(test1_sort);
2. Two-dimensional array
test1 = [{"name":"tank","total":"100"},{"name":"zhang","total":"23"},{"name":"hao","total":"325"}]; sort_by = function(field, reverse, primer){ //定义排序方法 var key = primer ? function(x) {return primer(x[field])} : function(x) {return x[field]}; reverse = [-1, 1][+!!reverse]; return function (a, b) { return a = key(a), b = key(b), reverse * ((a > b) - (b > a)); } } test1_sort=test1.sort(sort_by('total', true, parseInt)); //根据total,升序排 console.log(test1_sort); test1_sort=test1.sort(sort_by('name', false, '')); //根据name,倒序排 console.log(test1_sort);
It may not seem like a minor problem, but if you don’t use it for a long time, it will become rusty or forgotten.
Readers who are interested in more json-related content can check out the special topics on this site: "Summary of json operation skills in JavaScript" and "Summary of json data operation skills with jQuery"
I hope this article will be helpful to everyone in JavaScript programming.

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



1. First of all, it is false to block and delete someone permanently and not add them permanently. If you want to add the other party after you have blocked them and deleted them, you only need the other party's consent. 2. If a user blocks someone, the other party will not be able to send messages to the user, view the user's circle of friends, or make calls with the user. 3. Blocking does not mean deleting the other party from the user's WeChat contact list. 4. If the user deletes the other party from the user's WeChat contact list after blocking them, there is no way to recover after deletion. 5. If the user wants to add the other party as a friend again, the other party needs to agree and add the user again.

Many users are increasingly favoring the electronic ecosystem of Xiaomi smart home interconnection in modern life. After connecting to the Mijia APP, you can easily control the connected devices with your mobile phone. However, many users still don’t know how to add Mijia to their homes. app, then this tutorial guide will bring you the specific connection methods and steps, hoping to help everyone in need. 1. After downloading Mijia APP, create or log in to Xiaomi account. 2. Adding method: After the new device is powered on, bring the phone close to the device and turn on the Xiaomi TV. Under normal circumstances, a connection prompt will pop up. Select "OK" to enter the device connection process. If no prompt pops up, you can also add the device manually. The method is: after entering the smart home APP, click the 1st button on the lower left

1. Open the Douyin app, click [Message] at the bottom of the interface, and click the chat conversation entry that needs to be deleted. 2. Long press any chat record, click [Multiple Select], and check the chat records you want to delete. 3. Click the [Delete] button in the lower right corner and select [Confirm deletion] in the pop-up window to permanently delete these records.

Practical PHP Tips: Delete the Last Semicolon in the Code When writing PHP code, you often encounter situations where you need to delete the last semicolon in the code. This may be because copy-pasting introduces extra semicolons, or to optimize code style and structure. In this article, we will introduce some methods to remove the last semicolon in PHP code and provide specific code examples. Method 1: Use the substr function The substr function can return a substring of a specified length from a string. we can

Performance optimization methods for converting PHP arrays to JSON include: using JSON extensions and the json_encode() function; adding the JSON_UNESCAPED_UNICODE option to avoid character escaping; using buffers to improve loop encoding performance; caching JSON encoding results; and considering using a third-party JSON encoding library.

1. Open the WeChat app, click [Me] in the lower right corner, find and click the [Moments] option. 2. Click [My Moments] in the upper right corner and find the content in the Moments you want to delete on the My Moments interface. 3. Click to enter the details page of this circle of friends, and click the [small trash can] icon to the right of the content release time. 4. Select [OK] in the pop-up window, thus completing the operation of deleting the content in the circle of friends.
![Outlook stuck on adding account [Fixed]](https://img.php.cn/upload/article/000/887/227/171116770937641.jpg?x-oss-process=image/resize,m_fill,h_207,w_330)
When you encounter problems adding accounts in Outlook, you can try the following solutions to resolve it. Typically this can be caused by a faulty network connection, corrupted user profiles, or other temporary issues. Through the methods provided in this article, you can easily solve these problems and ensure that your Outlook can run normally. Outlook stuck on adding account If your Outlook is stuck on adding account, then use these fixes mentioned below: Disconnect and reconnect the internet Temporarily disable antivirus software Create a new Outlook profile Try adding account in safe mode Disable IPv6 Run Microsoft Support and Recovery Assistant Repair Office Application Outlook Add Account Required

Annotations in the Jackson library control JSON serialization and deserialization: Serialization: @JsonIgnore: Ignore the property @JsonProperty: Specify the name @JsonGetter: Use the get method @JsonSetter: Use the set method Deserialization: @JsonIgnoreProperties: Ignore the property @ JsonProperty: Specify name @JsonCreator: Use constructor @JsonDeserialize: Custom logic
