0

I Have an array of arrays and would like to return a dictionary like so.

function retFunc(array) {
var dict ={};
for(var i=0; i<=array.length-1;i++){
dict={[array[i][0]] : array[i][1]}
}
return dict
}  

retFunc([['a','b'],'['c','d']])

output
{ c : 'd'}

the return dict only returns the last key/value pair. I would like to return all key/value pairs but doing something like dict += {[array[i][0]] : array[i][1]} doesn't work.

1
  • You seem to have a typo Commented Oct 1, 2017 at 15:28

9 Answers 9

1

You are doing

dict={[array[i][0]] : array[i][1]}

That means you assign a new object to variable dict. Not appending! If you want to append to your dict object. Write this:

dict[array[i][0]] = array[i][1];
Sign up to request clarification or add additional context in comments.

Comments

1

It's very simple with Object.assign, .map(), spread syntax, and parameter destructuring:

function retFunc(arr) {
  return Object.assign({}, ...arr.map(([k, v]) => ({[k]: v})));
}

console.log(retFunc([['a','b'],['c','d']]));

Comments

0

you would have to do

function retFunc(array) {
var dict ={};
for(var i=0; i<=array.length-1;i++){
dict[array[i][0]] = array[i][1];
}
return dict
}  

console.log(retFunc([['a','b'],['c','d']]))

Comments

0

Instead use dict[array[i][0]] = array[i][1]; to map all key values.

function retFunc(array) {
var dict ={};
for(var i=0; i<=array.length-1;i++){
dict[array[i][0]] = array[i][1];
}
return dict
}  

var x = retFunc([['a','b'],['c','d']]);

console.log(x);

Comments

0

Try like this with object key = value assignment,

function retFunc(array) {
  var dict = {};
  for (var i = 0; i <= array.length - 1; i++) {
    dict[array[i][0]] = array[i][1];
  }
  return dict;
}

console.log(retFunc([
  ['a', 'b'],
  ['c', 'd']
]));

Comments

0

Use assignment as dict[array[i][0]] = array[i][1] to add a key:value pair to your dict Object

Remove the typo as well from the array.

function retFunc(array) {
  var dict = {};
  for (var i = 0; i <= array.length - 1; i++) {
    dict[array[i][0]] = array[i][1]
  }
  return dict
}

console.log(retFunc([
  ['a', 'b'],
  ['c', 'd']
]))

Comments

0

This is a perfect candidate for the Array.reduce function. Below is a commented example that shows how Array.reduce works for your example. You can also find documentation at MDN

const input = [
  ['a', 'b'],
  ['d', 'd'],
  ['e', 'f'],
];

// adds key/value from `arr` to dict.
function arrayToDict(dict, arr){
  dict = dict || {};
  // take an array of two items and turn it into a key value pair.
  let [key, value] = arr;
  // if you don't like es6 syntax, then this is equivalent
  /*
  var key   = arr[0];
  var value = arr[1];
  */
  
  dict[key] = value;
  return dict;
}

// this function can be used to add any one of our pairs to a dictionary

console.log('Example1: ', arrayToDict({}, ['a', 'b']));

// now we can use the Array Reduce function to add each pair to the dictionary.

let output = input.reduce(arrayToDict, {});
console.log('Output:', output);

Comments

0

You could use a direct assignment to the wanted key.

dict[array[i][0]] = array[i][1];

function retFunc(array) {
    var dict = {};
    for (var i = 0; i <= array.length - 1; i++) {
          dict[array[i][0]] = array[i][1];
    }
    return dict;
}

console.log(retFunc([['a', 'b'], ['c', 'd']]));

A more functional approach could include

function retFunc(array) {
    return Object.assign(...array.map(([k, v]) => ({ [k]: v })));
}

console.log(retFunc([['a', 'b'], ['c', 'd']]));

Comments

0

The Object.fromEntries() method transforms a list of key-value pairs into an object. link

Object.fromEntries(iterable);

var array = [
   ['a','b'],
   ['c','d']
];
var dict = Object.fromEntries(array);
console.log(dict);

// {a: "b", c: "d"}

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.