10 examples of 'javascript push object into array with key' in JavaScript

Every line of 'javascript push object into array with key' 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
this disclaimer
9function insertIntoArray<T>(array: T[], object: T): T[] {
10 return array ? [object, ...array] : [object];
11}
Important

Use secure code every time

Secure your code as it's written. Use Snyk Code to scan source code in minutes – no build needed – and fix issues immediately. Enable Snyk Code

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};
1758function arrayPush(array, values) {
1759 var index = -1,
1760 length = values.length,
1761 offset = array.length;
1762
1763 while (++index < length) {
1764 array[offset + index] = values[index];
1765 }
1766 return array;
1767}
168function arrayPush(array, values) {
169 var index = -1, length = values.length, offset = array.length
170
171 while (++index < length) {
172 array[offset + index] = values[index]
173 }
174 return array
175}
104function push (array, value) {
105 array.push(value)
106 return array
107}
59function addKey(jsonArray)
60{
61 let newJsonArray = [];
62
63 for(var i = 0; i < jsonArray.length; i++)
64 {
65 let jsonObject = jsonArray[i];
66
67 let newJsonObject =
68 {
69 key: i,
70 ...jsonObject
71 }
72
73 newJsonArray.push(newJsonObject);
74 }
75
76 return newJsonArray;
77}
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}
98function push(collection, object) {
99 Array.prototype.push.call(collection, object);
100 collection.trigger('add', object);
101}
88export function safePush(obj, prop, val) {
89 if (!obj[prop]) obj[prop] = [];
90 obj[prop].push(val);
91}

Related snippets