Bonjour
voici quelques méthodes pour travailler avec les tableaux

j'ai écris ça car j'ai constaté que souvent nous faisons re refaisons les même chose et souvent pas très bien entre autre j'ai par exemple trouvé dans code source ici même une parcours itératif pour déterminer si un élément fait partit d'un tableau alors que indexOf retourne -1 s'il n'y est pas

bref je ne dis pas que cet ensemble soit meilleur qu'un autre mais j'ai cherché à réunir les méthodes dont nous avons souvent besoin
méthode de classe
Code javascript : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
//Class method check if argument is an array
//Array.isAn(aVar)
Array.isAn = function (b) {
  return Object.prototype.toString.call(b) == '[object Array]';
}
méthode ne modifiant pas l'objet
Code javascript : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
//return true if argument is in this array
Array.prototype.isIn = function(a) {
  return -1 != this.indexOf(a)
}
 
//return new array has intersection of this and argument array
//throw exception if argument is not an array
Array.prototype.intersect = function(a) {
  var r = []
  if (!Array.isAn(a)) {
    throw a + ' is not an Array'
  }
  for(var i = 0, l = this.length; i < l; i++) {
    if ( -1 != a.indexOf(this[i])) && 
       ( -1 == r.indexOf(this[i])) {
        r.push(this[i])
    }
  }
  return r
}
 
//return new array has union of this and argument array
//throw exception if argument is not an array
Array.prototype.union = function(a) {
  var r = []
  if (!Array.isAn(a)) {
    throw a + ' is not an Array'
  }
  for(var i = 0, l = this.length; i < l; i++) {
    if ( -1 == r.indexOf(this[i])) {
      r.push(this[i])
    }
  }
  for(var i = 0, l = a.length; i < l; i++) {
    if ( -1 == r.indexOf(a[i])) {
      r.push(a[i])
    }
  }
  return r
}
 
//return new array containing all items
// of this not in argument array
//throw exception if argument is not an array
Array.prototype.minus = function(a) {
  var r = []
  if (!Array.isAn(a)) {
    throw a + ' is not an Array'
  }
  for(var i = 0, l = this.length; i < l; i++) {
    if ( -1 == a.indexOf(this[i])) {
      r.push(this[i])
    }
  }
  return r
}
 
//return new array containing all items
// of this not in argument array
// and all items of argument array not in this
//throw exception if argument is not an array
Array.prototype.diff = function(a) {
  if (!Array.isAn(a)) {
    throw a + ' is not an Array'
  }
  return this.minus(a).union(a.minus(this))
}
 
//return new array containing all result
// of argument function applied on all items
//throw exception if argument is not a function
Array.prototype.map = function(f) {
  var r = []
  if ( 'function' != typeof f) {
    throw f + ' is not a function'
  }
  for(var i = 0, l = a.length; i < l; i++) {  
    r.push(f(this[i]))
  }
  return r
}
 
//return new array containing all items
// for which the argument function returns true
//throw exception if argument is not a function
Array.prototype.filter = function(f) {
  var r = []
  if ( 'function' != typeof f) {
    throw f + ' is not a function'
  }
  for(var i = 0, l = a.length; i < l; i++) {
    if (f(this[i])) {
      r.push(this[i])
    }
  }
  return r
}
Méthodes modifiant l'objet
Code javascript : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
//Append argument array to this
//throw exception if argument is not an array
Array.prototype.append = function (a) {
  if (!Array.isAn(a)) {
    throw a + ' is not an Array'
  }
  for(var i = 0, l = a.length; i < l; i++) {  
    this.push(a[i])
  }
  return this
}
 
//Pack delete all undéfined items
Array.prototype.pack = function() {
  //pack defined values
  for(var i = 0, j = 0, l = this.length; i < l; i++) {
    if ( 'undefined' == typeof this[i]) {
    } else if ( j == i) {
      j++
    } else {
      this[j] = this[i]
      j++
    }
  }
  //reduce array length
  for(var i = 0, l = this.length - j; i < l; i++) {
    this.pop()
  }
  return this
}
 
 
//remove all duplicate items of this
Array.prototype.clean = function() {
  //delete duplicate values
  for(var i = 0, l = this.length; i < l; i++) {
    if ( i != this.indexOf(this[i])) {
      delete (this[i])
    }
  }
  this.pack()
  return this
}
 
//delete an entry
Array.prototype.del = function(i) {
  //delete value
  if ( delete (this[i]) ) {
    this.pack()
  }
  return this
}
 
//remove all items of this that are in argument array
//throw exception if argument is not an array
Array.prototype.remove = function(a) {
  if (!Array.isAn(a)) {
    throw a + ' is not an Array'
  }
  for(var i = 0, l = this.length; i < l; i++) {
    if ( -1 != a.indexOf(this[i])) {
      delete(this[i])
    }
  }
  this.pack()
  return this
}
 
//remove all items for which the
// argument function returns true
//throw exception if argument is not a function
Array.prototype.wash = function(f) {
  if ( 'function' != typeof f) {
    throw f + ' is not a function'
  }
  for(var i = 0, l = a.length; i < l; i++) {
    if (f(this[i])) {
      delete(this[i])
    }
  }
  this.pack()
  return this
}
 
//apply argument function on all items
//throw exception if argument is not a function
Array.prototype.apply = function(f) {
  if ( 'function' != typeof f) {
    throw f + ' is not a function'
  }
  for(var i = 0, l = a.length; i < l; i++) {  
    this[i] = f(this[i])
  }
  return this
}
Gestion de piles
Code javascript : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
//return the last item of this
//Array can be used has stack with push, pop and top méthods
Array.prototype.top = function () {
  if (0 == this.length) {
    throw 'Array is empty'
  }
  return this[this.length - 1];
}

Code javascript : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
a = [1,2,2]
b = [3,4]
c = [2, 3]
// a est un tableau ?
Array.isAn(a)
// 2 est-il dans a
a.isIn(2)
// 7 est-il dans a
a.isIn(7)
// nouveau tableau contenant les 
// éléments de c qui ne sont pas dans a
c.minus(a)
// nouveau tableau contenant les
// éléments de c qui ne sont pas dans b
c.minus(b)
// nouveau tableau contenant les
// éléments de a qui ne sont pas dans b
a.minus(b)
// nouveau tableau contenant les
// éléments de a et de b
a.union(b)
// nouveau tableau contenant les
// éléments qui sont dans a et b
a.intersect(c)
// nouveau tableau contenant les
// éléments de c qui ne sont pas dans a
// et de a qui ne sont pas dans c
c.diff(a)
// nouveau tableau contenant les
// résultats de la fonction sur chaque éléments de a
a.map(function(i){return (i + 6) % 2;})
// nouveau tableau contenant true ou false
// suivant le résultat de la fonc sur chaque éléments de a
a.filter(function(i){return i%2 == 0})
 
//ajoute à a les élément de b
a.append(b)
//élimine de a les doublons
a.clean()
 
//remplace chaque élément de a
// par le résultat de la fonction
a.apply(function(i){return (i + 6) % 2;})
//conserve dans a que les éléments
// pour lesquels la fonction vaut true
a.wash(function(i){return i%2 == 1})
 
//supprime de a l'élément d'index 1
a.del(1)
//supprime de a tous les éléments
// dont la valeur est dans le tableau
a.remove([1,3,5,7,9])
 
//consulte sans l'enlever le sommet de la pile a
a.top()

A+JYT