javascript - How to add index and value to a object? -


How to add an index and value to an object, below is my code, I tried to add index and value to the loop , But this error shows is not an undefined object ($ data.file_list_image.i.sequence) , why and how to fix it?

$ data = {file_list_image: {0: {sequence: 1, introduction: 1: {sequence: 1, intro:},}}

JS

  var $ el = $ content_list.find ('List of content-gallery-file-list'); Var file_length = $ el.length; Var file_image_length = 0; Var file_embed_youtube_length = 0; {If ($ el.eq (i) .attr ('data-type') == 0) {// console.log ('img for Var i = 0; i & lt; file_length; i ++) '); File_image_length ++; } And if ($ el.eq (i) .attr ('data-type') == 1) {// console.log ('video'); File_embed_youtube_length ++; }} Var $ data = {}; (Var i = 0; i & lt; file_image_length; i ++) {var $ file_list = $ el.filter (function () {return $ (this) .attr ('data-type') == 0 & amp; $ (This) .attr ('data-tmp-id') == i;}); Var sequence = $ file_list.attr ('data-sequence'); Var introduction = $ file_list.find ('intro textarea'). Val (); $ Data.file_list_image.i.sequence = Sequence; $ Data.file_list_image.i.intro = Introduction; } Console.log ($ data);  

HTML

  & lt; Div class = "content_list" & gt; & Lt; Div class = "content-list-gallery-file-list" data-type = "0" data-tmp-id = "1" data-sequence = "0" & ​​gt; & Lt; Div class = "intro" & gt; & Lt; Textarea & gt; & Lt; / Textarea & gt; & Lt; / Div & gt; & Lt; / Div & gt; & Lt; Div class = "content-list-gallery-file-list" data-type = "0" data -tmp-id = "0" data-order = "1" & gt; & Lt; Div class = "intro" & gt; & Lt; Textarea & gt; & Lt; / Textarea & gt; & Lt; / Div & gt; & Lt; / Div & gt; & Lt; Div class = "content-list-gallery-file-list" data-type = "1" data-tmp-id = "2" data-sequence = "2" & gt; & Lt; Div class = "intro" & gt; & Lt; Textarea & gt; & Lt; / Textarea & gt; & Lt; / Div & gt; & Lt; / Div & gt; & Lt; / Div & gt;  

How can this be done at the right time by introducing object properties:

  var $ data = {}; $ Data.file_list_image = {}; For (var i = 0; i <5; i ++) {$ data.file_list_image [i] = {}; $ Data.file_list_image [i] ["sequence"] = "sack" + i; $ Data.file_list_image [i] ["Introduction"] = "in" + i; } Console.log ($ data);  

By the way, $ data.file_list_image [i] ["sequence"] (with the same intro ) can also be < Code> $ data.file_list_image [i]. Resultant

.


Comments

Popular posts from this blog

java - Can't add JTree to JPanel of a JInternalFrame -

javascript - data.match(var) not working it seems -

javascript - How can I pause a jQuery .each() loop, while waiting for user input? -