Harish
Harish

Reputation: 510

Is it possible to add an entry to a Javascript object residing in an another file?

I have an array of JS objects like this in a file called expenses.js

var expenses = 
[
  {
    day: "9/11/2019", expenses: {
        pen: 15,
        tea: 32,
        auto: 40,
        juice: 30,
    }
  },
  {
    day: "10/11/2019", expenses: {
        bananas: 50,
        auto: 100,
        tea: 30,
    }
  }
]

Is is possible to have a function like addExpenses({day: "11/11/2019", expenses: {biscuit: 20}) in another JS file, called add-expense.js, that would add an object to the array of objects in expenses.js?

Upvotes: 0

Views: 53

Answers (3)

Mara Black
Mara Black

Reputation: 1751

I didn't test it on different files but something like this should work

Basically you create a function who accept an array and a value as params. In the main file you specify your array, expenses and the value you want to insert. To be sure that your array is ok and you don't insert wrong data, you can make a little validation on the object that you want to insert

// ------- expenses.js ------------

// import add-expense.js
const add = require('./add-expense'); 

var expenses = 
[
  {
    day: "9/11/2019", expenses: {
        pen: 15,
        tea: 32,
        auto: 40,
        juice: 30,
    }
  },
  {
    day: "10/11/2019", expenses: {
        bananas: 50,
        auto: 100,
        tea: 30,
    }
  }
]

let newExpense = {day: "11/11/2019", expenses: {biscuit: 20}}
add.addExpenses(expenses, newExpense)
console.log(expenses)



// ---------  add-expense.js -------------
module.exports = {

  addExpenses: function(myArr, value){
    if(isObjectCorrect(value)){
      myArr.push(value);
    }
  },

  isObjectCorrect: function(value) {
      try {
          JSON.stringify(value);
      } catch (e) {
          return false;
      }
      return true;
  }

}

Upvotes: 0

Kevin.a
Kevin.a

Reputation: 4296

It's possible using imports and exports. With the little information provided, i created a little rough example to show you how exporting and importing works.

    var expenses = 
    [
      {
        day: "9/11/2019", expenses: {
            pen: 15,
            tea: 32,
            auto: 40,
            juice: 30,
        }
      },
      {
        day: "10/11/2019", expenses: {
            bananas: 50,
            auto: 100,
            tea: 30,
        }
      }
    ]

module.exports = expenses;

In your add-expenses file :

var expenses = require('dir/yourfile')

Now you have access to your object in whatever file you wish

Upvotes: 1

Dexygen
Dexygen

Reputation: 12561

Depends on what you mean by "add". If you mean at run-time, and the function you are executing loads after the file containing the object you want to modify, then the answer is yes, though I don't think you need an addExpenses function but could rather just do the following:

expenses.push({day: "11/11/2019", expenses: {biscuit: 20});

But if by "add" you mean such that the entry is saved to the original file on-disk, then the answer is typically no, not in a browser context, though possibly in a node.js context

Upvotes: 1

Related Questions