var arr = [];

for (var prop in obj) {
   if (obj.hasOwnProperty(prop)) {
      var innerObj = {};
      innerObj[prop] = obj[prop];
      arr.push(innerObj)
   }
}
    
console.log(arr);

here is demo https://plnkr.co/edit/9PxisCVrhxlurHJYyeIB?p=preview

Answer from Salih Şenol Çakarcı on Stack Overflow
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Array › push
Array.prototype.push() - JavaScript | MDN
Notice that the elements are wrapped in an extra array — otherwise, if the element is an array itself, it would be spread instead of pushed as a single element due to the behavior of concat(). The push() method is generic. It only expects the this value to have a length property and integer-keyed ...
🌐
Delft Stack
delftstack.com › home › howto › javascript › javascript array push key value
How to Push Key-Value Pair Into an Array Using JavaScript | Delft Stack
February 2, 2024 - We utilized the push() method to add the obj object to the arr array. This action effectively inserted the object into the array as a single element. Finally, we logged the arr array to the console, presenting an array that held a single object ...
🌐
EyeHunts
tutorial.eyehunts.com › home › javascript push object into array with key | example code
JavaScript push object into Array with key | Example code
January 18, 2023 - <!DOCTYPE html> <html> <body> <script> var arr = []; let obj = { "id": 23, "name": "Jacob", "link": { "rel": "self", "link": "www.abc.com" }, "company":{ "data":{ "id": 1, "ref": 324 } }} for (var prop in obj) { if (obj.hasOwnProperty(prop)) { var innerObj = {}; innerObj[prop] = obj[prop]; arr.push(innerObj) } } console.log(arr); </script> </body> </html> ... Array(4) [ {…}, {…}, {…}, {…} ] ​ 0: Object { id: 23 } ​ 1: Object { name: "Jacob" } ​ 2: Object { link: {…} } ​ 3: Object { company: {…} } ​ length: 4
Top answer
1 of 5
225

There are no keys in JavaScript arrays. Use objects for that purpose.

var obj = {};

$.getJSON("displayjson.php",function (data) {
    $.each(data.news, function (i, news) {
        obj[news.title] = news.link;
    });                      
});

// later:
$.each(obj, function (index, value) {
    alert( index + ' : ' + value );
});

In JavaScript, objects fulfill the role of associative arrays. Be aware that objects do not have a defined "sort order" when iterating them (see below).

However, In your case it is not really clear to me why you transfer data from the original object (data.news) at all. Why do you not simply pass a reference to that object around?


You can combine objects and arrays to achieve predictable iteration and key/value behavior:

var arr = [];

$.getJSON("displayjson.php",function (data) {
    $.each(data.news, function (i, news) {
        arr.push({
            title: news.title, 
            link:  news.link
        });
    });                      
});

// later:
$.each(arr, function (index, value) {
    alert( value.title + ' : ' + value.link );
});
2 of 5
60

This code

var title = news.title;
var link = news.link;
arr.push({title : link});

is not doing what you think it does. What gets pushed is a new object with a single member named "title" and with link as the value ... the actual title value is not used. To save an object with two fields you have to do something like

arr.push({title:title, link:link});

or with recent Javascript advances you can use the shortcut

arr.push({title, link}); // Note: comma "," and not colon ":"

If instead you want the key of the object to be the content of the variable title you can use

arr.push({[title]: link}); // Note that title has been wrapped in brackets
Find elsewhere
🌐
Reddit
reddit.com › r/learnjavascript › pushing objects into array
r/learnjavascript on Reddit: Pushing Objects Into Array
November 8, 2021 -

Ive been googling for a fair bit but i simply dont understand how i should do this.

Ive got two input fields, in one field the user inputs the product name, and in the second input field the user inputs how much of this product to be added to the storage. For example "Red Bull" and "37". I then want to take these inputs and push them into an array that ive created. However i want to make them a singular object within the array so that it doesent look like this: ["Red Bull", "37", "Pizza", "35"] but rather [{"Red Bull", 37},{"Pizza", 35}] So that essentially every time the button is clicked that runs the function, a new object within the array is created. My code so far:

