Loop through the unnamed object

Here's the object that I am working with, which contains arrays: https://i.sstatic.net/WPAF4.png

To extract its key values for further use, I'm trying to iterate over it using for in loop like this:

for (let key in gameObj){
    console.log(key);
}

However, the loop doesn't seem to be entering at all to retrieve the arrays.
What could I possibly be missing?

Answer №1

Unfortunately, I don't have enough reputation to add this as a comment. For more information, please visit Is it possible to get the non-enumerable inherited property names of an object?

function findInheritedProperties(obj){
    let allProps = []
      , currentObj = obj
    do{
        let props = Object.getOwnPropertyNames(currentObj)
        props.forEach(function(prop){
            if (allProps.indexOf(prop) === -1)
                allProps.push(prop)
        })
    }while(currentObj = Object.getPrototypeOf(currentObj))
    return allProps
}

Answer №2

Everything is running smoothly. Feel free to take a look at the snippet.


    let playerInfo = {
        "name": ["Alice", "Bob", "Charlie", "David"],
        "score": [100, 200, 300, 400]
    }

    for(let key in playerInfo){
        console.log(key);
    }

Answer №3

To access the object in a for loop, simply use the key variable to reference each key value pair as if it were an array:

const gameObj = {
   '99lEEbmV7s': ['37966', '37966', '37965', '37966', '0'],
   'TggZdsbcje': ['37966', '37966', '37965', '37966', '0']
};

for (let key in gameObj){
    console.log(gameObj[key]);
}

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

When the document is fully loaded and processed, a script will be executed immediately following

I'm facing an issue with a modal plugin on my website. The plugin is triggered on $(document).ready, but I have another function (innerHTML) that inserts an <a> element 5-10 seconds after the page loads. This causes the modal not to work properl ...

Tips for resolving jQuery conflict problems

I am dealing with a jQuery issue where I have two scripts - one for the slider and the other for a dropdown menu. When I remove one script, the slider works but the dropdown doesn't, and vice versa. I have looked at tutorials online on how to resolve ...

How to replace text using jQuery without removing HTML tags

One of the functions in my code allows me to replace text based on an ID. Fortunately, this function is already set up and working smoothly. $('#qs_policy .questionTitle').text("This text has been updated"); However, there is another ...

Vue child component not displaying updates after property is cleared (utilizing Vue.js 3 without any bundler)

Currently, I am diving into learning vue.js version 3.0 and I'm in the process of experimenting with child components without using a build system for Vue. In my project, I pass an array to the child component and then clear it within that component. ...

What is the best way to extract the value from a textangular editor within an ng-repeat loop

I am using the TextAngular editor within an ng-repeat loop. Within the HTML <ul ng-repeat="lecture in section.lectures"> <div class="col-md-12 article-show" > <form ng-submit="lecture_content('article', $index + 1, l ...

Switch between two fields using just one animation

I've been experimenting with creating an information tag that slides out from behind a circular picture. To achieve this effect, I created a block and circle to serve as the information field placed behind the image. However, I'm facing a challe ...

Unable to change JSON object into a 2D array

I received a JSON object: var data=JSON.parse(server_response); Upon running console.log(data); I obtained the following result: [ [ [ "Dipu", "Mondal", "O Positive", "017xxxx", "AIS" ] ], [ [ ...

Various filters have been utilized on an array of objects

Within one of my Vue components, the code structure is as follows: <li class="comment" v-for="comment in comments"> ... </li> Accompanied by a computed method: computed: { comments() { // Here lies the logic for filtering comment ...

Component encounters issue with undefined props being passed

Encountering an issue where dummy data is not being passed to a component, resulting in undefined props. Even console.log() statements within the same page are not functioning properly. Can anyone identify what might be going wrong here? import AllPosts fr ...

Issues with jQuery code functionality within web forms

After creating a jQuery script to alter the CSS of a table row, I tested it on JSFiddle and it worked perfectly. However, when implemented into my web project, it doesn't seem to be functioning as intended. See the code below: HTML: <script src ...

Combining arrays to append to an array already in place

I have implemented the rss2json service to fetch an rss feed without pagination support. Instead of a page parameter, I can utilize the count parameter in my request. With this setup, I am successfully able to retrieve and display the feed using a service ...

Issue at hand: Unexpected error 500 encountered while sending AJAX request to PHP script through

UPDATE: Issue Resolved! I want to extend my gratitude to everyone who directed me to the error log files for assistance. The community here is truly incredible and I was able to get a resolution much quicker than anticipated. It's quite embarrassing, ...

Is there a way to display subfolders within a main folder using just one component in React (Next.js)?

English is not my strong suit, so I appreciate your understanding. First things first, please review my code below: const DriveFile = ({folderPk}) => { const [rootFolder, setRootFolder] = useState([]) const viewFolder = async () => { ...

What methods are available for me to apply a filter on an API?

I am working with the MovieDB API and want to implement a search bar for filtering. I could really use some assistance as I'm not sure how to get started. The requirement is to utilize JavaScript/jQuery for the code and only filter based on keywords. ...

Show the next option when a specific option is selected using HTML and jQuery

I have a form with two connected select options. Currently it looks like this: <form> <select onchange="this.form.submit()"> <option>1</option> <option>2</option> <option>3</optio ...

What could be the reason for my Angular website displaying a directory instead of the expected content when deployed on I

My current challenge involves publishing an Angular application to a Windows server through IIS. Upon opening the site, instead of displaying the actual content, it shows a directory. However, when I manually click on index.html, the site appears as intend ...

What could be causing the lack of activity or issues when users click on the target id and text after the .on click event?

After executing the code snippet provided in (1), I expected the code in (2) to update the friendName variable when a user clicks on the username. However, despite no console errors being returned, nothing happens when I interact with the username text on ...

Error occurred during the parsing of an AJAX response

Hello, I am currently exploring the world of JavaScript and jQuery. I recently encountered a situation where I initiated an AJAX call in my code and received an unexpected response. https://i.sstatic.net/AUHb7.png My current challenge revolves around imp ...

Express callback delaying with setTimeout

I'm working on a route that involves setting a data attribute called "active" to true initially, but then switching it to false after an hour. I'm curious if it's considered bad practice or feasible to use a setTimeout function within the ex ...

A Reactjs function that dynamically updates state elements using two input values

In my front-end application built with react-mui, I have a list of 8 words. Each word has its state updated based on user input and can be disabled. For example: <TextField required id="standard-basic" disabled={this.state.word1Disabled} label="Word1" ...