JS data structure 0x001: array

Posted by ponsho on Wed, 11 Dec 2019 17:20:34 +0100

0x000 overview

The array here refers to the array in the data structure, not specifically the array in JS. Just use js to explore the array in the data structure, because I think JS is more convenient.

0x001 array

What is an array? Look at the picture:

An array has two elements:

  • Index: 0, 1, 2, 3, 4 in the figure
  • Data: data1-data5 in the figure

You can find some data by index, and then operate on it. The data here refers to generally, because array is a general data structure, which can store any data, such as numbers, objects, strings, and even arrays.

0x002 initialization

The initialization of arrays in js is very simple, and there are many ways:

[] // []
[1,2,3,4,5,6,7] //[1,2,3,4,5,6,7]
new Array() //[]
new Array(10) // [ 10 empty items]
new Array(1,2,3,4,6) // [1,2,3,4,5,6]
Array.from([1,2,3,4,5],(a)=>a*2) // [2,4,6,8,10]
Array.from(new Set([1,2,3,3])) // [1,2,3]

Here we choose the simplest to implement init:

function init() {
    return []
}

0x002 insertion

There are also many methods inserted in js, and each method has its own characteristics. In fact, the array of js has implemented many data structures

let data=[]
data[0]=1 // [1]
data.push(2) // [1, 2]
data=data.concat(3) // [1, 2, 3]
data=data.concat([4],5) // [1, 2, 3, 4, 5]

We still choose the simplest index method, because this is more consistent with the use of arrays in the data structure, and push is more suitable for other data structure operations.

function insert(arr, index, data) {
    arr[index] = data
    return arr
}

0x003 lookup

There are many ways to find the js array

let data = [1, 2, 3, 4, 5, 6]
data.find(d => d === 1) // 1
data[data.findIndex(d => d === 2)] //2
data.filter(d => d === 3)[0] // 3
data.forEach(d => {
    if (d === 4) {
        result = d // 4
    }
})  
for (let i = 0; i < data.length; i++) {
    if (data[i] === 5) {
        result = data[i] // 5
        break
    }
} 

for (var d of data) {
    if (d === 6) {
        result = d //6
        break
    }
}

We still use the simplest

function find(arr, data) {
    return arr.find(d => d === data)
}

0x004 delete

There are many ways to delete js

let data = [1, 2, 3, 4, 5, 6] 
delete data[0] // [ <1 empty item>, 2, 3, 4, 5, 6 ]
data.pop() // [ <1 empty item>, 2, 3, 4, 5]
data.splice(0, 1) // [2, 3, 4, 5]

We still use the simplest

function delete_(arr, index) {
    arr.splice(index,1)
    return arr
}

0x005 use

function main() {
    let arr = init()
    
    arr = insert(arr, 0, 1) // [1]
    arr = insert(arr, 1, 2) // [1, 2]
    arr = insert(arr, 2, 3) // [1, 2, 3]
    arr = insert(arr, 3, 4) // [1, 2, 3, 4]
    arr = insert(arr, 4, 5) // [1, 2, 3, 4, 5]
    arr = insert(arr, 5, 6) // [1, 2, 3, 4, 5, 6]
    
    find(arr, 1) // 1
    find(arr, 2) // 2
    find(arr, 3) // 3
    find(arr, 4) // 4
    find(arr, 5) // 5 
    
    delete_(arr, 0)
    delete_(arr, 1)
    delete_(arr, 2)
    delete_(arr, 3)
    delete_(arr, 4)
    delete_(arr, 5)
}

0x006 note

Of course, we usually don't use js like this, just to demonstrate the array:

let data=[1,2,3]
data.push(4)
data.push(5)
data.push(6)
data.filter(d=>d===1)
data.splice(0,1)

0x007 resources

Topics: Javascript github