I must do this: to work over string that i will ask to some user:
text = "";
arr = ["*****"];
arr2 = [];
text = arr.toString();
for(i=0; i<text.length; i++) {
arr2.push(text[i]);
}
---> arr2 = [*,*,*,*,*] <--- thats what i want!.
But i must work with a lot of data like this, so i created and array to content that structured data:
var gems = [
{
name: 'Level-1',
row: {
r1: '*****',
r2: '-***-',
r3: '--*--'
},
canPushtoDb: true,
hideLevel: false,
status: 1
//canvas
},
{
name: 'Level-2',
row: {
r1: '**-**',
r2: '-*-*-',
r3: '-***-',
},
canPushtoDb: true,
hideLevel: false,
status: 1
//canvas
},
{
name: 'Level-3',
row: {
r1: '*-*-*',
r2: '**-**',
r3: '*-*-*'
},
canPushtoDb: true,
hideLevel: true,
status: 1
//canvas
}
];
I want to loop within gems to convert that string on some kind of array, just like I wrote on the beginning of this question.
I was looking on an Angular example, which wrap those objects with a controller.
But for now I'm far to get my code on some refactored version of his own. I just need some hard-coded function or method likegems.name[0].row.r1(this is not working to me!) to get out the object all that I'm working on.
2 Answers 2
You can iterator over gems as such:
for(var i = 0; i < gems.length; i++) {
...
}
You can then loop through the object as such (within your for loop above)
$.each(var obj in row) {
...Do something with each object found in row
}
To access hardcoded object, your issue was gems.name[0].row.r1
When it should just be gems[0].row.r1
8 Comments
for(i=0: i<=gems[2].row.length; i++ ){ arr = "r1: " + r1 + "r2: " + r2 + "r3: " + r3; } But it's not working.r1, r2, r3. You can do that via var r1 = gems[2].row.r1.. etc for all of them.for( arr in arr2){ text += '<br />' + arr2[arr] + '<br />'; } . Which present some problems. first, the output shows an undefined word as if it was a member on the object. And second the editor warm me to wrat the sentence inside the for with an if statement. I can't undertand both. I defined variables as: var text; var arr; var arr2 = gems[0].row;gems[0].row is an object, not an array. You can access it via gems[0].row.r1.As pointed out by @Patrick Evans, your code is correct except for the fact that arr2 should be arr2 = gems[0].row.r1. Have a look at this JSFiddle.
gems[0].row.r1notgems.name[0].row.r1gems[0].row.r1. To create array from string's characters:var arr = str.split('');array.fill.var arr2 = new Array(tet.length).fill('*')