10 examples of 'javascript push object into array at index' in JavaScript

Every line of 'javascript push object into array at index' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your JavaScript code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
9function insertIntoArray(array: T[], object: T): T[] {
10 return array ? [object, ...array] : [object];
11}
235insert(index, element) {
236 dart.as(element, E);
237 this[_checkInsertIndex](index);
238 this.callMethod('splice', [index, 0, element]);
239}
35export function insertAt (array, index, value) {
36 array.splice(index, 0, value)
37 return array
38}
18static up(array, index, callback) {
19 if (index > 0) {
20 const newIndex = index - 1;
21 const currentElement = _.clone(array[index], true);
22 array.splice(index, 1);
23 array.splice(newIndex, 0, currentElement);
24 if (callback) {
25 callback();
26 }
27 }
28}
11at (index, value) {
12 if (value !== undefined) {
13 if (this._array[index] !== undefined) {
14 this._removeValue(this._array[index])
15 }
16
17 this._addValue(this._array[index] = value)
18 }
19
20 return this._array[index]
21}
62var pushOrCreateArrayAt = function pushOrCreateArrayAt(object, key, valueToPush) {
63 // don't need to use hasOwnProp as there is no array in the prototype
64 // but still use it to avoid a warning
65 // const potentialArray = object[key]
66 if (Object.prototype.hasOwnProperty.call(object, key)) {
67 // eventually the if is always true
68 object[key].push(valueToPush);
69 } else {
70 // only for the first time
71 object[key] = [valueToPush];
72 }
73};
95var pushOrCreateArrayAt = function pushOrCreateArrayAt(object, key, valueToPush) {
96 // don't need to use hasOwnProp as there is no array in the prototype
97 // but still use it to avoid a warning
98 // const potentialArray = object[key]
99 if (hasOwnProperty.call(object, key)) {
100 // eventually the if is always true
101 object[key].push(valueToPush);
102 } else {
103 // only for the first time
104 object[key] = [valueToPush];
105 }
106};
158export function array_item_at(array: T[], index: number): T {
159 if (index >= array.length) {
160 return array[index % array.length];
161 }
162 else if (index < 0) {
163 return array[array.length - (-index % array.length)];
164 }
165 else {
166 return array[index];
167 }
168}
1module.exports = function array_push (inputArr) { // eslint-disable-line camelcase
2 // discuss at: https://locutus.io/php/array_push/
3 // original by: Kevin van Zonneveld (https://kvz.io)
4 // improved by: Brett Zamir (https://brett-zamir.me)
5 // note 1: Note also that IE retains information about property position even
6 // note 1: after being supposedly deleted, so if you delete properties and then
7 // note 1: add back properties with the same keys (including numeric) that had
8 // note 1: been deleted, the order will be as before; thus, this function is not
9 // note 1: really recommended with associative arrays (objects) in IE environments
10 // example 1: array_push(['kevin','van'], 'zonneveld')
11 // returns 1: 3
12
13 var i = 0
14 var pr = ''
15 var argv = arguments
16 var argc = argv.length
17 var allDigits = /^\d$/
18 var size = 0
19 var highestIdx = 0
20 var len = 0
21
22 if (inputArr.hasOwnProperty('length')) {
23 for (i = 1; i < argc; i++) {
24 inputArr[inputArr.length] = argv[i]
25 }
26 return inputArr.length
27 }
28
29 // Associative (object)
30 for (pr in inputArr) {
31 if (inputArr.hasOwnProperty(pr)) {
32 ++len
33 if (pr.search(allDigits) !== -1) {
34 size = parseInt(pr, 10)
35 highestIdx = size > highestIdx ? size : highestIdx
36 }
37 }
38 }
39 for (i = 1; i < argc; i++) {
40 inputArr[++highestIdx] = argv[i]
41 }
42
43 return len + i - 1
44}
30public removeAt(index: number): this {
31 const argumentName = this._getNextArgumentName();
32
33 this._scriptLines.push("this." + this._pathToArray + ".splice(args." + argumentName + ", 1);");
34 this._parameters[argumentName] = index;
35 return this;
36}

Related snippets