Adding objects to array

Posted on 2014-10-17
Medium Priority
Last Modified: 2014-11-06

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.
Question by:Al4ddin2
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
LVL 51

Expert Comment

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

Accepted Solution

Julian Hansen earned 2000 total points
ID: 40388008
Here is an example
<!doctype html>
<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;
<style type="text/css">
<span onclick="test();">Click</span>
<div id="result"></div>

Open in new window


Featured Post

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

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

In my daily work (mainly using ASP.net), I need to write a lot of JavaScript code. One of the most repetitive tasks I do are the jQuery Ajax calls. You know: (CODE) I don't know if for you it's the same, but for me is soooo tedious to write the …
This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
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…
Suggested Courses

800 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