-1

So, I'm trying to figure out how I can return the name of an object from a for loop. It seems that whenever you push an object into an array, the name seemingly vanishes, and whenever I run a for loop, I can never get the name to show. For example:

/* I know this is a variable, but I'm asking about this for something like a fetch, where the returned json is a list of objects. test is just an example name for those objects returned from the fetch. */
let test = {
    hi: 'hi'
};
let arr = [];
arr.push(test);
/* Here, I wish for the name of the objects, like if from a fetch we have an object test: {hi: 'hi'} from a fetch, and I put that into a for  loop, I want to be able to extract the name of that object and use it, instead of the only things being accessible are the object entries */
for (let count in arr) {
    console.log(arr[count])
};

Here, the name of the object will suddenly vanish, and there's no getter inside the object to return the name. In this example, my goal would be for 'test' to be returned. Without having a getter inside the object for the name How can I get some sort of list of objects, and put those objects in a for let loop that will actually return the name of the objects inside the loop--if there even is a way?

  • 1
    You can't get the name of variables. Well, I suppose if you count [this](https://stackoverflow.com/questions/4602141/variable-name-as-a-string-in-javascript) question, then sure, it's possible. But in your question, you can't use the answers. – kelsny Sep 20 '22 at 21:10
  • care to elaborate what do you mean by the `name` of the object? you mean you want to see your output like this? `test = { hi:'hi' }` instead of `{ hi:'hi' }` ? – Chris G Sep 20 '22 at 21:12
  • 1
    What "name" is "suddenly vanishing"? What you have are two references to an object. One is a variable, one is an array element. They both reference the same object. You are logging that object to the console from the array element. What are you expecting to happen and why? What are you trying to accomplish and why? *"In this example, my goal would be for 'test' to be returned."* - Then `console.log('test')`? – David Sep 20 '22 at 21:14
  • I'm afraid your edit hasn't clarified anything at all. Being "from a fetch" doesn't change what a variable is or what an object is. Can you provide a concrete example of what you're trying to do and why? – David Sep 20 '22 at 21:19
  • 1
    Also see: [What is the difference between ( for... in ) and ( for... of ) statements?](https://stackoverflow.com/questions/29285897/what-is-the-difference-between-for-in-and-for-of-statements) – Yogi Sep 20 '22 at 21:28

3 Answers3

3

If you want to store a name/value list, use an object

let obj = {"test": {hi: "hi"}}; 
Object.entries(obj).forEach(([key, value]) => 
  console.log(`${key} = ${JSON.stringify(value)}`)
);
James
  • 20,957
  • 5
  • 26
  • 41
0

Print out your count variable. It is what you are looking for.

It should better bet named key instead

Thomas
  • 2,431
  • 17
  • 22
0

The test is a variable name that only holds the referance of the object in the memory, but it's not the name of the object, if you need to have each object have its own name, I suggest you add name property in the object.

let test = {
    hi: 'hi',
    name: 'test',
};
let arr = [];
arr.push(test);
for (let count in arr) {
    console.log(arr[count].name);
};
Mina
  • 14,386
  • 3
  • 13
  • 26