3

I get an object with partial results of match from database.

[Object { home1=4, away1=3, home2=4, away2=5, home3=6, away3=7, home4=6, away4=5, home5=3, away5=6}]

home1 it's a result of first part of home team, away1 -> away team, home2 it's a result of second part of home team... etc etc

data in my case is each row, which i get from database. In rows i have td with class: home1, home2, home3, away1, away2 and there are values of corresponding part of match. I want to check if value is equal to what I got from database. Something like this

if ($('.home1') === data[index].home1;
if($('.away2') === data[index].away2;

there should be some loop. I have no idea how to do this, I thought about an array

var array = [
 {
 home1: data[index].home1,
 away1: data[index].away1
 },
 {
 home2: data[index].home2,
 away2: data[index].away2
 },
 {
 home3: data[index].home3,
 away3: data[index].away3
 },
 {
 home4: data[index].home4,
 away4: data[index].away4
 },
 {
 home5: data[index].home5,
 away5: data[index].away5
 }
 ]

and then for loop:

for(var X=0; X<5;X++){
homeX == data[index].homeX 
}

How can I increment name of variable by eval function? or is there any other solution? I'm very confused.

asked Oct 16, 2013 at 12:42
2
  • $(.home1) is invalid selector Commented Oct 16, 2013 at 12:47
  • 1
    you should get properly formatted JSON from database. + you should post your code to get some help. Commented Oct 16, 2013 at 12:49

5 Answers 5

1

You can access object properties using operator []:

for(var i=0; i<array.length; i++)
{
 var item = array[i];
 var homePropertyName = 'home' + (i+1);
 //now you can access homeX property of item using item[homePropertyName]
 //e.g. item[homePropertyName] = data[index][homePropertyName]
}
answered Oct 16, 2013 at 13:00
Sign up to request clarification or add additional context in comments.

Comments

0

Maybe you should use a little different structure which might fit your needs better, like this:

array = [
 0: array [
 "home": "Text for home",
 "away": "Text for away"
 ],
 1: array [
 "home": "",
 "away": ""
 ]
 // More sub-arrays here
];

You can also initialize it with a for loop:

var array = new Array();
var i;
for (i = 0; i < 4; i++) {
 array[i] = [
 "home": "",
 "away": ""
 ];
}

Or like this:

array[0]["home"] = "Text for home";
array[0]["away"] = "Text for away";

You can use this structure for the data-array also, and then use a for-loop to go through them both (like if you wish to find an element):

var result = NULL;
for (i = 0; i < array.length; i++) {
 if ( (array[i]["home"] == data[index]["home"]) &&
 (array[i]["away"] == data[index]["away"])
 ) {
 // Found matching home and away
 result = array[i];
 break;
 }
}
if (result != NULL) {
 alert("Found match: " + result["home"] + " - " + result["away"]);
}
else {
 alert("No match");
}

PS: Code is not tested, let me know if something is wrong.

answered Oct 16, 2013 at 12:59

Comments

0

you can access global properties in browser via window object like this (fiddle):

value1 = "ONE";
alert( window['value'+1] );

But it is not good design. You should look into how to properly format JSON object.

answered Oct 16, 2013 at 13:03

Comments

0

I have something like this:

for(var i=0; i<2; i++)
{
 var item = ARR[i];
 for(var x=0;x<5;x++){
 var hPropertyName = 'home_p' + (x+1);
 var aPropertyName = 'away_p' + (x+1);
 item[hPropertyName] = ARR[i][hPropertyName]; 
 item[aPropertyName] = ARR[i][aPropertyName];
}

and it works when i create an array:

 var ARR = [
 {
 home_p1: 4,
 away_p1: 5,
 home_p2: 8,
 away_p2: 9,
 home_p3: 2,
 away_p3: 1,
 home_p4: 5,
 away_p4: 3,
 home_p5: 3,
 away_p5: 2
 },
 {
 home_p1: 6,
 away_p1: 1,
 home_p2: 1,
 away_p2: 2,
 home_p3: 3,
 away_p3: 4,
 home_p4: 5,
 away_p4: 6,
 home_p5: 3,
 away_p5: 2
 }
 ];

but I don't have to create an array, because i have to work on object which I get from database :

[Object { event_id=19328, home_result=3, away_result=2, home_p1=4, away_p1=3, home_p2=1, away_p2=2 ...... }]

I'm only interested in these parameters --> home_p , away_p I want to push it to my array to looks like ARR. I think i should convert an object which I get to an array

answered Oct 17, 2013 at 7:08

Comments

0

If you are using string name for your attributes then you could try using template literals?

var someObject = {}
for(let i=0 ; i<values.length ; i++){
 someObject[`home${i+1}`] = values[i];
} 

and if you need it to be ES5 you could just use string concatenation. Below is a working example:

values = [1,2,3,4,5];
let someObject = {};
for(let i=0 ; i<values.length ; i++){
 someObject[`value${i+1}`]=values[i];
}
console.log(someObject.value1);
console.log(someObject.value2);
console.log(someObject.value3);
console.log(someObject.value4);
console.log(someObject.value5);

answered Apr 8, 2018 at 13:53

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.