JS operation object array to achieve add, delete, modify and query example code

JS operation object array to achieve add, delete, modify and query example code

1. Introduction

Recently, 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 Preparation

Here 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 operation

Query 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 operations

var 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 operations

Here are some other operation commands, which can also be used as references by friends in need:

  • push() adds an element at the end
  • unshift() adds the element to the front
  • pop() removes the last element
  • shift() removes the first element
  • splice() deletes elements, replaces elements, and inserts elements
  • sort() Sort an array
  • reverse() array reversal
  • Vue.set() modifies one of the elements in the array

Summarize

This 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:
  • Common array operations in JavaScript
  • Detailed explanation of JavaScript array deduplication
  • JavaScript Array Methods - Systematic Summary and Detailed Explanation
  • Summary of examples of common methods of JavaScript arrays
  • Summary of various methods for JavaScript to determine whether it is an array
  • JS uses map to integrate double arrays
  • JavaScript array merging case study
  • JS implements array filtering from simple to multi-condition filtering
  • Basic use of javascript array includes and reduce
  • js converts a multidimensional array into a one-dimensional array and then reorders it
  • Detailed explanation of the differences between js array find, some, filter, and reduce
  • JavaScript Array Detailed Summary

<<:  MySQL 8.0.16 installation and configuration tutorial under Windows 10

>>:  Commands to find domain IP address in Linux terminal (five methods)

Recommend

Dockerfile implementation code when starting two processes in a docker container

I want to make a docker for cron scheduled tasks ...

Introduction to Vue3 Composition API

Table of contents Overview Example Why is it need...

Apply provide and inject to refresh Vue page method

Table of contents Method 1: Call the function dir...

How to optimize logic judgment code in JavaScript

Preface The logical judgment statements we use in...

A Brief Analysis of MySQL - MVCC

Version Chain In InnoDB engine tables, there are ...

Ubuntu 18.04 disable/enable touchpad via command

In Ubuntu, you often encounter the situation wher...

MySQL time type selection

Table of contents DATETIME TIMESTAMP How to choos...

Web Design Tutorial (8): Web Page Hierarchy and Space Design

<br />Previous article: Web Design Tutorial ...

Nginx configuration SSL and WSS steps introduction

Table of contents Preface 1. Nginx installation 1...

CSS3 Tab animation example background switching dynamic effect

CSS 3 animation example - dynamic effect of Tab b...

How to set Nginx to forward the domain name to the specified port

Enter /usr/local/nginx/conf sudo cd /usr/local/ng...

Tutorial on installing MYSQL8.X on Centos

MySQL installation (4, 5, 6 can be omitted) State...

WeChat applet canvas implements signature function

In the WeChat applet project, the development mod...

JavaScript custom calendar effect

This article shares the specific code of JavaScri...

Detailed explanation of importing/exporting MySQL data in Docker container

Preface We all know that the import and export of...