Solved

Adding objects to array

Posted on 2014-10-17
2
61 Views
Last Modified: 2014-11-06
Hi,

I would like to add to an array the index and the value
so I am iterating through something and I want to add its index position and the value to an array then view the array.
0
Comment
Question by:Al4ddin2
2 Comments
 
LVL 50

Expert Comment

by:Steve Bink
ID: 40387985
Can you provide a little more detail about what you're trying to do?
0
 
LVL 56

Accepted Solution

by:
Julian Hansen earned 500 total points
ID: 40388008
Here is an example
<!doctype html>
<html>
<head>
<title>Test</title>
<script src="http://code.jquery.com/jquery.js"></script>
<script type="text/javascript">
var myArray = [];
function test()
{
  myArray['one'] = 1;
  myArray['two'] = 2;
  myArray['three'] = 3;
  var html = '';

  for(var key in myArray) {
    html += "Index : " + key + " = " + myArray[key] + '<br/>';
  }
  var id = document.getElementById('result');
  id.innerHTML = html;
}
</script>
<style type="text/css">
</style>
</head>
<body>
<span onclick="test();">Click</span>
<div id="result"></div>
</body>
</html>

Open in new window

0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
How to make google charts responsive 4 46
alert on input text 2 23
Jquery driving me nuts... 14 27
close pop up windows when logout or main window closes 17 38
This article shows how to create and access 2-dimensional arrays in JavaScript.  It includes a tutorial in case you are just trying to "get your head wrapped around" the concept and we'll also look at some useful tips for more advanced programmers. …
Article by: DanRollins
This article describes a JavaScript program that creates a maze made of hexagonal cells.  In Part 2 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7850-Hex-Maze-Part-2.html), we'll extend the program by adding a depth-…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

679 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question