2

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?

1 Answer 1

7

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();
Sign up to request clarification or add additional context in comments.

2 Comments

That was the exact answer I have been looking for. Thank you very much.
Also thank you very much for the improvement suggested in the comments.These two both worked well for me.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.