Questions tagged [array-push]

array-push refers to the javascript Array.prototype.push() method. The push() method adds one or more elements to the end of an array and returns the new length of the array.

refers to the javascript Array.prototype.push() method.

The push() method adds one or more elements to the end of an array and returns the new length of that array. developer

Example

var a = []; //or new Array()
var i = a.push('foo', 'bar');
console.log(i, a); 

the console will display the values

2 ["foo", "bar"]

The counterpart to push() is pop(), which removes the last item from an array.

Documentation

382 questions
81
votes
4 answers

Appending (pushing) and removing from a JSON array in PostgreSQL 9.5+

For versions less than 9.5 see this question I have created a table in PostgreSQL using this: CREATE TEMP TABLE jsontesting AS SELECT id, jsondata::jsonb FROM ( VALUES (1, '["abra","value","mango", "apple", "sample"]'), (2,…
Evan Carroll
  • 78,363
  • 46
  • 261
  • 468
74
votes
8 answers

How can I push an object into an array?

I know it's simple, but I don't get it. I have this code: // My object const nieto = { label: "Title", value: "Ramones" } let nietos = []; nietos.push(nieto.label); nietos.push(nieto.value); If I do this I'll get a simple array: ["Title",…
pmiranda
  • 7,602
  • 14
  • 72
  • 155
73
votes
4 answers

PHP add elements to multidimensional array with array_push

I have a multidimensional array $md_array and I want to add more elements to the sub-arrays recipe_type and cuisine coming from a loop that reads data from a table. In the loop, I create a new table $newdata for each row: $newdata = array ( …
bikey77
  • 6,384
  • 20
  • 60
  • 86
61
votes
10 answers

Push only unique values into an array while looping

I am using the following loop to add items to an an array. I would like to know if it is somehow possible to not add $value to the $liste array if the value is already in the array? $liste = array(); foreach($something as $value){ …
Marc
  • 9,217
  • 21
  • 67
  • 90
34
votes
6 answers

How can I extend Array.prototype.push()?

I'm trying to extend the Array.push method so that using push will trigger a callback method and then perform the normal array function. I'm not quite sure how to do this, but here's some code I've been playing with unsuccessfully. arr =…
Geuis
  • 41,122
  • 56
  • 157
  • 219
29
votes
7 answers

Why does Array.prototype.push return the new length instead of something more useful?

Ever since its introduction in ECMA-262, 3rd Edition, the Array.prototype.push method's return value is a Number: 15.4.4.7 Array.prototype.push ( [ item1 [ , item2 [ , … ] ] ] ) The arguments are appended to the end of the array, in the order in…
Bryce
  • 6,440
  • 8
  • 40
  • 64
27
votes
2 answers

array_push for associative arrays

I'm trying to extend an assoc array like this, but PHP doesn't like it. I receive this message: Warning: array_push() expects parameter 1 to be array, null given Here's my code: $newArray = array(); foreach ( $array as $key => $value ) { …
EnglishAdam
  • 1,380
  • 1
  • 19
  • 42
20
votes
4 answers

Array push with associate array

If I am working with an associate array like such: Array ( [Username] => user [Email] => email ) and I want to add an element to the end, I would think to do: array_push($array, array('Password' => 'pass')); However, this leaves me…
grep
  • 3,986
  • 7
  • 45
  • 67
19
votes
4 answers

Push elements from one array into rows of another array (one element per row)

I need to push elements from one array into respective rows of another array. The 2 arrays are created from $_POST and $_FILES and I need them to be associated with each other based on their indexes. $array1 = [ [123, "Title #1", "Name #1"], …
ticallian
  • 1,631
  • 7
  • 24
  • 34
16
votes
6 answers

Can I use array_push on a SESSION array in php?

I have an array that I want on multiple pages, so I made it a SESSION array. I want to add a series of names and then on another page, I want to be able to use a foreach loop to echo out all the names in that array. This is the…
zeckdude
  • 15,877
  • 43
  • 139
  • 187
14
votes
2 answers

Associative array to JSON

I would like to be able to generate a JSON output in the following format: {"a":{"ax":1,"abx":2},"b":{"bax":1,"bbx":2},"c":3,"d":4,"e":5} Although I have found that the respective code is this: $arr = array('a' => array('ax' => 1, 'abx' => 2), 'b'…
Nick
  • 151
  • 2
  • 3
  • 12
13
votes
4 answers

php array_push with index and key

I am not sure if I got the terms right in my title, but I am trying to do a php array_push like so array_push($countryList, "US" => "United States"); but this gives me a syntax error. Am I not doing this properly?
user1269625
  • 3,121
  • 26
  • 79
  • 111
11
votes
2 answers

Javascript add item to current array

I am trying to add an item to a current array. var arrayValues = new Array(); arrayValues.push("Value 1"); arrayValues.push("Value 2"); arrayValues = document.getElementsByTagName('a'); arrayValues.push("Value 3"); By doing this way I get a error,…
Vinay
  • 221
  • 1
  • 6
  • 13
10
votes
2 answers

array_push not working within function, PHP

I have this case when I have array_push inside function and then I need to run it inside foreach filling the new array. Unfortunately I can't see why this does not work. Here is the code:
devjs11
  • 1,898
  • 7
  • 43
  • 73
9
votes
3 answers

Why is this JavaScript map NOT an Infinite Loop?

I'm learning JavaScript. I wrote this code to learn the map function. But then I got confused as to why is this not mapping over it continuously as with each map sequence a new element is pushed to the array. Shouldn't it continue to push new…
ExpertNoob
  • 149
  • 1
  • 9
1
2 3
25 26