0

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?

2
  • How is it possible? Commented Nov 19, 2019 at 12:18
  • You can use modules for that Commented Nov 19, 2019 at 12:25

3 Answers 3

1

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

Sign up to request clarification or add additional context in comments.

Comments

1

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

4 Comments

I have understood your solution, but I am using live server plugin of VS code. Do I have to run a node server instead?
@Harish you dont need a server for as far as i am concerned. Most modern browsers support this function.
Unfortunately, it doesn't work on the browser platform. It gives a "module is not defined" error. I will try to port my application to node.js platform to make this work.
yeah , i wasnt entirely sure. But with node it should work 100%
0

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;
  }

}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.