question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

getting values from the list

See original GitHub issue

Using the example, I find it ridiculous that this is what I have to do to get the list of items:

$('li').toArray().map(function(x){ return $(x).text()})

Issue Analytics

  • State:closed
  • Created 9 years ago
  • Comments:8 (3 by maintainers)

github_iconTop GitHub Comments

5reactions
ccorcoscommented, Dec 19, 2014

Here’s an even better example:

Here’s some html:

 <tr id="frank_menu">
    <th class="pomona_background"><a href="http://www.pomona.edu/administration/dining/menus/frank.aspx" target="_blank">Frank</a></th>
    <td>
      <span class="mobile_meal_header">Breakfast</span>
      <ul>


            <li>Bacon</li>

            <li> Vegan Sausage</li>

            <li> Hash Brown Potatoes</li>

            <li> Tofu &amp; Apple Sausage Scramble</li>

            <li> Hard Boiled Eggs</li>

            <li> Scrambled Eggs</li>

            <li> Scrambled Egg Whites</li>

            <li> Breakfast Special: Breakfast Burrito: Flour Tortilla</li>

            <li> Scrambled Eggs</li>

            <li> Sausage</li>

            <li> Potatoes</li>

            <li> Cheese</li>

            <li>Omelet Bar</li>

            <li>Breakfast Smoothies</li>

            <li>Made To Order: Eggs</li>

            <li> Buttermilk Pancakes &amp; Cinnamon French Toast</li>

            <li> Topping Bar Make Your Own Belgian Waffle</li>

            <li>Oatmeal with Assorted Toppings: Brown Sugar</li>

            <li> Raisins</li>

            <li> Cinnamon Sugar</li>

            <li> Banana Chips</li>

            <li> Golden Raisins</li>

            <li> Chocolate Chips</li>

            <li>Bagel</li>

            <li> Fruit &amp; Yogurt Bar</li>

            <li> Frank&#39;s Granola</li>

            <li> Breakfast Muesli</li>

            <li> Smoothies</li>


      </ul>
    </td>
    <td>
      <span class="mobile_meal_header">Lunch</span>
      <ul>


            <li>Kung Pao Chicken Wraps</li>

            <li> Broccoli Beef</li>

            <li> Mushu Seitan &amp; Mushrooms</li>

            <li> Steamed Rice</li>

            <li> Noodles</li>

            <li> Spring Rolls</li>

            <li> Tofu-Vegetable Stir Fry</li>

            <li>Cheese</li>

            <li> Sour Cream</li>

            <li> Apple</li>

            <li> Pecan &amp; White Chocolate Pizza</li>

            <li> Pepperoni</li>

            <li>Chicken &amp; Noodle</li>

            <li>Fresh Fruit</li>


      </ul>
    </td>
    <td>
      <span class="mobile_meal_header">Dinner</span>
      <ul>


            <li>Hamburgers</li>

            <li> Cheeseburgers</li>

            <li> Garden Burgers</li>

            <li> Bacon</li>

            <li> Chili</li>

            <li> Grilled Onions &amp; Mushrooms</li>

            <li> French Fries</li>

            <li> Onion Rings</li>

            <li> Lettuce</li>

            <li> Tomato</li>

            <li> Red Onions</li>

            <li> Pickles</li>

            <li>Cheese</li>

            <li> Vegetable</li>

            <li> Pepperoni</li>

            <li> Special</li>

            <li>Fish-N-Chips</li>

            <li>Spicy Chicken &amp; Noodle</li>

            <li>Fruit Salad</li>


      </ul>
    </td>
  </tr>

And here’s the solution:

$("tr#frank_menu>td").toArray().map(function(x) {
  return $(x).find('ul').children().toArray().map(function(x) {
    return $(x).text();
  });
});

Its not that this library isn’t useful – it really is! But it would be a lot cleaner…

1reaction
jugglinmikecommented, Dec 20, 2014

There are certainly more concise ways to achieve your goal. This is no different than how you would do this in the browser with jQuery.

-$("tr#frank_menu>td").toArray().map(function(x) {
-  return $(x).find('ul').children().toArray().map(function(x) {
-    return $(x).text();
-  });
-});
+$("#frank_menu li").map(function() {
+  return $(this).text();
+}).toArray();

Does that help?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Python - Lists - Tutorialspoint
The list is a most versatile datatype available in Python which can be written as a list of comma-separated values (items) between square...
Read more >
List get() method in Java with Examples - GeeksforGeeks
The get() method of List interface in Java is used to get the element present in this list at a given specific index....
Read more >
Extract values from a Python list - Stack Overflow
Use audit_items_list.items() if you are using Python 3 or audit_items_list.iteritems() if you are using Python 2. Share.
Read more >
Python Lists and List Manipulation | by Michael Galarnyk
Lists store an ordered collection of items which can be of different types. The… ... Slices are good for getting a subset of...
Read more >
Python Get List of Values from List of Keys - Finxter
To get the list of dictionary values from the list of keys, use the list comprehension statement [d[key] for key in keys] that...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found