使用jQuery.map()函数创建自定义对象数组

use*_*830 2 javascript arrays jquery object

在此输入图像描述
我有如上所示的表,我想创建一个自定义数组来传递值.

目前我使用以下代码行:

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();
Run Code Online (Sandbox Code Playgroud)

我希望我的网格中的所有项目的对象数组分别以Date和Value作为属性.

但有些事情是错的,我无法解决.例如上面的代码说" jQuery.map(...).get不是函数 "

如何更正我的代码以执行正确的操作?

Aru*_*hny 7

没有必要在静态jQuery.map()函数上使用.get(),它返回一个正确的数组,而插件方法.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();
Run Code Online (Sandbox Code Playgroud)