1

While iterating over an object i get the error, Object.each is not a function, Here's my code:

$("#print").click(function() {
        $values = {};
        $("[data-type=text]").each(function(i,e){
            if($(e).val() !='') {
                $values[$(e).attr('name')] = $(e).val();
            }
        });
        console.log($values);

        $values.each(function(i,e){
            console.log(i.e);
        });
    });

moreover, i can't use for loop too, since i don't know the keys .

4
  • typo error, console.log(i,e); Commented Apr 7, 2018 at 19:58
  • 1
    $values is an object, why not use Object.values( $values ).forEach( /*...*/ ) or Object.keys( $values ).forEach( /*...*/ ) Commented Apr 7, 2018 at 19:58
  • It is forEach !!! Commented Apr 7, 2018 at 20:03
  • Using forEach Solved the issue. Commented Apr 7, 2018 at 20:09

1 Answer 1

1

In your code, $values is not a jQuery Object and .each() will not work.

Iterate over a jQuery object, executing a function for each matched element.

You will want to use $.each() instead:

$.each($values, function(k,v){
    console.log(k + ": " + v);
});

A generic iterator function, which can be used to seamlessly iterate over both objects and arrays. Arrays and array-like objects with a length property (such as a function's arguments object) are iterated by numeric index, from 0 to length-1. Other objects are iterated via their named properties.

You can also just send an Object (jQuery or not) to console:

console.log($values);
Sign up to request clarification or add additional context in comments.

Comments

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.