1

Am building a nodejs app and it has very many clients with different timezones. I save all the data in Unix time stamp, when a client logs in i use the clients timezone to add or subtract to the unix time stamp stored in the database, so that it matches the clients time.

The user data (a document/object), has keys with Arrays and other objects aswell,

{
 name: "data",
 location": "us",
 activity: [],
 time: 1476301165252,
 subdoc: [{
          time: 1476301165252,
          someotherKeys: ....
         }]

}

How can i loop through this document and edit all the "time" keys(including inner objects and arrays aswell) and add or subtract a number to all "time" keys respectively.

The number will be uniform. meaning all the "time" keys will be iterated with the same number.

3 Answers 3

1

You'll want to use a recursive for...in loop to solve this, I think.

Something like this:

function changeTime (inputObj, newTimeVal) {
    var key;
    for (key in inputObj) {
        if (key === "time") {
            inputObj[key] = newTimeVal;
        }
        if (typeof inputObj[key] === 'object') {
            changeTime(inputObj[key], newTimeVal);
        }
    }
}

Might not be exactly right, but it should get you on the right path.

edit: If you only want to add or subtract from the original value, it's easy enough to use this same pattern:

function changeTime (inputObj, delta) { // we'll always add here-- you can pass in a negative or positive number to account for add or subtract
    var key;
    for (key in inputObj) {
        if (key === "time") {
            inputObj[key] = inputObj[key] + delta;
        }
        if (typeof inputObj[key] === 'object') {
            changeTime(inputObj[key], delta);
        }
    }
}
Sign up to request clarification or add additional context in comments.

5 Comments

I only want to add or subtract from the current value,
Objects in JS are passed by reference-- you're editing the same object you passed in.
Check out this example data jsfiddle.net/grnxex31 . Your method only outputs the edited keys, can you make your method return the same object together with keys changed.
@Tuna -- I think you are misunderstanding what is happening. When you pass an object or an array into a method, the method isn't editing a copy like a primitive-- it is editing the same object you passed in. My method doesn't return anything because it is editing the object you passed in directly-- that's how objects are passed in JS. Try console logging the value of userData before you pass it into the method, and then logging its value after it has been passed in-- I think you will see that userData has been updated/transformed after my function completes.
Please see this section of the MDN reference on functions for details for details-- it clearly states "...object references are values, too, and they are special: if the function changes the referred object's properties, that change is visible outside the function..."
1

You could do something like this:

    var obj = {
        name: "data",
        location: "us",
        activity: [],
        time: 1476301165252,
        subdoc: [{
            time: 1476301165252,
        }]

    };

function iterate(obj) {
    for (var property in obj) {
        if (obj.hasOwnProperty(property)) {
            if (typeof obj[property] == "object") {
                iterate(obj[property], property);
            } else {
                if (property === 'time') {
                    console.log(property + "   " + obj[property]);
                }
            }
        }
    }
}

iterate(obj)

https: //jsfiddle.net/uxqgd8cf/

4 Comments

Your method only outputs the edited "time" keys, can you make that method return the same object together with the edited keys.
if (property === 'time') { console.log(property + " " + obj[property]); } , inside this you can edit the time... which would update the orginal object...
Yes it works, look at a real example with my user data, it doesn't return the whole object jsfiddle.net/1gmx3kk0
your object is getting modified... check this one... look where i have placed my console.log statement and inside my if block i am updating the property value... jsfiddle.net/1gmx3kk0/1
-1

You have to use for in loop to loop objects in Javascript https://www.google.com.tr/url?sa=t&source=web&rct=j&url=https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for...in&ved=0ahUKEwiJ7q6WhtbPAhUkAsAKHXehDAkQFggaMAA&usg=AFQjCNGzYTP9ERuhLVoFVYR52HohZmZ0eQ&sig2=N_sNA5ZjKyeVmCjEisDNDQ

1 Comment

Can you please share some example code in context of my question

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.