0
var myeleArr = {
                    advertiser :{},
                    campaign :{},
                    strategy :{},
            };

i want to insert some string into myeleArr.campaign object, so i am doing as shown below, but console displays as Object # has no method 'push'

myeleArr.campaign.push('<span class="'+myele['status']+'"></span>'+myele['name']+'');

Can some body help me.

Manojkumar
  • 1,351
  • 5
  • 35
  • 63

4 Answers4

1

If you intend for campaign to be a string, then set

myeleArr.campaign = "my string";

If you intend for campaign to hold a bunch of strings, in order, where the strings aren't referred to by name, then make it an array:

myeleArr.campaign = [];
myeleArr.campaign.push("My String");
myeleArr.campaign[0]; // "My String"

If you intend for campaign to hold a bunch of strings by name, then give your current campaign object named properties, and set each of those named properties to be a string:

myeleArr.campaign = {
    title : "My Title",
    type  : "Campaign Type",
    description : "Campaign Description",
    num_managers : 7,
    isActive : true
};

myeleArr.campaign.title; // "My Title"
Norguard
  • 26,167
  • 5
  • 41
  • 49
0

The push function is used to add an item to an array but campain isn't defined as an array.

Try with this :

 var myeleArr = {
        advertiser :{},
        campaign :[], // this makes an array
        strategy :{},
 };

Or, if you want to have only one value in campaign, don't use push but simply

myeleArr.campaign = '<spa...
Denys Séguret
  • 372,613
  • 87
  • 782
  • 758
0

The push method is meant for javascript arrays. So, instead of object literals, if you change campaign, etc keys to point to array literals, you'd be able to push. i.e. -

var myFoo = { advertiser: [], campaign: [], strategy: [] };
myFoo.campaign.push('my stuff');

Also, if you just want to assign a string to campaign, you could simply -

var myeleArr = {};
myeleArr.campaign = "<span>...";

Checkout the javascript console in developer tools under Google Chrome, or install nodejs, both are good places to try out these things when you're not sure.

Abhishek Mishra
  • 5,002
  • 8
  • 36
  • 38
0

Make the campaign as an array instead of an object . Because push is a method on arrays and not objects

campaign :[],
Sushanth --
  • 55,259
  • 9
  • 66
  • 105