Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 07-29-2012, 02:27 AM   PM User | #1
bunzu
New Coder

 
Join Date: Jul 2012
Posts: 10
Thanks: 3
Thanked 0 Times in 0 Posts
bunzu is an unknown quantity at this point
Object within and Object?

Hey Guys,

I need a little help and I'm a bit confused. Here is the result that I want:
Code:
loggedObject2 = { 
	'0': { 'items': 'marble', 'quantity': 5 },
	'1': { 'items': 'rock', 'quantity': 3 } 
};

The problem I'm having right now is sticking an object into an object. I can get a part of it but not the other part.
Code:
loggedObject = {};

function logToObject(items,quantity){
	loggedObject.items = items;
	loggedObject.quantity = quantity
}
logToObject('marble', 5);
console.log(loggedObject);
Any help would be great!
Thanks!
bunzu is offline   Reply With Quote
Old 07-29-2012, 05:21 AM   PM User | #2
Richter
New Coder

 
Join Date: Jun 2012
Posts: 63
Thanks: 0
Thanked 11 Times in 11 Posts
Richter is an unknown quantity at this point
Why not array ? since you still index them by number.

Code:
loggedObject = [];

loggedObject.push( { items:'marble', quantity:5} );
loggedObject.push( { items:'rock', quantity:3 } );

console.log(loggedObject);
If you want some OOP.
Code:
loggedObject = [];

function Material(Name, Quantity) { this.items = Name; this.quantity = Quantity ; }

loggedObject.push( new Material('marble' , 5) );
loggedObject.push( new Material('rock', 3) );

console.log(loggedObject);

Last edited by Richter; 07-29-2012 at 05:30 AM..
Richter is offline   Reply With Quote
Old 07-29-2012, 05:37 AM   PM User | #3
bunzu
New Coder

 
Join Date: Jul 2012
Posts: 10
Thanks: 3
Thanked 0 Times in 0 Posts
bunzu is an unknown quantity at this point
It's just so I can learn more about objects. Maybe eventually I'll categorize it similar to:

Depending on what type of fruit/vegetable it is

Code:
loggedObject2 = { 
	'Vegetable': { 'items': 'Carrots', 'quantity': 5 },
	'Fruit': { 'items': 'Apple', 'quantity': 3, 'items': 'Banana', 'quantity': 3 } 
};
bunzu is offline   Reply With Quote
Old 07-29-2012, 05:45 AM   PM User | #4
Richter
New Coder

 
Join Date: Jun 2012
Posts: 63
Thanks: 0
Thanked 11 Times in 11 Posts
Richter is an unknown quantity at this point
In that case just put the name you want in to object directly

Code:
loggedObject2 = {};

loggedObject2['Vegetable'] = { 'items': 'Carrots', 'quantity': 5 };
//or	
loggedObject2.Fruit = [{ 'items': 'Apple', 'quantity': 3 }, { 'items': 'Banana', 'quantity': 3 } ];
{ 'items': 'Apple', 'quantity': 3, 'items': 'Banana', 'quantity': 3 } is error, you can't do that and usualy items mean array of item, in this case you don't need it.

I write a long version but it easier to understand.
Code:
loggedObject2 = {};

loggedObject2.Vegetable = {};
loggedObject2.Vegetable.Carrot = {};
loggedObject2.Vegetable.Carrot.quantity = 5;

loggedObject2.Fruit = {};
loggedObject2.Fruit.Apple = {};
loggedObject2.Fruit.Apple.quantity = 3;

loggedObject2.Fruit.Banana = {};
loggedObject2.Fruit.Banana.quantity = 3;
ps. what you do isn't object, it's call Json
Code:
//Json  
{ 'items': 'Apple' }

//Object
{ items: 'Apple' }

Last edited by Richter; 07-29-2012 at 06:09 AM.. Reason: Ah man, I edit this post too many :(
Richter is offline   Reply With Quote
Users who have thanked Richter for this post:
bunzu (07-29-2012)
Old 07-29-2012, 06:19 AM   PM User | #5
bunzu
New Coder

 
Join Date: Jul 2012
Posts: 10
Thanks: 3
Thanked 0 Times in 0 Posts
bunzu is an unknown quantity at this point
Ahh. I guess I'm trying to write function that stores JSON data? I think that's what it's called.

Thanks by the way!
bunzu is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 06:50 AM.


Advertisement
Log in to turn off these ads.