tags:

views:

65

answers:

2

hello,

I have populated an array with databack from a webservice and need to populate the UI based on the last 6 years, even if they do not exist in the data. The Data coming back can be in terms of the UI incomplete, so what I need to do is insert values into the array for the "missing pieces".

Example

Data returns {2010, 2007}

What my javascript code must do is loop through the data returned, check the values of the indices, and insert values where needed.

UI returns (2010, 2009, 2008, 2007, 2006, 2005}

Also in 3 years when the dates have changed (current year is 2013)

Data returns {2011, 2009, 2008} UI returns {2013, 2012, 2011, 2010, 2009, 2008}


The array is populated with a return from a webservice, the webservice only puts values into the array that exist in the database, but the UI needs to display all values from the range 2010-2006 (ie data from the last 6 years, so in the future it could be 2015-2011)

A: 

You need to define your rules. For example, what will you do in the case (2020, 2006)? Now you can't just get 6 items by filling in the gaps. Another example, what was the rule that leads you to add 2010 rather than 2005 to your example?

What would you do for input like (2005, 2008, 2006)? Is that order important?

Once you can clearly state the rules then the algorithm might fall ou, for example.

1). Sort in descending order - my example (2008, 2006, 2005)

2). Count how many extra values needed - 3 in my case.

3). Start at lowest value, look for gaps. Fill in the gaps until you have added the missing items.If any missing items left over add them at the start.

(2010, 2009, 2008, 2007, 2006, 2005)

implementing that in Javascript (or any such language) would be pretty trivial.

djna
Why all the hassle if `[2010, 2009, 2008, 2007, 2006, 2005]` is the required constant outcome? *"6 elements with max = 2010 and min = 2005"* is just that. o_O
Tomalak
what about the cases where a contiguous set doesn't work? (eg. 2020, 2010, 2005) Anyway the point I'm trying to make is that the questioner needs to specify more completely his expectations. If he did that correctly then implementation is likely to be easier.
djna
@djna: Ahh the joys of shifting unclear requirements :-)
Tomalak
A: 

If I understand correctly, you want to display the last 6 years, filling in data for whatever years you have data for. Easy enough:

var now = new Date(),
    year = now.getFullYear(),
    minYear = year - 6,
    data = getFromWebService(),
    thisData,
    i = 0,
    obj = {};

for (; i < data.length; i++)
{
    obj[data[i].year] = data[i];  // This effectively creates a year-indexed "array" of your objects
}

for (; year > minyear; year--)
{
    if (obj[year])
    {
        // There is data with the index 'year'
        // Do stuff with the data
    }
    else
    {
        // There is no data with the index 'year'
        // show the default UI
    }
}

This gets the current year, loops backward until it reaches 6 years previous, and tests to see if you have data for each of those years. You will need to fill in your own UI/data manipulation code, but this is the basic structure you'll need.

Ryan Kinal
ryan, thanks that looks close to what i need, the issue is that the year isnt the index of the array, but a property in an object located in the array, how would you do that?
Isaac Levin
My answer has been edited to include a loop that creates a year-indexed "array" of your objects.
Ryan Kinal