Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

get sub object data to array

object data

I have the object from the console like the following

object { 0: div#ch2.group-item, 1: div#ch4.group-item, length: 2 }

I want to convert it to array ids [ch2,ch4]

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

var ids=$.each(children, function() { return $(this).attr('id'); });
console.log(ids);

>Solution :

So, you need to use jquery map instead of each.

For example.

const children = $('li');

const result = $.map(children, (item) => item.id)

console.log(result)
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<ul>
  <li id="one">
    first
  </li>
    <li id="two">
    second
  </li>
    <li id="three">
    third
  </li>
    <li id="four">
    fourth
  </li>
    <li id="five">
    fivth
  </li>
</ul>
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading