user12048519
user12048519

Reputation:

Problem pushing a value inside an array located in an object key

Suppose I have the following function:

const createMenu = () => {
  const obj = {
    consumption: [],
  };

  return obj;
};

This is a function that, when called, returns the object

{ consumption: [] }

What I am trying to do is create a key inside that object that is a function that, when called with a string parameter, it pushes the string into the array inside the key 'consumption';

Here's my attempt:

const createMenu = () => {
  const obj = {
    consumption: [],
  };

  let order = (item) => {obj.consumption.push(item); };
  obj.order = order;

  return obj;
};

The expected result is that, when calling that function inside the object with a string parameter,like this:

createMenu().order('pizza');

when I run:

console.log(createMenu().consumption);

my result is:

['pizza']

but it is not working. I would appreciate if anyone could help me with this.

const createMenu = () => {
  const obj = {
    consumption: [],
  };

  let order = (item) => {
    obj.consumption.push(item);
  };
  obj.order = order;

  return obj;
};

createMenu().order('pizza');

console.log(createMenu().consumption);

Upvotes: 1

Views: 59

Answers (3)

Andy
Andy

Reputation: 63524

You may want to consider a class for this. Set up the array in the constructor, add a method to update the array when a new menu item is introduced, and have a final method that returns your desired object.

class CreateMenu {
  
  constructor() {
    this.consumption = [];
  };

  orderItem(item) {
    this.consumption.push(item);
    return this;
  }

  getList() {
    return { consumption: this.consumption };
  }

};

const menu = new CreateMenu();

const order = menu
  .orderItem('pizza')
  .orderItem('cheese sticks')
  .getList();
  
console.log(order);

Upvotes: 0

Keith
Keith

Reputation: 24191

You creating two instance of createMenu, Your likely wanting to create 1

const menu = createMenu()

Also if you want to chain the functions, you will want to return the obj again inside order.

Below is an example..

const createMenu = () => {
  const obj = {
    consumption: [],
  };

  let order = (item) => {
    obj.consumption.push(item);
    return obj;
  };
  obj.order = order;

  return obj;
};

const menu = createMenu().order('pizza');
console.log(menu.consumption);

Upvotes: 1

Sajeeb Ahamed
Sajeeb Ahamed

Reputation: 6390

You have to store the object created by createMenu() to a variable then perform operations to that variable. The update below should work.

In your code, you've created a new object when called the createMenu() function at the console.log. Which is not what you are wanting.

const createMenu = () => {
  const obj = {
    consumption: [],
  };

  let order = (item) => {
    obj.consumption.push(item);
  };
  obj.order = order;

  return obj;
};

const menu = createMenu();
menu.order('pizza');
menu.order('burger');
console.log(menu.consumption); // ["pizza", "burger"]
.as-console-wrapper{min-height: 100% !important; top: 0}

Upvotes: 1

Related Questions