2014-01-29 45 views
2

enter image description here
自定義對象陣列I具有表如上所見,我想創建自定義陣列將值傳遞。創建jQuery.map()函數

目前我使用下面的代碼行:

var arr = $('input[type=text].editfield').map(function() { 
     return this; 
    }).get(); 
    var objArr = jQuery.map(arr, function (i) { 
     return { 
      myDate: i.parentElement.previousSibling.previousSibling.previousSibling.previousSibling.previousSibling.previousSibling.childNodes[0].textContent, 
      myValue: i.value 
     } 
    }).get(); 

,我預計分別有所有項目的物體在我的網格日期和值的屬性的數組。

但是有些事情是錯誤的,我無法解決。例如上面的代碼中說,「jQuery.map(...)。得到的是不是一個函數

我怎樣才能更正我的代碼執行正確的操作?

回答

7

沒有必要對靜態jQuery.map()功能使用獲得(),它返回一個合適的陣列,而.map()返回您擁有jQuery對象插件的方法來調用.get()得到一個數組。


也沒有必要使用2個循環,

var objArr = $('input[type=text].editfield').map(function (idx, i) { 
    //the element selection used here is cruel, if you can share the html used we can help you to clean it 
    return { 
     // you can try myDate: $(this).parent().prevAll().eq(5).text() - not testable without the html and what is the expected output 
     myDate: i.parentElement.previousSibling.previousSibling.previousSibling.previousSibling.previousSibling.previousSibling.childNodes[0].textContent, 
     myValue: i.value 
    }; 
}).get(); 
+0

這就是我一直在尋找確切的答案。非常感謝你。 – user3021830

+0

也非常感謝你在評論中提出的改進。這兩個對我都很好。 – user3021830