OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

Create a dictionary from two lists, with one list's values being the key and the other being the value

  • Thread starter Thread starter NomadsThings
  • Start date Start date
N

NomadsThings

Guest
I am trying to create a dictionary from two separate lists with one list being the key and the other being the value.

I am currently using the code below to achieve this but it isn't working.

Code:
            const objects = columnNames.map((value) => {
                const object: { [key: string]: any} = {};
                standardNames.forEach((key, index) => {
                    object[key] = value[index];
                });
                return object;
            });

The two lists I am trying to join together are:

Code:
const standardNames = ['Tests 1', 'Tests 2', 'Tests 3', 'Tests 4', 'Tests 5'];
const columnNames= ['Test 1', 'Test 2', 'Test 3', 'Test 4', 'Test 5'];

However what I get instead is:

Code:
[{"Tests 1":"T","Tests 2":"e","Tests 3":"s","Tests 4":"t","Tests 5":" "},{"Tests 1":"T","Tests 2":"e","Tests 3":"s","Tests 4":"t","Tests 5":" "},{"Tests 1":"T","Tests 2":"e","Tests 3":"s","Tests 4":"t","Tests 5":" "},{"Tests 1":"T","Tests 2":"e","Tests 3":"s","Tests 4":"t","Tests 5":" "},{"Tests 1":"T","Tests 2":"e","Tests 3":"s","Tests 4":"t","Tests 5":" "}]

<p>I am trying to create a dictionary from two separate lists with one list being the key and the other being the value.</p>
<p>I am currently using the code below to achieve this but it isn't working.</p>
<pre><code> const objects = columnNames.map((value) => {
const object: { [key: string]: any} = {};
standardNames.forEach((key, index) => {
object[key] = value[index];
});
return object;
});
</code></pre>
<p>The two lists I am trying to join together are:</p>
<pre><code>const standardNames = ['Tests 1', 'Tests 2', 'Tests 3', 'Tests 4', 'Tests 5'];
const columnNames= ['Test 1', 'Test 2', 'Test 3', 'Test 4', 'Test 5'];
</code></pre>
<p>However what I get instead is:</p>
<pre><code>[{"Tests 1":"T","Tests 2":"e","Tests 3":"s","Tests 4":"t","Tests 5":" "},{"Tests 1":"T","Tests 2":"e","Tests 3":"s","Tests 4":"t","Tests 5":" "},{"Tests 1":"T","Tests 2":"e","Tests 3":"s","Tests 4":"t","Tests 5":" "},{"Tests 1":"T","Tests 2":"e","Tests 3":"s","Tests 4":"t","Tests 5":" "},{"Tests 1":"T","Tests 2":"e","Tests 3":"s","Tests 4":"t","Tests 5":" "}]
</code></pre>
 

Latest posts

J
Replies
0
Views
1
Jeff P Chacko
J
Top