Discovering the presence of a key and value within an object and implementing CSS styling specifically for them in JavaScript

Here is an example of the object I have:

let myObject = {
0: {id:'1001',name:'john'},
1: {id:'1002',name:'johnson'},
2: {id:'1001',name:'jack'},
3: {id:'1021',name:'mark'},
}

I am looking to search for key-value pairs in the object where id is '1001' and then apply specific CSS properties, such as background color.

In my HTML table, there will be two columns - ID and Name. Based on the value found, I want to apply CSS styling to the entire row in the table.

Answer №1

Thank you for the information.

var data = {
0: {id:'1001',name:'john'},
1: {id:'1002',name:'johnson'},
2: {id:'1001',name:'jack'},
3: {id:'1021',name:'mark'}
}
Object.keys(data).forEach(function(item) {
  if(data[item]["id"] == 1001){
      console.log(data[item]["name"]);
  }
});

Edit: I now understand your question better with your comment. Hopefully, this example will assist you in achieving your goals.

P.s. Let's keep the negativity to a minimum.

let table = document.getElementById("exTable").children[0];
for(var i = 1; i<table.children.length; i++){
    if(table.children[i].children[1].innerHTML == "Rafi"){
        table.children[i].style.backgroundColor = "yellow";
    }
}
<!DOCTYPE html>
<html>
<head>

</head>

<body>
<table style="width:100%" id="exTable" border="1">
<tbody>
  <tr>
    <th>ID</th>
    <th>Name</th>
  </tr>
  <tr>
    <td>1</td>
    <td>Rafi</td>
  </tr>
  <tr>
    <td>2</td>
    <td>Quinn</td>
  </tr>
<tbody>
</table>
</body>
</html>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

An element failing to submit using AJAX requests

I have a login form with an <a> element that I want to use for making a post request. However, when I'm examining the backend Django code during debugging, it seems to interpret the request method as GET instead of POST. HTML <form id= ...

How can I implement a division animation with Javascript upon clicking a hyperlink?

I need help with animating a div when clicking on a link. Specifically, when clicking on the "About" link, the height of the div should change. Currently, the height is set to '0em' in the CSS file. I attempted to change the height using "documen ...

How do I use React and Material-UI to efficiently display multiple tables from a complex JSON object containing multiple arrays of data?

Trying to come up with an innovative approach to generate a unique dynamic table component that can create individual tables based on the number of arrays in a dictionary object (essentially iterating through each array and generating a table). For my sce ...

Steer clear of Cross-Site Request Forgery through

As someone who is still learning about web security, I am curious about the best practices for using tokens on JavaScript requests to prevent CSRF attacks. Is it possible for someone to provide a code example? I already know how to implement this properly ...

Prevent submission of form by disabling button until modifications are made to the original data in Vue.js

For experimental purposes, I have created a simple form and am trying to implement a feature where the button remains disabled until the original form data is changed. Additionally, I want to ensure that the button stays disabled even if the changed data i ...

Implementing Vue method to apply filter criteria on an array for efficient data filtering

Having trouble filtering listItems by filterCriteria before rendering. The issue arises when attempting to pass the filterCriteria into the filter function using Array.prototype.filter. Is there a more efficient method of passing it without having to ...

Obtain the HTTP status code in Node.js

Currently, I am looking to retrieve the HTTP status code of a URL that is passed to a function. Up to this point, I have been using the request module for this purpose. The challenge I've encountered is that the request module fetches all the content ...

What is the best method for eliminating buttons and text in a row using CSS?

I faced an issue when trying to create a row, so I improvised with the following setup. However, I actually need a proper row layout for both the Google button and telephone number button. Here are my CSS snippets: .google-button { color: white; borde ...

No tests were located for execution. Despite our efforts, a cypress.json file was not found in the search

Having recently set up Cypress in my project, I encountered an issue while attempting to run it using npx cypress run. The error message displayed was: Could not find any tests to run. We searched for a cypress.json file in this directory but did not ...

Adding unique characters into the database

I'm encountering difficulties when attempting to insert special characters into my database. For instance, if the input string is "\kdjfg*&(^^&%%//""dfkjs/Z?!", only "\kdjfg*" is being successfully inserted into the table. I am unsur ...

Patience is key when using Selenium with Node.js - make sure to wait for the

Is there a way to ensure my code waits until the page is fully loaded in Node.js while using selenium-webdriver version 4.0.0? const driver = new Builder().forBrowser("firefox").build(); await driver.get("http://www.tsetmc.com/Loader.a ...

Calculating Future Dates in JavaScript by Adding Days

I am looking to add 20 days to the current date and present the output in the mm/dd/yyyy format. Presently, with today being 05/15/2014, the result produced is 05/35/2014, which is clearly an invalid date. var currentYear = new Date(); alert((currentYea ...

Typescript void cannot be assigned to the parameter type ProcedureListener

When attempting to call a function within my class, I encounter a warning/error in this particular section of the code: rpc.register('cBrowser-SetUrl', (url: string, enableCursor: boolean) => { this.setBrowserUrl(url, enableCursor); }); T ...

How can we create a JavaScript function that compares the value in an object with the value selected in an HTML dropdown menu?

Looking to develop an innovative NFL playoffs bracket with dynamic functionality using javascript. Unlike a typical tournament bracket, the match-ups in rounds 2 and 3 will be determined by the teams' ranks. To achieve this, I have created a comprehen ...

Display the map using the fancybox feature

I have added fancybox to my view. When I try to open it, I want to display a map on it. Below is the div for fancybox: <div id="markers_map" style="display:none"> <div id="map_screen"> <div class="clear"></div> </div&g ...

Creating a basic page caching system:

Searching for a way to restrict the number of times a page is generated within a specific time frame has proven to be quite challenging. In the past, I have tackled similar issues by setting up pages to be automatically generated and saved using a cron jo ...

What is the best way to access the names of iframes and frames within window.length?

I am currently working on a project with numerous frames. While I am aware that using window.length will provide the number of "child browsing contexts", like frames and iframes, I am unsure how to access a list of these frames and view their names. Is t ...

Mastering the art of managing button toggles with a wrapper

Currently, I am in the process of developing a Toggle Button that has specific requirements. According to the specifications, a group of Toggle Buttons should exhibit behavior that allows for both single click (see example) and multiple clicks (see example ...

What is the best way to include an ajax request within a function and execute it only when needed?

I am working on an ajax request that fetches data from a specific URL. I have the following code snippet: $.ajax({ type: 'POST', url: '/get-result.php', dataType: 'json', data: 'pid=' + $(this).attr( ...

Need assistance with resolving this issue: Uncaught (in promise) TypeError: Unable to access properties of undefined (reading 'length')

I am currently working on a project involving the loading of gltf 3D models on a website. I am looking to dynamically load multiple models using a loop. const loader = new GLTFLoader() //.setPath( 'models/gltf/DamagedHelmet/glTF/' ); .setPa ...