7 Replies - 835 Views - Last Post: 24 June 2013 - 08:29 PM

#1 AIintern  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 53
  • Joined: 06-June 12

Adding more than one element to an array.

Posted 20 June 2013 - 12:28 PM

I accidentally posted this question in the wrong forum. Sorry.

I was wondering if there was a possible way to add more than one element to an array..

For example each section of the array will hold a Name, Birthday, and Favorite color.


So when someone tries to print out Array[1]
It will show the Birthday, Name and Favorite color..

I hope this question makes sense.
Is This A Good Question/Topic? 0
  • +

Replies To: Adding more than one element to an array.

#2 JMRKER  Icon User is offline

  • D.I.C Addict

Reputation: 128
  • View blog
  • Posts: 826
  • Joined: 25-October 08

Re: Adding more than one element to an array.

Posted 20 June 2013 - 01:26 PM

Stupid example...
<!DOCTYPE HTML>
<html>
<head>
<title> Untitled </title>
<meta charset="utf-8">

</head>
<body>
<button onclick="document.getElementById('debugger').innerHTML = DB.join('<p>')">Show DB</button>
<div id="debugger"></div>

<script type="text/javascript">
var DB = [
  ['John',  '3/16', 'Blue'],
  ['April', '4/21', 'Green'],
  ['Summer','6/21', 'Brown'],
  ['Autum', '10/21','Red']
];

</script>

</body>
</html>


:bananaman:
Was This Post Helpful? 0
  • +
  • -

#3 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3489
  • View blog
  • Posts: 10,057
  • Joined: 08-June 10

Re: Adding more than one element to an array.

Posted 20 June 2013 - 10:39 PM

though I wouldn’t use an array of arrays for that, rather an array of objects.
var DB = [
  { name: 'John',  birthday: '3/16', color: 'Blue' },
  { name: 'April', birthday: '4/21', color: 'Green' },
  { name: 'Summer',birthday: '6/21', color: 'Brown' },
  { name: 'Autum', birthday: '10/21',color: 'Red' }
];

Was This Post Helpful? 1
  • +
  • -

#4 JMRKER  Icon User is offline

  • D.I.C Addict

Reputation: 128
  • View blog
  • Posts: 826
  • Joined: 25-October 08

Re: Adding more than one element to an array.

Posted 21 June 2013 - 07:07 AM

View PostDormilich, on 20 June 2013 - 11:39 PM, said:

though I wouldn’t use an array of arrays for that, rather an array of objects.
...


It didn't look like from his question that he was ready for that concept jump.
However, I have been wrong before ...
:)

And just for demonstration purposes to show how the information
access and display would be different ...
<!DOCTYPE HTML>
<html>
<head>
<title> Untitled </title>
<meta charset="utf-8">

</head>
<body>
<button onclick="document.getElementById('debugger').innerHTML = DBo.join('<p>')">Show DB original</button>
<button onclick="document.getElementById('debugger').innerHTML = showDBa()">Show DB altered</button>

<div id="debugger"></div>

<script type="text/javascript">
// For: http://www.dreamincode.net/forums/topic/323342-adding-more-than-one-element-to-an-array/

var DBo = [
  ['John',  '3/16', 'Blue'],
  ['April', '4/21', 'Green'],
  ['Summer','6/21', 'Brown'],
  ['Autum', '10/21','Red']
];

var DBa = [
  { name: 'John',  birthday: '3/16', color: 'Blue' },
  { name: 'April', birthday: '4/21', color: 'Green' },
  { name: 'Summer',birthday: '6/21', color: 'Brown' },
  { name: 'Autum', birthday: '10/21',color: 'Red' }
];
function showDBa() {
  var str = 'DBa contents<p>';
  for (var i=0; i<DBa.length; i++) {
    str += i+': '+DBa[i].name+', '+DBa[i].birthday+', '+DBa[i].color+'<p>';
  }
  return str;
}

</script>

</body>
</html>


This post has been edited by JMRKER: 21 June 2013 - 10:59 AM

Was This Post Helpful? 0
  • +
  • -

#5 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3489
  • View blog
  • Posts: 10,057
  • Joined: 08-June 10

Re: Adding more than one element to an array.

Posted 24 June 2013 - 05:13 AM

View PostJMRKER, on 21 June 2013 - 04:07 PM, said:

It didn't look like from his question that he was ready for that concept jump.
However, I have been wrong before ...

But if we don’t tell about better concepts, who else will?
Was This Post Helpful? 0
  • +
  • -

#6 JMRKER  Icon User is offline

  • D.I.C Addict

Reputation: 128
  • View blog
  • Posts: 826
  • Joined: 25-October 08

Re: Adding more than one element to an array.

Posted 24 June 2013 - 05:21 AM

Baby steps before the hop, skip & jump. :)/>

I agree with your solution, just not the order of presentation to the new OP.
In general, statements - logic - I/ O - arrays - hash - etc.

This post has been edited by JMRKER: 24 June 2013 - 05:26 AM

Was This Post Helpful? 0
  • +
  • -

#7 AIintern  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 53
  • Joined: 06-June 12

Re: Adding more than one element to an array.

Posted 24 June 2013 - 08:17 PM

thank you all for the replies! I should have gave better detail on my question.

What I meant was send multiple variables per one array.

For example.

The program would ask.
Whats your name?
(Type name)

Whats your favorite color?
(Type color)

would you like to add another person?
Y/N

Then that info would be sent to the array.

Then would repeat until user didn't want to add anymore.

and you could pull up a persons profile by the array placement.
Was This Post Helpful? 0
  • +
  • -

#8 laytonsdad  Icon User is online

  • Cheese and Sprinkles
  • member icon

Reputation: 433
  • View blog
  • Posts: 1,829
  • Joined: 30-April 10

Re: Adding more than one element to an array.

Posted 24 June 2013 - 08:29 PM

There are many ways to do this,

Example:

For the first person the array could be like this
  people[0][0] = name;
  people[0][1] = color;


For the Second the array could be like this
  people[1][0] = newName;
  people[1][1] = newColor;



you could do a loop so that for every new person you add a new element to the first array.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1