-1

I want to pass 10 different objects into a function (logObjekt) so that I can console.log the property name which is setup inside all objects.

Instead of having to write 10 lines of code to make this possible I am using a for loop to increment each name that's going to be passed to the function, I made this possible because all objects starts with "objekt".

When I debug this everything looks good and I get the result "object1, object2 ... and so on" when JS coerce the int to string and combine the two. So at first glance I thought iI got a good solution but when I implemented the solution I got 10x undefined instead of 10 values from the objects.

var objekt1  = { "name": "value1" };
var objekt2  = { "name": "value2" };
var objekt3  = { "name": "value3" };
var objekt4  = { "name": "value4" };
var objekt5  = { "name": "value5" };
var objekt6  = { "name": "value6" };
var objekt7  = { "name": "value7" };
var objekt8  = { "name": "value8" };
var objekt9  = { "name": "value9" };
var objekt10 = { "name": "value10" };

var i;
for (i = 0; i < 10; i++) {
  logObjekt("objekt" + i);
};

function logObjekt(objekt) {
  console.log(objekt.name)
};

I expect to log properties in the objects thats called "name".

Right now to actual result is 10x undefined.

Mr. Polywhirl
  • 42,981
  • 12
  • 84
  • 132
Gobban
  • 7
  • 3
  • 4
    Because you are passing a **string** value to `logObject`, and strings don't have a `.name` property. – Bergi Jan 28 '19 at 20:36
  • 1
    What you really want is to use an array of objects, which you can easily access by index. Don't use enumerated variable names. – Bergi Jan 28 '19 at 20:36
  • It needs to be an object data-type on the left side of . like this: object.property is that the problem, that i currently is passing string.property? – Gobban Jan 28 '19 at 20:40
  • See https://stackoverflow.com/questions/5187530/variable-variables-in-javascript for better approaches. – Felix Kling Jan 28 '19 at 20:49

2 Answers2

2

To access global variables with a variable name, you could (but in your case shouldn't, see below first snippet) use the square bracket syntax.

var objekt1 = {
  "name": "value1"
};

var objekt2 = {
  "name": "value2"
};

var objekt3 = {
  "name": "value3"
};

var objekt4 = {
  "name": "value4"
};

var objekt5 = {
  "name": "value5"
};

var objekt6 = {
  "name": "value6"
};

var objekt7 = {
  "name": "value7"
};

var objekt8 = {
  "name": "value8"
};

var objekt9 = {
  "name": "value9"
};

var objekt10 = {
  "name": "value10"
};

var i;
for (i = 1; i < 10; i++) {
  logObjekt(window["objekt" + i]);
};

function logObjekt(objekt) {
  console.log(objekt.name)
};

Please note that I showed this only to show how to do it if need be.

By no means should you do it that way. As others have mentioned already, instead put your objects into an array:

var objekt = [{
  "name": "value1"
}, {
  "name": "value2"
}, {
  "name": "value3"
}, {
  "name": "value4"
}, {
  "name": "value5"
}, {
  "name": "value6"
}, {
  "name": "value7"
}, {
  "name": "value8"
}, {
  "name": "value9"
}, {
  "name": "value10"
}]

for (var i = 0; i < objekt.length; i++) {
  logObjekt(objekt[i]);
};

function logObjekt(objekt) {
  console.log(objekt.name)
};
connexo
  • 53,704
  • 14
  • 91
  • 128
1

You need to create an array of objects and push to the array in order to loop through the objects and print out the name property.

var objectlist = [];
objectlist.push({ "name": "value1"});
objectlist.push({ "name": "value2"});

for(var i = 0; i < objectlist.length; i++)
{
   logObjekt(objectlist[i]);
}
BugCatcherJoe
  • 487
  • 3
  • 17
  • Thank you everyone for your time and answers. Helped me alot to futher my understanding of JS. If i could i whould set all comments as answeers becouse you were all helpful. – Gobban Jan 28 '19 at 20:53