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
}
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 .
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']; }
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]];
}
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);
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
}
}
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);
Use this simple code:
var myFinalObj = {};
myArray.forEach(function(key){
myFinalObj[key] = startingObj[key].default;
});
console.log(myFinalObj)
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)
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.