0

I have a loop which I want to compile an object for the frontend.

The loop sets a user, hours and timestamp variable from which I want to dynamically create the new object.

This is what I have so far which does not work as it obviously just replaces the user. Any help would be great as the best way to go about this, thanks.

var Things = { /* big dump of data */ };
var usersHours = {};

for (var i = Things.length - 1; i >= 0; i--) {

    var user = Things[i].user;
    var hours = Things[i].hours;
    var timeStamp = Things[i].timeStamp;

    var hoursAdd = {};

    hoursAdd[timeStamp] = hours;
    usersHours[user] = hoursAdd;

};


// example of wanted final output

var usersHours = {

    user1 : {
        1406178757855 : 10:00,
        1406178743473 : 04:00,
        1406178759600 : 04:44
    },
    user2 : {
        1406178475847 : 01:30,
        1406193847384 : 07:00,
        1406984783487 : 08:44
    },
    user3 : {
        1406173847787 : 01:40,
        1406139847873 : 07:14,
        1406183748374 : 08:34
    }
}
2
  • 1
    what does Things[i].user give you as it will only be overwriting if it is the same each time, could you show an example of what is in Things Commented Nov 10, 2014 at 4:15
  • @Quince thanks for you response. Things in this case is a Firebase array. The loop is a little more complicated than what I have in the question to try and make the question simple. Commented Nov 10, 2014 at 4:24

2 Answers 2

3

So you can test if the user already has data and if they don't create an blank object ready to except the new data being added. With the example below if any timestamps are the same for the same user then the data will be overridden.

Things = [{
    user: "user1",
    hours: "5",
    timestamp: "12312312322"
  }, {
    user: "user1",
    hours: "2",
    timestamp: "12322312322"
  }, {
    user: "user1",
    hours: "1",
    timestamp: "12312392322"
  }, {
    user: "user2",
    hours: "5",
    timestamp: "12312312322"
  }, {
    user: "user2",
    hours: "2",
    timestamp: "12322312322"
  }, {
    user: "user2",
    hours: "1",
    timestamp: "12312319322"
  }, {
    user: "user3",
    hours: "5",
    timestamp: "12312312322"
  }, {
    user: "user3",
    hours: "2",
    timestamp: "12322312522"
  }, {
    user: "user3",
    hours: "1",
    timestamp: "123123222322"
  },

]

var usersHours = {};
for (var i = Things.length - 1; i >= 0; i--) {

  var user = Things[i].user;
  var hours = Things[i].hours;
  var timeStamp = Things[i].timestamp;



  //test if user has already got data
  if (!usersHours[user]) {
    usersHours[user] = {};
  }
  

//set/or possibly override a timestamp for the user with the hours
  usersHours[user][timeStamp] = hours;

};

console.log(usersHours);

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

Comments

0

If you want multiple dynamic values, you'll want to use an array instead of a key. Keys have one pair, arrays can hold the data you need it to.

2 Comments

thanks for your response. So in this case I would build multiple arrays for each user?
Yep. Data should be layered in Array's, so anytime you think you might be specifying more information about a key, use an array instead.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.