0

myObj is an object which contains some parameters

myObj:
    {
     person: "John", 
     age: "20"
    };

Now, I need to create a new object similar to original object (only slight changes to values), something like

newObj:
    {
     person: "John" + GetVal();
     age: "20" + GetVal();
    };

I tried something like:

var newObj = {};
angular.forEach(myObj,
                    function (value, key) {
                        _.extend(newObj, { key: value + GetVal() });
                    });

But this produces something like,

newObj:
{
  key: "John" + GetVal();
}

But I am expecting an object

newObj:
{
  person: "John" + GetVal();
}

Notice that key is taken as such but instead it should be the parameter name of the original object.

Any ideas how to achieve that ?

2
  • Why not just do newObj.person = "John" + GetVal();? Commented May 3, 2017 at 14:53
  • I don't know the exact property name of myObj. It could be anything. All I want is a similar object with every property's value modified. Commented May 3, 2017 at 14:58

3 Answers 3

1

You can use Object.keys. Please try this:

var myObj = {
     person: "John", 
     age: "20"
};
var newObj = {};

angular.forEach(Object.keys(myObj), function (value, key) {
         newObj[value] = myObj[value] + ' someValue';
     });

console.log(newObj)
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>

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

5 Comments

did u mean newObj[key] = myObj[value] + GetVal() ?
No, Object.keys returns an array which items are object key names and you want those values so it should be value. If you use key in such case it will give you array indexes (0, 1 ..)
Great answer. Thanks for the detailed explanation. Understood, tried and worked great :) Bliss...
Having a doubt. What is the difference between using _.extend and simply using newObj[value] = oldObj[value]. Both seems to create a new property called value if one doesn't exist.
_.extend is 'underscore' library method which shallowly copy all the properties in the source objects over to the destination object (check underscore doc for more details: underscorejs.org/#extend). So it's helpful if you want to make a copy of entire object without modifications. In your case there is no need to use it since you iterate over object properties with forEach loop one by one and use them to build new object. You don't need to copy object but to create new object based on another object properties and you don't need any library to add object property in JS :)
1

The problem is key is treated as a litteral not an expression, you must use [key] instead in your solution to make it work.

var myObj = {
  person: 'John',
  age: 20
};

const GetVal = () => 3;

var newObj = {};
angular.forEach(myObj,
    function (value, key) {
        _.extend(newObj, { [key]: value + GetVal() });
    });
console.log(newObj)
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.4/lodash.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>

Otherwise, using _.each (as in _.extend), you can do the following :

var new_obj = {};
_.each(obj, (v, k) => new_obj[k] = v + getVal())

var obj = {
  person: "John", 
  age: "20"
};

const getVal = () => '_prefix';

var new_obj = {};
_.each(obj, (v, k) => new_obj[k] = v + getVal())

console.log(new_obj)
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.4/lodash.min.js"></script>

1 Comment

I did not know that newObj[key] = val would create a new property called key in newObj and then assign a value to it. Now, understood better
0

You could try Object.assign() like this:

var myObj = {
    person: "John", 
    age: "20"
};

function GetVal(val) {
  return val;
}

myObj = Object.assign({}, myObj, {
    person: myObj.person + GetVal(" Lastname"),
    age: myObj.age + GetVal(" years old")
});

console.log(myObj);

You should checkout the documentation on Object.assign() over at MDN. An added benefit of this approach is that your data is not mutated. Avoiding data mutation when possible is always a plus.

2 Comments

the problem is that I don't know the property names of myObj. It could be anything. So, you cannot hardcode the person: and age just like you did above.
My apologies, I didn't see in your question where it mentioned dynamic key names. Definitely use Object.keys() to obtain those keys then. On another note, angular.forEach is much less performant than a simple for loop.

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.