Any help would be greatly appriciated <3

The "addProduct" function is the one im struggling with as it pushes the items directly into the array.

Heres the working one
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › how-to-add-an-object-to-an-array-in-javascript
JavaScript- Add an Object to JS Array - GeeksforGeeks
The push() method adds objects to the end, unshift() adds to the beginning, and concat() combines arrays. The spread operator offers a modern way to append objects, while splice() allows insertion at specific indices.
Published   July 12, 2025
🌐
EyeHunts
tutorial.eyehunts.com › home › javascript array push key value | example code
JavaScript array push key value | Example code
July 20, 2022 - You have to use bracket notation to push a key value into a JavaScript array. obj["key3"] = "c"; // dynamic - 'key3' can be a variable · Note: use .push() method doesn’t work on an object.
🌐
Linux Hint
linuxhint.com › push-an-object-to-an-array-in-javascript
How to Push an Object to an Array in JavaScript
Linux Hint LLC, [email protected] 1210 Kelly Park Circle, Morgan Hill, CA 95037 Privacy Policy and Terms of Use
🌐
Talkerscode
talkerscode.com › howto › javascript-push-object-into-array-with-key.php
JavaScript Push Object Into Array With Key
We can use bracket notation to assign one or more key-value pairs to the object, and then use the push method to add the object to the end of the Array once the values have been assigned.
🌐
GeeksforGeeks
geeksforgeeks.org › how-to-push-an-array-into-the-object-in-javascript
How to Push an Array into Object in JavaScript? | GeeksforGeeks
January 9, 2025 - Here are the different methods ... to convert a two-dimensional array into an object is by iterating through the array using a for loop and assigning key-value pairs.JavaScriptconst a = [['name', 'Abhay...
🌐
W3Schools
w3schools.com › jsref › jsref_push.asp
JavaScript Array push() Method
The push() method adds new items to the end of an array.
🌐
Stack Abuse
stackabuse.com › bytes › push-an-object-to-an-array-in-javascript
Push an Object to an Array in JavaScript
July 23, 2022 - let array = []; let obj = { name: ... 'Joe', age: 25, role: 'user'}] You can also achieve the same thing by passing the object directly to the push() method, without first assigning it to a variable....
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Array › from
Array.from() - JavaScript | MDN
When an iterable is passed as items, the constructor is called with no arguments; when an array-like object is passed, the constructor is called with the normalized length of the array-like object. The final length will be set again when iteration finishes. If the this value is not a constructor function, the plain Array constructor is used instead. ... const map = new Map([ [1, 2], [2, 4], [4, 8], ]); Array.from(map); // [[1, 2], [2, 4], [4, 8]] const mapper = new Map([ ["1", "a"], ["2", "b"], ]); Array.from(mapper.values()); // ['a', 'b']; Array.from(mapper.keys()); // ['1', '2'];
🌐
ServiceNow Community
servicenow.com › community › app-engine-forum › need-to-push-object-in-an-array-from-loop › m-p › 2421480
Solved: Need to push object in an array from loop - ServiceNow Community
September 24, 2025 - Here's an example of how you can push an entire GlideRecord object into an array: var users = []; var obj = {}; var a = new GlideRecord('sys_user'); a.addQuery('active',true); a.setLimit(5); a.query(); while (a.next()) { for (var key in a) { obj[key] = a[key].toString(); } users.push(obj); } ...
🌐
DEV Community
dev.to › awaisalwaisy › 7-ways-to-convert-objects-into-array-in-javascript-35m4
7 ways to convert Objects into Array in JavaScript - DEV Community
February 12, 2023 - Here's an example · let obj = {a: 1, b: 2, c: 3}; let arr = []; for (let key in obj) { arr.push(obj[key]); } console.log(arr); // Output: [1, 2, 3] The fourth method to convert a JavaScript object into an array is by using the Object.keys() method ...