0

What is the easiest to understand solution to get out the values as follows:

Bill, Smith, 32
Sarah, Lord, 28

I only got this far:

HTML

<a href="#" onclick="displayEmployees();">displayEmployees</a>

Javascript

function displayEmployees() {
var employees = new Array();
employees['firstName'] = new Array('Bill','Sarah','Elizabeth','David','Robert');
employees['lastName'] = new Array('Smith','Lord','Crayford','Clark','Robinson');
employees['age'] = new Array('32','28','20','54','18');
for(x in employees) {
    var value = employees[x]
    document.write(value + "<br />");
}
Chris
  • 545
  • 7
  • 29

3 Answers3

2
var array = [
    ["Bill", "Smith", "32"],
    ["Sarah", "Lord", "28"],
    ["Elizabeth", "Crayford", "20"],
    ["David", "Clark", "54"],
    ["Robert", "Robinson", "18"]
];
for (var i = 0, len = array.length; i < len; ++i) {
    document.write(array[i].join(", ") + "<br>");
}
Esailija
  • 130,716
  • 22
  • 250
  • 308
2
function displayEmployees() {
var employees = new Array();
employees['firstName'] = new Array('Will','Sarah','Elizabeth','David','Robert');
employees['lastName'] = new Array('Smith','Lord','Crayford','Clark','Pattinson');
employees['age'] = new Array('32','28','20','54','18');
for(var i=0;i<employees['firstName'].length;i++)             //can be employees['lastName']or employee['age'] too 
{
    document.write(employees['firstName'][i]+','+employees['lastName'][i]+','+employees['age'][i]+'<br />');}
}
bugwheels94
  • 26,775
  • 3
  • 35
  • 57
1

You can do like this

var value;
for(x in employees['firstName']){
   value = employees['firstName'][x]+', '
          +employees['lastName'][x]+', '
          +employees['age'][x];

   document.write(value + "<br />");
}

The better way will be to create the employees array in other way, like this

employees = [{'firstName':'Bill','lastName':'Smith','age':32},
             {'firstName':'Sarah','lastName':'Lord','age':28},...];

var value;
for(x in employees){
   value = employees[x].join(', ');
   document.write(value + "<br />");
}
  • Don't you think that this answer looks like the hybrid of the existing answers – bugwheels94 Jun 18 '13 at 21:37
  • @Ankit Don't you think down voting my question is irrelevant as 1. your answer is definitely not clearer than the accepted one, not even formatted better; 2. the second answer (Esailija) does not actually answer the question?! – Chris Jun 18 '13 at 22:02
  • @ChrisDemetriad let me politely clear you one thing that i didn't downvote you mate – bugwheels94 Jun 18 '13 at 22:34
  • @ChrisDemetriad let me clear you that in this answer the second and the so called better way is extremely wrong and errorful – bugwheels94 Jun 18 '13 at 22:38
  • @@nkit can you give more details then? is it bad to use a 'for in' loop? I thought it is not? [link](http://stackoverflow.com/questions/242841/javascript-for-in-vs-for) – Chris Jun 19 '13 at 10:08
  • @Ankit I don`t think this is extremely wrong and errorful :) this sure can be used without any problem and sure it will work, what about hybrid, I have posted the answer simultaneously so when I started to write the answer there were no answers and after research and submit there were already 2 answers – ashvardanyan Jun 19 '13 at 12:07
  • @ChrisDemetriad one can't use join on object so that is extremely wrong – bugwheels94 Jun 19 '13 at 12:21
  • 1
    @Ankit but aren't mine arrays as they are explicitly declared so? And didn't Esailija used it in his solution?! I am extremely confused now. – Chris Jun 19 '13 at 12:23
  • employees = [{'firstName':'Bill','lastName':'Smith','age':32}, {'firstName':'Sarah','lastName':'Lord','age':28}]; this is creating array, not object – ashvardanyan Jun 19 '13 at 12:47
  • @ashvardanyan but employees[x] is an object and you are using join on that – bugwheels94 Jun 23 '13 at 17:41