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 object from array

  • Thread starter Thread starter Hussain Dehgamwala
  • Start date Start date
H

Hussain Dehgamwala

Guest
I want to create an object from a list inside an array. I have an array which is dynamic and supposed to look like this:

var dynamicArray = ["2007", "2008", "2009", "2010"];

And I want to make an object like this with some JavaScript ES6:

Code:
const obj = {
    2007: {
        x: width / 5,
        y: height / 2
    },
    2008: {
        x: (2 / 5) * width,
        y: height / 2
    },
    2009: {
        x: (3 / 5) * width,
        y: height / 2
    },
    2010: {
        x: (4 / 5) * width,
        y: height / 2
    }
}

Don't worry about the inner objects. I just want to create a structure like this:

Code:
 obj = {
      2007: ...,
      2008: ...,
      ...
    }

<p>I want to create an object from a list inside an array. I have an array which is dynamic and supposed to look like this:</p>
<p><code>var dynamicArray = ["2007", "2008", "2009", "2010"];</code></p>
<p>And I want to make an object like this with some JavaScript ES6:</p>
<pre><code>const obj = {
2007: {
x: width / 5,
y: height / 2
},
2008: {
x: (2 / 5) * width,
y: height / 2
},
2009: {
x: (3 / 5) * width,
y: height / 2
},
2010: {
x: (4 / 5) * width,
y: height / 2
}
}
</code></pre>
<p>Don't worry about the inner objects. I just want to create a structure like this:</p>
<pre><code> obj = {
2007: ...,
2008: ...,
...
}
</code></pre>
 

Latest posts

Top