Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamically Add to Javascript Object

I have a Javascript object that looks like this.

ips[ipID] = {}

So I end up with a bunch of ips that need to store information that will look like

ipID { name : 'val', anotherName : 'anotherVal' }

My question is, how do I dynamically add these names and values?

like image 517
Mike Avatar asked Nov 01 '10 17:11

Mike


2 Answers

I believe this is the easiest thing to do if your names are dynamic:

var myobj = {}; var newFieldName = 'my new field name'; var newFieldValue = 'my new field value'; myobj[newFieldName] = newFieldValue; 
like image 114
mathisonian Avatar answered Sep 23 '22 19:09

mathisonian


var ipID = {}; ipID.name = 'val'; ipID.anotherName = 'anotherVal'; 
like image 35
Jakub Konecki Avatar answered Sep 24 '22 19:09

Jakub Konecki