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.

2
  • 1
    stringArray.reduce((a, c) => (a[c] = c, a), {}) Commented Apr 15, 2020 at 7:43
  • You can also use an IIFE, for example: hashmap = (() => {.. return "something"})() Commented Apr 15, 2020 at 8:00

4 Answers 4

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/[email protected]/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);

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

6 Comments

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
What do you mean? Do you want to add the array as properties to the current instance?
class b { hashmap = _.zipObject(arr, arr); }
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
It doesn't in this case but it will with other multiline code cases.
|
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];
}

Comments

1

Try this:

const arr = ["one","two","three"]
let obj = {}
arr.forEach(item => {obj[item] = item})
document.write(JSON.stringify(obj))

Comments

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/[email protected]/lodash.min.js"></script>

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.