简体   繁体   中英

How to push object in array Javascript?

i have array

var CatTitle = ['Travel', 'Daily Needs','Food & Beverages','Lifestyle','Gadget & Entertainment','Others']

i want to push an object into this array

var myObj = {Coupon exp : 'xxx', couponcode : 'xxx'}

and i ll i have new array like this

var CatTitle = [
'Travel': [{Coupon exp : 'xxx', couponcode : 'xxx'},{Coupon exp : 'xxx', couponcode : 'xxx'}], 
'Daily Needs',
'Food & Beverages',
'Lifestyle',
'Gadget & Entertainment',
'Others']

How i can do it? Thanks

How to push an object in an Array

const animals = ['pigs', 'goats', 'sheep'];

animals.push({animal: 'cows'});

console.log(animals); // ["pigs", "goats", "sheep", { animal: "cows" }]

We can filter Travel because it is type of string , and then just push desired object into array:

 let catTitle = ['Travel', 'Daily Needs','Food & Beverages','Lifestyle','Gadget & Entertainment','Others']; catTitle = catTitle.filter(f => f;=='Travel'). catTitle:push({Travel:{'Coupon exp', 'xxx': couponcode. 'xxx'}}) console.log(catTitle)

If CatTitle were an Object :

var CatTitle = {'Travel': 0, 'Daily Needs': 0,'Food & Beverages': 0,'Lifestyle': 0,
    'Gadget & Entertainment': 0,'Others': 0}

Then it would be fairly simple:

CatTitle.Travel = {'Coupon exp': 'xxx', couponcode : 'xxx'}

I seriously do not know what you intend to do or where you are heading but below code will eventually do what you want.

Just using the only fact that Javascript Arrays are objects internally.

 var CatTitle = ['Travel', 'Daily Needs', 'Food & Beverages', 'Lifestyle', 'Gadget & Entertainment', 'Others'] var myObj = {Couponexp: 'xxx', couponcode: 'xxx'} CatTitle.shift(); CatTitle["Travel"] = []; CatTitle["Travel"].push(myObj); CatTitle["Travel"].push(myObj); console.log(CatTitle); console.log(CatTitle.Travel);

I think like this:

var CatTitle = ['Travel', 'Daily Needs','Food & Beverages','Lifestyle','Gadget & Entertainment','Others'];
var myObj = {Coupon exp : 'xxx', couponcode : 'xxx'};
var newObject = {};
var newArray = [];

var i;
for(i=0; i < CatTitle.length; i++) {
   var dump = {
      CatTitle[i]: myObj 
   }
   newArray.push(dump);
}
newObject = newArray;

I believe that you confusing Array and Object . To easily explain I'm going to use literal syntax.

Arrays are represented by [] .

Arrays doesn't have keys, so you can't use like that. Arrays keys are dynamically ordered index.

var CatTitle = [
    'Travel', 
    'Daily Needs',
    'Food & Beverages',
    'Lifestyle',
    'Gadget & Entertainment',
    'Others',
]

And Objects are represented by {} .

var myObj = {
    exp : 'xxx', 
    couponcode : 'xxx',
}

If you'd like to push some object into the array, use CatTitle.push({ exp: 'xxx' }) . So that you will have an array like this.

var CatTitle = [
    'Travel', 
    'Daily Needs',
    'Food & Beverages',
    'Lifestyle',
    'Gadget & Entertainment',
    'Others',
    { exp: 'xxx' }, // <-- you pushed here at last index (6)
];

CatTitle[6] // { exp: 'xxx' }

But I believe that you're looking for an object of objects. Like this:

var CatTitle = {
    'Travel': [{ exp: 'xxx' }, { exp: 'yyy' }], 
    'Daily Needs': [],
    'Food & Beverages': [],
    'Lifestyle': [],
    'Gadget & Entertainment': [],
    'Others': [],
};

CatTitle['Travel'] // [{ exp: 'xxx' }, { exp: 'xxx' }]
CatTitle['Travel'][0] // { exp: 'xxx' }

I see that you're starting learning JavaScript. I recommend you to read the docs about Arrays and Objects .

Other commendation is about semantic, never create variables in capital. Avoid creating keys as string, but I believe that you want it like something dynamic, in this case ok.

A good example:

var catTitles = {
    'Travel': [{ exp: 'xxx' }, { exp: 'yyy' }], 
    'Daily Needs': [],
    'Food & Beverages': [],
    'Lifestyle': [],
    'Gadget & Entertainment': [],
    'Others': [],
};

catTitles['Travel'] // [{ exp: 'xxx' }, { exp: 'xxx' }]
catTitles['Travel'][0] // { exp: 'xxx' }


// you can add another category like this
catTitle['Restaurants'] = [{ exp: 'xxx' }];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM