1. IntroductionRecently, I helped a friend to make a simple page for adding, deleting and modifying json arrays, which happened to involve js to operate object arrays to implement the add, delete, modify and query functions. I guess many of my friends will encounter this kind of operation, so I record it for sharing. 2. Data PreparationHere I will take the student object array as an example. In fact, the operation of this array is basically the same as that of the JSON array, so you only need to convert it. For example, you can use JSON.parse to convert a JSON string into a js object array. Test data: // Student object array var students = [ {id:1, name: "张三", age: 14}, {id:2, name: "Li Si", age: 15}, {id:3, name: "Wang Wu", age: 17}, {id:4, name: "Zhao Liu", age: 18} ]; 3. Query operationQuery by subscript console.log(students[1]); Query by id var ss = students.filter((p) => { return p.id == 4; }); console.log(ss); console.log(ss[0]); // Print the first element Fuzzy search by name 4. Add new operationsvar e = {id:5, name: "Wang Ba", age: 20}; students.push(e); 5. Delete// Get the subscript based on ID var e = students.filter((p) => { return p.id == 4; }); var index = students.indexOf(e); // Delete students according to the index.splice(index,1); console.log(students.length); // 4 left 6. Modifications// You can modify students[0].name='张三1' directly according to the subscript; students[0].age=20; console.log(students[0]); 7. How to test?Here you can use the console panel in the F12 developer tool of Google Chrome (actually it is a js execution engine). You only need to enter and execute the above code in sequence to see the effect: 8. Other array operationsHere are some other operation commands, which can also be used as references by friends in need:
SummarizeThis concludes the article on how to use JS to manipulate object arrays to implement additions, deletions, modifications, and lookups. For more information on how to use JS to manipulate object arrays, please search previous articles on 123WORDPRESS.COM or continue browsing the following related articles. I hope you will continue to support 123WORDPRESS.COM in the future! You may also be interested in:
|
<<: MySQL 8.0.16 installation and configuration tutorial under Windows 10
>>: Commands to find domain IP address in Linux terminal (five methods)
The default_server directive of nginx can define ...
Table of contents What is a mind map? How to draw...
A jQuery plugin every day - step progress axis st...
Execute the command to install the plugin postcss...
This article mainly introduces how to call desktop...
CSS (Cascading Style Sheet) is used to beautify H...
This article uses an example to illustrate how to...
<br />In the past, creating a printer-friend...
Today, we use uniapp to integrate Echarts to disp...
Table of contents 1. The concept of process and t...
MySQL password is correct but cannot log in local...
Today I had some free time to write a website for...
A data volume container is a container specifical...
Use indexes to speed up queries 1. Introduction I...
Table of contents 1. In project development, the ...