0

How do I convert ["one","two","three"] into {one:"one", two:"two", three:"three"}

import stringArray from './a.js';

class b {
 hashmap = stringArray// convert this into Object here inline.
}

Before you jump I know of for how to achieve this in say constructor() with tricks like forEach, for in loop etc. Is there a simple one line code to achieve this in the class property not inside a function.

aWebDeveloper
  • 36,687
  • 39
  • 170
  • 242

4 Answers4

2

Lodash

You can use _.zipObject. It accepts two arrays - one for keys, another for values but if you just use the same array twice you'd get matching pairs:

const arr = ["one","two","three"];

const obj = _.zipObject(arr, arr);
console.log(obj);
<script src="https://cdn.jsdelivr.net/npm/lodash@4.17.15/lodash.min.js"></script>

Plain JavaScript

You can use Object.fromEntries to do the same thing. It works with an array of key-value pairs, so you'll have to transform yours to that:

const arr = ["one","two","three"];

const matchingKeyValuePairs = arr.map(x => [x, x]);

const obj = Object.fromEntries(matchingKeyValuePairs);
console.log(obj);

Also you can use Array#reduce to generate an object with a computed property name:

const arr = ["one","two","three"];

const obj = arr.reduce((acc, item) => ({...acc, [item]: item}), {});
console.log(obj);
Community
  • 1
  • 1
VLAZ
  • 26,331
  • 9
  • 49
  • 67
  • I am trying to do it in class member declaration so multi line code won't work. But I will go with your answer if you change it – aWebDeveloper Apr 15 '20 at 07:53
  • What do you mean? Do you want to add the array as properties to the current instance? – VLAZ Apr 15 '20 at 07:55
  • class b { hashmap = _.zipObject(arr, arr); } – aWebDeveloper Apr 15 '20 at 07:57
  • Why does it matter what are you assigning to? You're interested in the data being correct - whether you produce a variable or an object property or whatever, the [the result is the same](https://jsbin.com/qunuxokize/edit?js,console) – VLAZ Apr 15 '20 at 08:01
  • It doesn't in this case but it will with other multiline code cases. – aWebDeveloper Apr 15 '20 at 08:06
  • I'm afraid I don't understand what the issue is. [Object.entries](https://jsbin.com/dojerutoyo/edit?js,console) and [reduce](https://jsbin.com/tobalidawi/2/edit?js,console) both work. What is the issue with multilines? – VLAZ Apr 15 '20 at 08:14
1

Try this:

const arr = ["one","two","three"]
let obj = {}
arr.forEach(item => {obj[item] = item})
document.write(JSON.stringify(obj))
Henke
  • 4,445
  • 3
  • 31
  • 44
Sayooj V R
  • 2,255
  • 2
  • 11
  • 23
1
data = ["one","two","three"];
data = data.map(e => [e,e]) // keyvalue pairs [["one","one"],["two","two"],["three","three"]]
data = Object.fromEntries(data); // {"one":"one","two":"two","three":"three"}

map will convert each element of your input array to a structure you want.

In this case, we want to convert each element to an array with the element repeated twice in it

Object.froEntries will convert a list of key-value pair to an Object

This can be also done with the plain old for loop

data = ["one","two","three"];
obj = {};
for(let i = 0; i < data.length ; i++){
   obj[data[i]] = data[i];
}

ifelse.codes
  • 2,289
  • 23
  • 21
0

Lodash has the _.keyBy() function, that creates an object from an array by generating keys from the values via the function supplied (the iteratee). The default iteratee is _.identity(), which returns the value.

const arr = ["one","two","three"];

const obj = _.keyBy(arr);

console.log(obj);
<script src="https://cdn.jsdelivr.net/npm/lodash@4.17.15/lodash.min.js"></script>
Ori Drori
  • 183,571
  • 29
  • 224
  • 209