0

I've this structure here build from a previous question:

jQuery(document).ready(function($) {
  let entries = [jQuery("#row .entry")].map(data => ({
    issue: data.children[0].children[0].value,
    value: data.children[1].children[0].value
  }));
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="row">
  <div id="entry-wrapper">
    <div class="entry">
      <div class="text-wrapper">
        <input type="text" class="text" value="ABC">
      </div>
      <div class="value-wrapper">
        <input type="text" class="value" value="123">
      </div>
    </div>
    <div class="entry">
      <div class="text-wrapper">
        <input type="text" class="text" value="DEF">
      </div>
      <div class="value-wrapper">
        <input type="text" class="value" value="456">
      </div>
    </div>
  </div>
</div>

Because of CSS I needed to wrap everything into some divs and wrappers and now I'm unable to get every text and the associated value in my array of objects. What I'm doing wrong?

1
  • In your example the script gets all the present elements but causes a Warning. I guess you have to add a check if the element has children of its own. Commented Dec 16, 2019 at 21:56

3 Answers 3

1

You shouldn't wrap the jQuery object inside an array. That's setting data to the jQuery collection, not the elements.

jQuery has its own map() method, which you can use to loop over the elements in a collection (note that it takes arguments in the opposite order of Array.prototype.map()). It returns a jQuery object, but you can call .get() to convert that to an ordinary array.

jQuery(document).ready(function($) {
  let entries = $("#row .entry").map((i, data) => ({
    issue: data.children[0].children[0].value,
    value: data.children[1].children[0].value
  })).get();
  console.log(entries);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="row">
  <div id="entry-wrapper">
    <div class="entry">
      <div class="text-wrapper">
        <input type="text" class="text" value="ABC">
      </div>
      <div class="value-wrapper">
        <input type="text" class="value" value="123">
      </div>
    </div>
    <div class="entry">
      <div class="text-wrapper">
        <input type="text" class="text" value="DEF">
      </div>
      <div class="value-wrapper">
        <input type="text" class="value" value="456">
      </div>
    </div>
  </div>
</div>

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

1 Comment

Thanks barmer for this working solution. Now I've got it. I was always wondering why it sometimes works with and without .get()
1

To turn a jQuery collection into a standard Array, you should not do:

[jQuery("#row .entry")]

...as this creates an array with just one value, and that value is the jQuery collection.

Instead use the method that jQuery provides for this purpose, i.e. get():

Without a parameter, .get() returns an array of all of the elements

So:

jQuery("#row .entry").get()

Comments

1

You can use the Array.from method to convert the jQuery collection to a JavaScript array.

jQuery(document).ready(function($) {
  let entries = Array.from(jQuery("#row .entry")).map(data =>({
    issue: data.children[0].children[0].value,
    value: data.children[1].children[0].value
  })).forEach(el => {
    console.log(el);
  });
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="row">
  <div id="entry-wrapper">
    <div class="entry">
      <div class="text-wrapper">
        <input type="text" class="text" value="ABC">
      </div>
      <div class="value-wrapper">
        <input type="text" class="value" value="123">
      </div>
    </div>
    <div class="entry">
      <div class="text-wrapper">
        <input type="text" class="text" value="DEF">
      </div>
      <div class="value-wrapper">
        <input type="text" class="value" value="456">
      </div>
    </div>
  </div>
</div>

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.