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
| Array.prototype.insertAt = function (beginSlice, lengthSlice, elemToInsert) {
var arrReturned;
lengthSlice = lengthSlice >= 0 ? lengthSlice : -lengthSlice; // ensure abs value. Faster than Math.abs
arrReturned = this.slice(0, beginSlice);
arrReturned.push(elemToInsert);
return arrReturned.concat(this.slice(beginSlice + lengthSlice));
};
var result = [{
"id": "1",
"Afficher": "Un",
"Cacher": "Un"
}, {
"id": "2",
"Afficher": "Deux",
"phaseCacher": "Deux"
}, {
"id": "3",
"Afficher": "Quatre",
"phaseCacher": "Quatre"
}, {
"id": "4",
"Afficher": "Cinq",
"phaseCacher": "Cinq"
}, {
"id": "5",
"Afficher": "Six",
"phaseCacher": "Six"
}];
result = result.insertAt(2, 0, {
"id": "6",
"Afficher": "Trois",
"phaseCacher": "Trois"
})
console.log(result); // le nouvel objet est entre l'id 2 et l'id 3. A l'indice 2 du tableau, en somme |
Partager