admin管理员组

文章数量:1026669

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.

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.

Share Improve this question asked Oct 9, 2012 at 16:39 ManojkumarManojkumar 1,3616 gold badges36 silver badges66 bronze badges
Add a ment  | 

4 Answers 4

Reset to default 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"

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...

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.

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

campaign :[],
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.

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.

Share Improve this question asked Oct 9, 2012 at 16:39 ManojkumarManojkumar 1,3616 gold badges36 silver badges66 bronze badges
Add a ment  | 

4 Answers 4

Reset to default 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"

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...

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.

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

campaign :[],

本文标签: jqueryhow to insert string into a javascript objectStack Overflow