[英]Looping through object and setting as values in second object javascript
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. myFinalObj
从一个空对象开始。
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
}, {})
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. reduce()方法对一个累加器应用一个函数,该数组的每个值(从左到右)将其减小为单个值。
Arrow functions are a recent addition to javascript, and if your environment does not support them you can use the more verbose but equivalent : 箭头功能是javascript的最新 功能 ,如果您的环境不支持它们,则可以使用更详细但等效的功能:
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 . 对于IE <9,缩减功能也不可用,但是您可以使用同一MDN文章中可用的polyfill 。
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: 您可以使用obj["key"]=value
语法设置属性键,因此应该可以:
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. 建议使用for(.. in ..)
或for (var i=0;i<..;i++)
作为Array.forEach
这在旧版浏览器中可能不受支持。
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)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.