0

I have an original two-dimensional array which represents the 100% version budget per year, looking like:

budget = [['2001-01-01', 100], ['2001-01-02', 110], ... , ['2001-12-31', 140]]

Now I need to create subarrays of the budget for projects. For example:

project1 = [['2001-01-01', 10], ['2001-01-02', 11]]

meaning, that it goes for 2 days and uses 10% of the available budget.

I create the project arrays by the following function:

project1 = [[]];
project1 = new_project(budget, 0, 1, 0.1);

function new_project(origin, start, end, factor) {
    var result = [[]];
    result = origin.slice(parseInt(start), parseInt(end)).map(function (item) {
        return [item[0], parseInt(item[1]) * factor]
    });
    return result;
}

Problem

How can I decrease my budget array now by the values of my created project? I need to modifiy budget on the fly by calling new_project in order to get:

budget = [['2001-01-01', 90], ['2001-01-02', 99],..., ['2001-12-31', 140]]

jsfiddle of the setup

http://jsfiddle.net/gcollect/7DAsL/

6
  • Why are you calling parseInt on the 'start', 'end' and 'item[1]' arguments? You don't need to do that. Commented Jun 15, 2014 at 13:27
  • you're right. Sometimes start is defined by year/12. year is 365. year /12 gets me 30,41.. thats why Commented Jun 15, 2014 at 13:31
  • So you want to filter out some of the budget array members which are or aren't in the new_project result? Commented Jun 15, 2014 at 13:31
  • I want to decrease the second element of the budget array, by the the project arrays second elements. Commented Jun 15, 2014 at 13:33
  • So it'll be ['2001-01-02', 99]? Commented Jun 15, 2014 at 13:37

2 Answers 2

1

You can use the built-in array forEach() method to loop over the subset of the budget (ie project1) and change the original budget's values. I've made a function called decreaseBudget() which will do that for you:

function decreaseBudget(original, subset) {
    // Loop over the subset's items.
    subset.forEach(function(item, index){
        // Get the original amount out of the original item.
        // As we are looping over subset it will always have less items then the original 
        // so no need to check if the index is valid.
        var originalItemAmount = original[index][1];
        // Decrease the amount and set it back in it's original place
        original[index][1] = (originalItemAmount - item[1]); 
    });
};

Call it with the budget and the project1 arrays as arguments to change the value of the budget array. Take a look at the updated fiddle: http://jsfiddle.net/7DAsL/4/

If you prefer decreaseBudget() won't change the original array but return a copy you could use the array .slice() method to make a shallow copy of the original, make the changes to the copy and return it.

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

Comments

1

I got the answer. see http://jsfiddle.net/gcollect/7DAsL/2/

solution

    function new_project(origin, start, end, factor) {
        var result = [
            []
        ];

        result = origin.map(function (item) {
            return [item[0], (item[1] * factor)]
        });
        for (var i = 0; i < result.length; i++) {
            origin[i][1] = parseInt(origin[i][1]) - parseInt(result[i][1]);
        }
        result = origin.slice(parseInt(start), parseInt(end));
        return result;

    };

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.