There is an array of the form

[File, File, File] 0:File 1:File 2:File 

How to add an object with a given key value? Try so

 var obj = {}; obj[image_id] = file; images.push(obj); 

I get

 [Object] 0:Object 86:File 

If so

 images[image_id] = file; 

I get

 [86: File] 86:File 

Closed due to the fact that the essence of the issue is incomprehensible to the participants by Grundy , Dmitriy Simushev , aleksandr barakin , Pavel Mayorov , Saidolim 5 May '16 at 12:27 .

Try to write more detailed questions. To get an answer, explain what exactly you see the problem, how to reproduce it, what you want to get as a result, etc. Give an example that clearly demonstrates the problem. If the question can be reformulated according to the rules set out in the certificate , edit it .

  • and what result is expected? - Grundy
  • An array of objects - Guest
  • Well, so you get it = and in both of your examples. What in their results do not like? - Grundy
  • Initially, it was [File, File, File] 0:File 1:File 2:File I needed to be able to add objects to the array + change existing elements in the array, but in the end I got this [86: File] 86:File Instead of [File] 86:File As a result, for example, if you drive such an array through each, I received a count of elements from 0 to 86 :) - Guest
  • so you have the first option does just what you want, why even the second was to invent? - Grundy

3 answers 3

try this: this code inserts the value 2 between 1 and 3

 var my_array = [0,1,3,4]; var start_index = 1; var number_of_elements_to_remove = 0; my_array.splice(start_index, number_of_elements_to_remove, 2); console.log(my_array); // [1,2,3,4]; 
  • like he asked another - Jean-Claude
  • @ stas.t, splice - far from easy way - Grundy
  • I agree that I screwed up. by inattention did not understand what is required - spectre_it
  • @Grundy, I beg your petition if, because of my inexperience, I misled someone. really thought that my code would help the man understand the question. - spectre_it

 var arr = []; var filesArr = ['file.jpg', 'secfile.jpg', 'elsefile.jpg']; filesArr.forEach(function(element) { var obj = {}; obj['image_id'] = element; arr.push(obj); }); document.getElementById('res').innerHTML = JSON.stringify(arr); 
 <div id="res"></div> 

Option via map

 window.onload = function() { var arr = []; var filesArr = ['file.jpg', 'secfile.jpg', 'elsefile.jpg']; arr = filesArr.map(function(el) { return { image_id: el }; }); document.getElementById('res').innerHTML = JSON.stringify(arr); } 
 <div id="res"></div> 

  • JSON.stringify does not fit - Guest
  • @ Guest stringify is just a visualization of the fact that an array of objects is formed. Adding objects to an array inside the forEach method. - Jean-Claude
  • I did so var images = []; images.push({key: id, value: file}); var images = []; images.push({key: id, value: file}); - Guest
  • @ Jean-Claude, why not a map ? - Grundy
  • No idea, to be honest. And what are the differences in execution speed? - Guest

If I understand you correctly, the code can be like this:

 <script> var arr = new Array("File", "File", "File"); arr[3] = new Object(); arr[3]["mykey"] = "MyFile"; alert(arr[3]["mykey"]); // выведет MyFile alert(arr[3].mykey); // то же самое </script>