0

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
}
4
  • if each position of the startingObj, you have the key, why you need myArray? Commented Nov 20, 2015 at 19:03
  • Maybe I don't? But then how would I loop the data to get the object desired? Commented Nov 20, 2015 at 19:04
  • What have you tried? We're not here to do your (home)work for you, but to answer specific questions about problems you're having. Try some things yourself, you might come up with the answer on your own, and if not, identify specific questions you can ask. Commented Nov 20, 2015 at 19:06
  • 1
    you would get 10 different answer here you haven't showed us what you've tried to get the result Commented Nov 20, 2015 at 19:07

8 Answers 8

2
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 .

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

2 Comments

Could you explain this answer please?
I have added an explanation. Please review and let me know if any more clarifications are required.
0

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']; 
}

Comments

0

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]];
}

Comments

0

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);

Comments

0

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
    }
}

Comments

0
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);

Comments

0

Use this simple code:

var myFinalObj = {};
myArray.forEach(function(key){
    myFinalObj[key] = startingObj[key].default;
});
console.log(myFinalObj)

Comments

0

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)

Comments

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.