最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

jquery - how to insert string into a javascript object - Stack Overflow

programmeradmin1浏览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.

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 :[],
发布评论

评论列表(0)

  1. 暂无评论