16

I don't know how I would .map into an array. I want to get all values of children but 2 and then put it into an array format.

Here's the code I'm attempting it with:

$("#schoolSupplies").submit(function() {
    var test = $(":input").not("#numOfSupplies, #submitBtn").map(function() {
        return $(this).val();
    })
    .get()
  .join( "\", \"" );

    console.log(test);
});

And this is the output: Billy", "John

I have been working on this for about an hour and I have no idea how.

4
  • 3
    Could you show example input and output, the desired output and what you are actually getting? Commented Sep 20, 2016 at 4:15
  • also provide html with it Commented Sep 20, 2016 at 4:17
  • Here's an output of what I get if I make more than just output 3", "1", "5", " I want something like that but I don't want the first and last element to not have " and I want everything to be surrounded by [ ]. Why would you need HTML for it? Commented Sep 20, 2016 at 4:20
  • If you want the string representation of an array, I suspect what you're looking for is JSON.stringify(). Commented Nov 22, 2024 at 17:07

3 Answers 3

29

The jQuery .get() method returns an Array 1.

screenshot of annotated jQuery documentation for get method with outline around return value

If the call to the Array .join() method was removed then test would simply be an array; otherwise test would be a string (since that is what the .join() method returns).

$("#schoolSupplies").submit(function() {
  var arrayOfValues = $(":input").not("#numOfSupplies, #submitBtn")
    .map(function() {
      return $(this).val();
    })
    .get()
  //.join( "\", \"" ) // this String method returns a string
  ;

  console.log('arrayOfValues is array: ', Array.isArray(arrayOfValues) ? 'yes' : 'no');
  console.log(' contents of arrayOfValues: ', arrayOfValues);
  return false; //for demonstration purposes, don't submit form normally
});
$($("#submitBtn").click());
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form id="schoolSupplies">
  Supply Name:  <input id="name" type="text" value="Tables" /><br />
  Student Name: <input id="studentName" type="text" value="Bobby" /><br /> 
  # of Supplies: <input id="numOfSupplies" type="number" value="3" /><br />
  <input type="submit" id="submitBtn" />
</form>

Sign up to request clarification or add additional context in comments.

2 Comments

I found toArray() also works... which one is 'better'? (or is there no such thing as "better" in javascript?)
It appears .toArray() was added in version 1.4 while .get() was added in version 1.0, which is likely why the documentation for .map() suggests using it. “Better” is subjective - one could argue semantically toArray is better but get returns the same and is shorter to write. See also this related GH issue.
0

It looks like what you're trying to create is a JSON string representing the array, so use JSON.stringify().

$("#schoolSupplies").submit(function() {
  var test = $(":input").not("#numOfSupplies, #submitBtn").map(function() {
      return $(this).val();
    })
    .get()

  console.log(JSON.stringify(test));
});

Comments

-1

Try the following

$("#schoolSupplies").submit(function() {
    var test = $(":input").not("#numOfSupplies, #submitBtn").map(function() {
        return $(this).val();
    })
    .get()
  .join( "\", \"" );
    test = '["'+test+'"]';//note this is a string not a array
    console.log(test);
});

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.