user3021830
user3021830

Reputation: 2924

Create custom object array with jQuery.map() function

enter image description here
I have table as seen above and I want to create a custom array to pass values.

Currently I am using the following lines of code:

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();

and I expect to have an array of objects of all items in my grid with Date and Value as properties respectively.

But something is wrong and I can not solve. For example the code above says "jQuery.map(...).get is not a function"

How can I correct my code to perform the correct action?

Upvotes: 2

Views: 9555

Answers (1)

Arun P Johny
Arun P Johny

Reputation: 388316

There is no need to use .get() on the static jQuery.map() function, it returns a proper array, while the plugin method .map() returns a jQuery object on which you have to call .get() to get an array.


Also there is no need to use 2 loops,

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();

Upvotes: 7

Related Questions