-3

I have a JavaScript object

const categories = Object.keys(titles);

that looks like this

["scifi","horror","comedy"]

I want to add "drama" to the object. I've tried

categories['drama']; 

but that doesn't work. How can I add another category to this categories object?

Modelesq
  • 5,192
  • 20
  • 61
  • 88
  • 1
    I'm voting to close. Read the fine manual ~ https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/push – Phil Jun 10 '16 at 02:37
  • FYI, `categories` is an `Array`. See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/keys – Phil Jun 10 '16 at 02:39
  • Or there is also [How to insert an item into an array at a specific index?](http://stackoverflow.com/q/586182/1529630) – Oriol Jun 10 '16 at 02:47

1 Answers1

2

It's an array of object keys so you need to use Array#push() method

categories.push('drama'); 

const categories = ["scifi", "horror", "comedy"];

categories.push('drama');

console.log(categories);
Pranav C Balan
  • 113,687
  • 23
  • 165
  • 188