Stock a JS array in NKSettings ?

Ask questions and share your skills here

Stock a JS array in NKSettings ?

Postby raf69 » Wed Jul 28, 2010 11:32 am

Hi everybody,

I want to create a global array that i can manipulate in all JS scripts pages... This is the array structure :

myArray = new Array();
myArray[i] = new Array(2);
myArray[i]["id"] = id;
myArray[i]["title"] = title;
i++;

So, i thought about the NKSettings class. Did I think well ? ^^

I tried this :
...
settings.setValueForKey(myArray, "array");
var my_array = settings.getValueForKey("array");
NKLog(my_array);

I got a JSON object (?) :

"9":{"id":944,
"title":"l’iPad explose les records de vente",
etc...

But now, i don't know how to use it... I tested :

NKLog(my_array[0]); >> "{"
NKLog(my_array[0].id); >> undefined
NKLog(my_array["0"].id); >> undefined
NKLog(my_array["0"]["id"]); >> undefined

How can I get my array ?

I'm little bit frustrated... I need help :(
raf69
 
Posts: 15
Joined: Tue Jun 29, 2010 10:23 am

Re: Stock a JS array in NKSettings ?

Postby ueilat » Wed Jul 28, 2010 7:32 pm

But the value of var i when you declare the array is 0 or 9?
According to the log seems 9.
Try reading [9] or using a loop for each
User avatar
ueilat
NimbleKit Expert
 
Posts: 158
Joined: Fri Feb 12, 2010 2:31 am
Location: Torino, Italy

Re: Stock a JS array in NKSettings ?

Postby raf69 » Thu Jul 29, 2010 4:55 pm

The value 9 correspond to the 9 th value in the array. So, it could be 0 or 4... For instance, a sample of NKLog(my_array); :

"0":{"id":394,
"title":"l’iPhone 4",

So, How can I get those values ?

Sunny ? Can you help me ? :p
raf69
 
Posts: 15
Joined: Tue Jun 29, 2010 10:23 am

Re: Stock a JS array in NKSettings ?

Postby ryubr » Thu Jul 29, 2010 5:17 pm

Hello,

Try using the eval function from javascript, it will interpret the json code real time and then the object will be available, json is much better than array to store data and you can "read" it as an array too.

Try this:
settings.setValueForKey(myArray, "array");
var my_array = settings.getValueForKey("array");
if( my_array != "(null") )
{
my_array = eval(my_array);
}

Now you should have my_array["0"] and my_array["0"].id available :-)
ryubr
 
Posts: 52
Joined: Fri Jun 19, 2009 5:36 am

Re: Stock a JS array in NKSettings ?

Postby sunny » Thu Jul 29, 2010 5:20 pm

Code: Select all
<script>

var settings = new NKSettings();

var id = 0;
myArray = new Array();

for (i = 0; i<10; i++, id++)
{
   myArray[i] = new Object;
   myArray[i]['id'] = id;
   myArray[i]['title'] = "Item #"+id;
}

saveArray();
printArray();

function saveArray()
{
   settings.setValueForKey(myArray, "array");
}

function printArray()
{
   myArrayNew = settings.getValueForKey("array");
   
   for (i = 0; i<myArrayNew.length; i++)
   {
      NKLog(myArrayNew[i].title);
   }
}

</script>


The output is this:

2010-07-29 18:19:58.355 settingsTest[13321:207] Item #0
2010-07-29 18:19:58.358 settingsTest[13321:207] Item #1
2010-07-29 18:19:58.359 settingsTest[13321:207] Item #2
2010-07-29 18:19:58.361 settingsTest[13321:207] Item #3
2010-07-29 18:19:58.363 settingsTest[13321:207] Item #4
2010-07-29 18:19:58.364 settingsTest[13321:207] Item #5
2010-07-29 18:19:58.366 settingsTest[13321:207] Item #6
2010-07-29 18:19:58.367 settingsTest[13321:207] Item #7
2010-07-29 18:19:58.369 settingsTest[13321:207] Item #8
2010-07-29 18:19:58.370 settingsTest[13321:207] Item #9
User avatar
sunny
Staff
 
