0

I have a input form file picker which returns the following object output :

enter image description here

Image as text ->

FileList {0: File, 1: File, length: 2}
0: File {name: "Screenshot 2021-02-19 at 12.27.47 PM.png", lastModified: 1613717870763, lastModifiedDate: Fri Feb 19 2021 12:27:50 GMT+0530 (India Standard Time), webkitRelativePath: "", size: 81142, …}
1: File {name: "Screenshot 2021-02-18 at 12.11.00 PM.png", lastModified: 1613630510734, lastModifiedDate: Thu Feb 18 2021 12:11:50 GMT+0530 (India Standard Time), webkitRelativePath: "", size: 128961, …}
length: 2

I need to convert it to an array of objects of the form :

[{file0: file}, {file1: file}]

Please note that I don't need the length item present in the initial object in the final array

NeERAJ TK
  • 2,447
  • 1
  • 11
  • 25
  • 1
    Please don't present text as an image. – trincot Feb 19 '21 at 13:48
  • What did you try so far? – szczocik Feb 19 '21 at 13:48
  • @trincot I have updated it – NeERAJ TK Feb 19 '21 at 13:50
  • 5
    Are you sure that you want a different property name inside each of the objects in the output array? I would hate it if someone fed me that kind of data... I would recommend trying `var arr = Array.from(theFileList)` and see if that works for you. – Peter B Feb 19 '21 at 13:51
  • @szczocik I tried using iterating for loop in object items and filtering them but seems little bit complicated. Wanted to know if there is any easier solution that I missed out. – NeERAJ TK Feb 19 '21 at 13:51
  • 1
    I agree with PeterB: that is a horrible choice for an output format. You don't need those numerical suffixes... you already know the index from the position they have in the array. – trincot Feb 19 '21 at 13:52
  • Does this answer your question? [Iteration over FileList, React, Typescript](https://stackoverflow.com/questions/46977584/iteration-over-filelist-react-typescript) – Peter B Feb 19 '21 at 13:58
  • 1
    As a comment on: *"I need to convert it to an array of objects"* and *"note that I don't need the length item present in the initial object in the final array"* An array always has a (non-enumerable) `length` property. Which will automatically be set to 2 if you have an array with 2 elements. – 3limin4t0r Feb 19 '21 at 14:11
  • @3limin4t0r Yes – NeERAJ TK Feb 19 '21 at 14:52
  • Does this answer your question? [How to convert an Object {} to an Array \[\] of key-value pairs in JavaScript](https://stackoverflow.com/questions/38824349/how-to-convert-an-object-to-an-array-of-key-value-pairs-in-javascript) –  Feb 19 '21 at 15:32

2 Answers2

2
const objOfObjs = {
   "one": {"id": 3},
   "two": {"id": 4},
};

const arrayOfObj = Object.entries(objOfObjs).map((e) => ( { [e[0]]: e[1] } ));

i think it work for you.

Parth M. Dave
  • 853
  • 1
  • 5
  • 16
1

You could do with Array#map

const arr = [{name:1},{name:2}];

const res =  arr.map((item,ind)=>({[`file${ind}`]:item}));

console.log(res)
prasanth
  • 22,145
  • 4
  • 29
  • 53
  • 1
    A [`FileList`](https://developer.mozilla.org/en-US/docs/Web/API/FileList) does not have the `map` method. Use `Array.from(fileList).map(...)` instead. Or skip the `map` call, and pass the callback as second argument to [`Array.from()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/from) eg. `Array.from(fileList, (file, i) => { [\`file${i}\`]: file })` – 3limin4t0r Feb 19 '21 at 15:17