1

I have js object that looks like this:

{
  "id": 9,
  "user_name": "John Kim",
  "age": 25,
  "is_elig": true
}

I have table where data should be populated, looks like this:

<table>
   <tr>
      <th>ID</th>
      <th>Name</th>
      <th>Age</th>
      <th>Eligible</th>
   </tr>
   <tr>
      <td id="id"></td>
      <td id="user_name"></td>
      <td id="age"></td>
      <td id="is_elig"></td>
   </tr>
</table>

I use JQuery in my project and I was wondering if there is a way to loop over js object and check if the key exist in table td cell id. If does exist then populate the value in the cell.

2
  • Look into Object.entries() or just for... in Commented Dec 20, 2018 at 20:19
  • @benvc Can you provide an example for Object.entires()? Commented Dec 20, 2018 at 20:21

3 Answers 3

5

You could iterate the object:

var obj = {
  "id": 9,
  "user_name": "John Kim",
  "age": 25,
  "is_elig": true
}

$.each(obj, function (key, value) { 
    $("#" + key).text(value);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table>
   <tr>
      <th>ID</th>
      <th>Name</th>
      <th>Age</th>
      <th>Eligible</th>
   </tr>
   <tr>
      <td id="id"></td>
      <td id="user_name"></td>
      <td id="age"></td>
      <td id="is_elig"></td>
   </tr>
</table>

Without jQuery, it would not be a lot more difficult:

var obj = {
  "id": 9,
  "user_name": "John Kim",
  "age": 25,
  "is_elig": true
}

Object.entries(obj).forEach(([key, value]) => 
    document.getElementById(key).textContent = value
);
<table>
   <tr>
      <th>ID</th>
      <th>Name</th>
      <th>Age</th>
      <th>Eligible</th>
   </tr>
   <tr>
      <td id="id"></td>
      <td id="user_name"></td>
      <td id="age"></td>
      <td id="is_elig"></td>
   </tr>
</table>

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

2 Comments

Is there any major difference in efficiency? Which method is recommended?
Depends on your goal: if you need support for old browsers, then take the jQuery form. If you don't need that, and don't need jQuery for other things, then go for the second one. Access to DOM elements is anyway costly, so the way you loop (with jQuery or not) is not significant for the overall efficiency.
2

Iterate through your object:

let obj = {
  "id": 9,
  "user_name": "John Kim",
  "age": 25,
  "is_elig": true
}

Object.keys(obj).forEach(i => $('td#'+ i).text(obj[i]) ); 
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table>
   <tr>
      <th>ID</th>
      <th>Name</th>
      <th>Age</th>
      <th>Eligible</th>
   </tr>
   <tr>
      <td id="id"></td>
      <td id="user_name"></td>
      <td id="age"></td>
      <td id="is_elig"></td>
   </tr>
</table>

4 Comments

Can you please explain how that works and the matching of the key and cell ID?
@espresso_coffee, in my previous answer i was looping over table cells, sorry. I edited to loop over your object.
$('td#'+ i) <= that is less efficient than if you just looked up by the id. And just looking up by id should return the same result.
Is your original asnwer @LLeon better fit then? Iterating over table td cells and setting text?
1

I think when you say "loop over js object" you mean "loop over js array of objects". In that case you might do something like this:

<table>
   <tr id="tr_users">
      <th>ID</th>
      <th>Name</th>
      <th>Age</th>
      <th>Eligible</th>
   </tr>
   <div id="tr_users"></div>
</table>

In your js file:

var users = [];
var data = [ALL USERS IN ARRAY];

function setTable(data ) {
    data .forEach(u => {
        if(userIsNewInTable(u)) {
            users.push(u);          
        }
    });
    users.forEach(u => {
        appendInTable(u);
    });
}

function userIsNewInTable(user) {
    return !users.includes(user);
}

function appendInTable(value) {
    let htmlToInsert = "";
    htmlToInsert = `<tr>
          <td>${id}</td>
          <td>${user_name}</td>
          <td>${age}</td>
          <td>${is_elig}</td>
       </tr>`;
    $('#tr_users').append(htmlToInsert);
}

In this approach, we are declaring an empty array that will be fullfilled with the users. We have a function "setTable" that will receive the array of all the data (data). It will iterate over them and check if they already are in the users array to avoid duplicated. Once we finished the iteration, we can insert the html with all the "td".

Please let me know if that's what you were looking for. I wish it helped you.

Goodbye!

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.