Posts: 1786
Joined: Sat May 30, 2009 5:18 am
Location: kiev, Ukraine

Re: Stock a JS array in NKSettings ?

Postby ueilat » Thu Jul 29, 2010 5:27 pm

You didn't test the only one correct that is
NKLog(my_array[0]["id"]);

in any case test this code and you will see that it works
Code: Select all
myArray = new Array();
myArray[1] = new Array();
myArray[1]["id"] = "x";
myArray[1]["title"] = "y";
myArray[2] = new Array();
myArray[2]["id"] = "w";
myArray[2]["title"] = "z";
   
var settings = new NKSettings;
NKAlert(1,myArray[2]["title"]);
NKAlert(2,myArray[1]["id"]);
   
settings.setValueForKey(myArray, "array");
var my_array = settings.getValueForKey("array");
NKAlert(3,my_array[2]["title"]);
NKAlert(4,my_array[1]["id"]);
User avatar
ueilat
NimbleKit Expert
 
Posts: 158
Joined: Fri Feb 12, 2010 2:31 am
Location: Torino, Italy

Re: Stock a JS array in NKSettings ?

Postby raf69 » Thu Jul 29, 2010 8:40 pm

Thank you very much for all those answers ! :)

I have 3 solutions so I will try it tomorrow. Sunny, what is the most efficient ?
- create an object (like in your example)
- use the eval() function
- my_array[0]["id"] (that seems to be the easiest solution)


Thx again !!!
raf69
 
Posts: 15
Joined: Tue Jun 29, 2010 10:23 am

Re: Stock a JS array in NKSettings ?

Postby raf69 » Fri Jul 30, 2010 10:44 am

Huuuum... I really don't understand... So, I try your code Sunny in a new project, it workes well. But, in my project i've got this result :

var NEWS = new Array();
var settings = new NKSettings();

for (var i = 0; i <10; i++) {

NEWS[i] = new Object();
NEWS[i]['id'] = i;
NEWS[i]['title'] = 'title'+i;
}

NKLog(NEWS);
settings.setValueForKey(NEWS, "array");

var myArrayNew = settings.getValueForKey("array");
NKLog(myArrayNew);

output :

>> [object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
>> {"0":{"id":0,
"title":"item0"},
"1":{"id":1,
"title":"item1"},
"2":{"id":2,
"title":"item2"},
"3":{"id":3,
"title":"item3"},
"4":{"id":4,
"title":"item4"},
"5":{"id":5,
"title":"item5"},
"6":{"id":6,
"title":"item6"},
"7":{"id":7,
"title":"item7"},
"8":{"id":8,
"title":"item8"},
"9":{"id":9,
"title":"item9"},
"each":,
"eachSlice":,
"all":,
"every":, etc...

What's wrong with my code ? : / The output looks like a JSON structure... and not an object :(

Else, I tried the eval() function :

var myArrayNew = settings.getValueForKey("array");
myArrayNew = eval(myArrayNew);
NKLog(myArrayNew[0].id);
NKLog("test");

I think that there is an error with the eval function because i don't have any output after using it.
raf69
 
Posts: 15
Joined: Tue Jun 29, 2010 10:23 am

Re: Stock a JS array in NKSettings ?

Postby Big-O » Fri Jul 30, 2010 3:23 pm

What is that the output of? I don't think you're showing us all the code.
I am a small and fragile flower.

-- Big-O
User avatar
Big-O
NimbleKit Expert
 
Posts: 310
Joined: Thu Sep 24, 2009 4:02 am

Re: Stock a JS array in NKSettings ?

Postby ryubr » Fri Jul 30, 2010 9:07 pm

Hello,
The correct syntax for the eval function is this:

var myArrayNew = settings.getValueForKey("array");
eval("myArrayNew =" + myArrayNew);
NKLog(myArrayNew[0].id);

That way the eval will be parsed just fine and the json converted to a javascript object
ryubr
 
Posts: 52
Joined: Fri Jun 19, 2009 5:36 am

Next

Return to How To...

Who is online

Users browsing this forum: Google [Bot] and 1 guest