Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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?

Regards.

share|improve this question

1 Answer 1

up vote 3 down vote accepted

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();
share|improve this answer
    
That was the exact answer I have been looking for. Thank you very much. –  user3021830 Jan 29 at 9:47
    
Also thank you very much for the improvement suggested in the comments.These two both worked well for me. –  user3021830 Jan 29 at 9:52

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.