I have a object like where I declear some properties and value.
var obj = {
"country" : "USA",
"CApital" : "WDC",
"President" : "Trump"
}
Now I am adding in this way, I make one id and in that id I want to add my object property and values.
var myId = obj.President;
this.newObj[myId] = {};
Object.each(obj, function(key, value) {
this.newObj[myId][key] = value;
});
My output shoud be
obj :{
Trump :{
"country" : "USA",
"CApital" : "WDC",
"President" : "Trump"
}
}
There is no such thing as Object.each.
Simplest is use Object.assign()
to merge copy of original object to new object
let obj = { "country" : "USA", "CApital" : "WDC", "President" : "Trump" } let newObj= {[obj.President] : Object.assign({}, obj)} console.log(newObj)
I understood you want to create a new object with a property value equals to source object identifier evaluation, and inside that property goes the initial object. Something like this:
const obj = { country: 'usa', capital: 'dc', president: 'trump' }; function myPropAndObjectValues(obj, prop) { return { [obj[prop]]: {...obj} }; } console.log(myPropAndObjectValues(obj, 'president'));
Pd. Object.each
doesn't exist.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.