Object data type - object

Object data type - object

Understanding Object data types - Object

​ 1. Is a data type in js and a complex data type

​ 2. It is a "box" that carries data

​ 3. It is an unordered data set and a set of key value pairs

Creation of object data type

​ 1. Create objects literally

(1) create an empty object: var obj = {}

var obj = {}

(2) create an object with data: var obj = {key value pair}

< 1 > key value pair, key: value

< 2 > multiple can be written, separated by commas (,)

< 3 > there is no need to write comma (,) after the last key value pair. It can be written, but it is not recommended

Objects with data

There is a member in the obj object. The key is name and the value is' Jack '

var obj = {
       name: 'Jack',
       age: 18,
       gender: 'male'

​ 2. Built in constructor (class) creation

Create an empty object: var obj = new Object()

  var obj = new Object()

Basic operations of objects (adding, deleting, modifying and querying)

Add, delete, modify and check

​ 1. Add: add members in relative

​ 2. Delete: deletes a member in an object

​ 3. Change: modify a member in an object

​ 4. Query: get the value of a member in the object, and get the value according to the key

In JS, two sets of syntax are provided for the operation of objects

1. Dot grammar

  1. Array Association syntax

Point grammar

​ 1. increase

Syntax: object name Key name = value

For example, add a key called name to obj, and the value is stored as' Jack '

     obj.name = 'Jack'
     obj.age = 18
     obj.gender = 'male'
     obj.info = { weight: 180, height: 180 }
     obj.sayHi = function () { console.log('hello world') }
     console.log('After adding : ', obj)
  1. delete

Syntax: delete object name Key name

For example, delete a member called age in the obj object

     delete obj.age
     console.log('After deletion : ', obj)
  1. modify

​ 1. Syntax: object name Key name = value

​ 2. Because duplicate names are not allowed for key s in objects

​ 3. When you set it, the original is to modify, and the original is not to add

For example, set a key called gender in obj, and store the value as "female"

Because there was a gender key originally, the original key was modified

     obj.gender = 'female'
     console.log('After modification : ', obj)

4. Query

​ 1. Syntax: object name Key name

​ 2. When accessing, if there is this key in the object, the value will be given directly

​ 3. If there is no key in the object, give undefined (temporarily)


Array Association syntax

1. Increase

Syntax: object name ['key name'] = value

2. Delete

Syntax: delete object name ['key name']

3. Modification

Syntax: object name ['key name'] = value

What was originally modified was not originally added

  1. query

Syntax: object name ['key name']

    // Prepare an empty object
    var obj = {}
    console.log('Original object : ', obj)

    // 1. Increase
    obj['name'] = 'Rose'
    obj.age = 20
    obj['gender'] = 'female'
    console.log('After adding : ', obj)

    // 2. Delete
    delete obj['age']
    console.log('After deletion : ', obj)

    // 3. Modification
    obj['gender'] = 'male'
    console.log('After modification : ', obj)

    // 4. Check

Differences between two object operation grammars

​ 1. If the operation conforms to the variable naming rules and specifications, it doesn't matter to use two kinds of syntax

​ 2. Only array Association syntax can be used to operate a pure numeric key or a key with special symbols

​ 3. When your key involves variables, you can only use array Association syntax

var obj = {
      name: 'Jack',
      n1: 100,
      abc$222: 'hello',
      1: 100,
      2: true,
      'a-c': 'Feel free to come',
      '^_^': 'O(∩_∩)O ha-ha~'

    var s = 'name'

    // Simply accessing the value saved by a key called s in obj has nothing to do with the s variable
    // Simply accessing the value saved by a key called s in obj has nothing to do with the s variable
    // There is no written string in brackets, but a written variable
    // It is equivalent to accessing the s variable and taking the value of the s variable and putting it in []
    // obj[s] === obj['name '] accesses the value saved by a key called name in obj

What can the key of the object write

1. It is recommended to use names that conform to variable naming rules and specifications as key s

  1. You can use a pure number as the key of the object. Once a pure number is used, the pure number will be arranged at the front of all members

3. Any symbol can be used as the key of the object. If it contains special symbols, the key position needs to be wrapped in quotation marks

 var obj = {
       name: 'Jack',
       n1: 100,
       abc$222: 'hello',
       1: 100,
       2: true,
      'a#c ':' feel free to come ',
       '^_^': 'O(∩_∩)O ha-ha~'

Traversal of objects

​ 1. You cannot use a for loop for traversal

​ 2. Use the for in loop for traversal

​ 3. Syntax:

for (var variable in object){

Repeatedly executed code

​ }

(1) how many members are there in the object and how many times the loop is executed

(2) with the loop, the variable is each key

var obj = {
      name: 'Jack',
      age: 18,
      gender: 'male',
      score: 100
    // Start traversal
    for (var k in obj) {
      // With the loop, k is each key in obj
      console.log('I did', k)
      // You can get the object name, and you can get a variable along with the loop. k represents each key
      // How to access each member in an object
      console.log(obj.k) // It is a member called k in the exact access object, which has nothing to do with the k variable

Keywords: Javascript Front-end Vue.js

Added by lazytiger on Fri, 25 Feb 2022 16:48:21 +0200