0

I got item a, which is an javascript object I think?

const item = myItem.getAsFile();

which returns something like

lastModified: 1521979332955
name: "image.png"
size: 15254
type: "image/png"
webkitRelativePath: ""

Next I create a object url:

const preview = window.URL.createObjectURL(item);

which returns "blob:https://localhost:3000/..."

Now I want to "push" preview: "blob:https://localhost:3000/..." inside my const item, so that next to lastModified, name, size, etc. preview: will be listed.

How can I do this?

Marian Rick
  • 3,350
  • 4
  • 31
  • 67
  • 4
    `item.preview = preview` will work. – Arup Rakshit Mar 25 '18 at 12:09
  • Change `const` to `var` or `let` so you can change item. Then use `item.preview = preview` to add the attribute to the object. – Tostifrosti Mar 25 '18 at 12:11
  • 1
    Where are the arrays advertised in the question title?! – axiac Mar 25 '18 at 12:11
  • 6
    @Tostifrosti `const` [does not prevent](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/const#Description) the modification of `item`. It just disables storing another value in `item`. – axiac Mar 25 '18 at 12:13
  • It is really simple thing, seems it must not be a question. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Working_with_Objects – Vayrex Mar 25 '18 at 12:13
  • @axiac Oh my bad! You apparently need to use Object.freeze() to make the object completely immutable. – Tostifrosti Mar 25 '18 at 12:18
  • Possible duplicate of [Add a property to a JavaScript object using a variable as the name?](https://stackoverflow.com/questions/695050/add-a-property-to-a-javascript-object-using-a-variable-as-the-name) – FisNaN Mar 25 '18 at 12:28

1 Answers1

2

You can do the following:

item.preview = preview;

if you don't want to change the origin item, you can use Object.assign

let newItem = Object.assign({}, item, {preview: preview})
Marian Rick
  • 3,350
  • 4
  • 31
  • 67
欧阳维杰
  • 1,608
  • 1
  • 14
  • 22