2014-01-29 47 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