Reputation: 1113
I have an array, which need to be the keys in my object. I also have an object with the same keys and a default property, which I need to set as the value to my keys in the object I need to build. I'm stuck at how to loop and set the key/values in the empty object. Thanks!!
myFinalObj
starts off as an empty object.
My starting object
var startingObj = {
'one' : {'default': 1, 'name': 'un'},
'two' : {'default': 2, 'name': 'deux'},
'three': {'default': 3, 'name': 'trois'}
}
My Array
var myArray = ['one', 'two', 'three'];
My finished Object
var myFinalObj = {
'one' : 1,
'two' : 2,
'three' : 3
}
Upvotes: 0
Views: 38
Reputation: 13095
var myFinalObj = myArray.reduce((memo, name) => {
memo[name] = startingObj[name].default
return memo
}, {})
Quoting from MDN :
The reduce() method applies a function against an accumulator and each value of the array (from left-to-right) to reduce it to a single value.
Arrow functions are a recent addition to javascript, and if your environment does not support them you can use the more verbose but equivalent :
var myFinalObj = myArray.reduce(function(memo, name) {
memo[name] = startingObj[name].default
return memo
}, {})
For IE < 9 the reduce function is also not available but you can use the polyfill available in the same MDN article .
Upvotes: 2
Reputation: 5606
Is this what you're trying to do? Based on your test case listed above it seems to work.
var startingObj = {
'one' : {'default': 1, 'name': 'un'},
'two' : {'default': 2, 'name': 'deux'},
'three': {'default': 3, 'name': 'trois'}
}
var myArray = ['one', 'two', 'three'];
var return_Obj = {}
for (var i = 0; i < myArray.length; i++){
for (var key in startingObj){
if (key == myArray[i]){
var val = startingObj[key]['default']
break
}
}
return_Obj[myArray[i]] = val
}
console.log(return_Obj)
Upvotes: 0
Reputation: 6467
Use this simple code:
var myFinalObj = {};
myArray.forEach(function(key){
myFinalObj[key] = startingObj[key].default;
});
console.log(myFinalObj)
Upvotes: 0
Reputation: 430
function buildObj(startObj, keyArr, finalObj) {
var currKey;
for (var i = 0; i < keyArr.length; i++) {
currKey = keyArr[i];
finalObj[currKey] = startObj[currKey]['default'];
}
}
var myFinalObj = {};
buildObj(startingObj, myArray, myFinalObj);
Upvotes: 0
Reputation: 10722
Try this:
var startingObj = {
'one' : {'default': 1, 'name': 'un'},
'two' : {'default': 2, 'name': 'deux'},
'three': {'default': 3, 'name': 'trois'}
}
var myArray = ['one', 'two', 'three'];
var myFinalObj = {}
for(n in myArray){
if(startingObj.hasOwnProperty(myArray[n])){
myFinalObj[myArray[n]] = startingObj[myArray[n]].default
}
}
Upvotes: 0
Reputation: 2655
Recommend using for(.. in ..)
or for (var i=0;i<..;i++)
as Array.forEach
might not be supported in older browsers.
var myArray = ['one', 'two', 'three'];
var startingObj = {
'one' : {'default': 1, 'name': 'un'},
'two' : {'default': 2, 'name': 'deux'},
'three': {'default': 3, 'name': 'trois'}
}
var myFinalObj = {};
for (key in myArray) {
myFinalObj[myArray[key]] = startingObj[myArray[key]].default;
}
console.log(myFinalObj);
Upvotes: 0
Reputation: 4339
You can set property keys using obj["key"]=value
syntax, so this should work:
var myFinalObject={};
for(var i=0;i<myArray.length;i++){
myFinalObj[myArray[i]]=startingObj[myArray[i]];
}
Upvotes: 0
Reputation: 738
var startingObj = {
'one': {
'default': 1,
'name': 'un'
},
'two': {
'default': 2,
'name': 'deux'
},
'three': {
'default': 3,
'name': 'trois'
}
}
var myArray = ['one', 'two', 'three'];
var finalObject = {};
myArray.forEach(function(value){
finalObject[value] = startingObject[value]['default'];
}
Upvotes: 0