Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How can new objects be placed in an array as values?

Now I’m trying to add photos in my app.

For example, if there is one picture, the key is img_no_1, and the value is getting through the backend.

if can put up to 10 pictures, how can make img_no + number(The number of images.) according to the number of pictures?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

I want to make a Array like imageList.

If there are five pictures,

const image_no = [145, 331, 392, 2, 39];
const imageList = [];


--------------------------------------------------------
imageList = [{
             img_no_1: 145,
             img_no_2: 331,
             img_no_3: 392,
             img_no_4: 2,
             img_no_5: 39
     }];

Thanks for your attention.

>Solution :

Here is a very naive example using map and then Object.fromEntries:

  1. use map to return an array of array values in the shape ["key", "value"]
  2. Convert this array of arrays into an object using Object.fromEntries
const image_no = [145, 331, 392, 2, 39];
const imageList = [Object.fromEntries(
  image_no.map((num, i) => [
    `img_no_${i+1}`,
    num
  ])
)];

console.log(imageList)

If image_no is an array of image numbers:

const image_nos = [
  [145, 331, 392, 2, 39],
  [145, 331, 392, 2, 39]
];
const imageList = image_nos.map(image_no => Object.fromEntries(
  image_no.map((num, i) => [
    `img_no_${i+1}`,
    num
  ])
));

console.log(imageList)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading