You are not logged in.

1

Thursday, June 29th 2023, 11:18am

Access krpano-array from javascript??? SOLVED

Hi,

as Klaus said the krpano-arrays internally normal JS-arrays. But no matter what I try, I can´t access the krpano-array with JS.

Here is what I´m trying to achieve:
in krpano:

Source code

1
2
set(test,'1-2-3');
txtsplit(get(test),'-',my_array);

Now we have a krpano-array with [1,2,3].

But how can I access this array within JS?
var new_array = krpano.get("my_array"); produces errors all over the place if I try to access this array *mellow*

new_array=[object Object]
Uncaught TypeError: find.includes is not a function
applyFilter http://localhost/Tour_Database/tour.html:100
applyFilter http://localhost/Tour_Database/tour.html:100
applyFilter http://localhost/Tour_Database/tour.html:99
onreadystatechange http://localhost/Tour_Database/tour.html:105
readData http://localhost/Tour_Database/tour.html:88
anonymous http://localhost/Tour_Database/tour.js line 6 > Function line 3 > Function:3
js http://localhost/Tour_Database/tour.js line 6 > Function:3
executeActions http://localhost/Tour_Database/tour.js line 6 > Function:3
callaction http://localhost/Tour_Database/tour.js line 6 > Function:3
if http://localhost/Tour_Database/tour.js line 6 > Function:3

This post has been edited 1 times, last edit by "Nupsi" (Jul 25th 2023, 6:46pm)


2

Thursday, June 29th 2023, 1:40pm

Now we have a krpano-array with [1,2,3]

1. this is wrong... [1,2,3] would be a native javascript array, a krpano array is described here :
https://krpano.com/docu/plugininterface/#array

var new_array = krpano.get("my_array");

2. this code is not necessary but the syntax is ok and should not produce errors. $
it must be the context, you're probably mixing js code with krpano action script code (?)
in action script you'd write copy(new_array, my_array);

a global variable (like this array) is stored globally in the krpano object... so it is simple krpano.my_array
console.log(krpano.my_array.getItem(0)); would print the first element to the console
krpano.set(my_var,5); is the same as krpano.myvar = 5; (this is javascript, not action script)

This post has been edited 9 times, last edit by "indexofrefraction" (Jun 29th 2023, 10:32pm)


3

Saturday, July 1st 2023, 10:13am

Hi and thanks for the explanation *smile*

Unfortunately console.log(krpano.my_array.getItem(0)); just ends up in an error "krpano.my_array not defined" *unsure*

I really don´t get the difference between the krpano-array and a js-array.
var my_array = krpano.get("my_array");
console.log('MY_ARRAY='+my_array);

shows: MY_ARRAY=[Object object], which seems to me like an array?!

But no matter how I try to access this array:
console.log(my_array[1]);
console.log(my_array.at(1));

the console only reports "undefined".

But if I access the krpano-array directly:
krpano.get("my_array[1].value"); the console returns the correct value at index 1 "17".

There is definitely some misunderstanding on my side *wacko*




The main reason I want to access the krpano-array directly is that I can use it to filter database-entrys:

Source code

1
2
3
4
5
6
7
const applyFilter = (data, filter) => data.filter(obj =>
	Object.entries(filter).every(([prop, find]) => find.includes(obj[prop]))
	);

	var filter = {[id]:my_array};
						
	result = (applyFilter(data, filter));


Right now I use a workaround with txtsplit, where I loop though the krpano-array and txtadd the values to a var. It´s working but it´s waaay slower and much less elegant *mellow*

Thanks again, indexofrefraction! I really appreciate your time and help *thumbup*

4

Sunday, July 2nd 2023, 1:22am

how to work with krpano arrays is explained in the link above
this is the traditional way, i dont know why they were introduced, maybe bc of flash.. anyway, atm txtsplit still creates a krpano array, which is is an object with its own data structure and functions. a native js array wouldnt show as [Object object] in the console! but... if you work in js , you don't need to use txstsplit... why not use the js split function?

btw you don't seem to use the full krpano object... try this:
krpano = krpano.get("global");
console.log(krpano);

This post has been edited 2 times, last edit by "indexofrefraction" (Jul 2nd 2023, 1:37am)


5

Sunday, July 2nd 2023, 9:31am

Okay...I think I got where my mistake was. From the array-docu:
array.getItem(name or index)
Get an existing array item.
Returns the array item object or null when the items doesn't exist.


So krpano.my_array.getItem(1) does NOT return the value at index 1 but reads the object from the array which contains the value?! Because:
var my_array = krpano.my_array.getArray();
console.log('Value_1='+my_array[1].value);

returns the correct value at index 1.

This array/object/index/value-thingy is really confusing *wacko*

So I guess with krpano.my_array.getArray() I can transfer the whole krpano-array from krpano to JS.
And with console.log('MY_ARRAY_VALUE='+krpano.my_array.getItem(1).value); I finally get the value at index 1 *thumbsup*

I really can´t thank you enough for your help, but anyway...THANKS *thumbup*

NUPSI

P.S.: Sorry, I meant the JS split-function which I used like this: const filter_value = krpano_favs.split(',');. And within krpano I used txtadd to create a var which contains the content from the krpano-array as a workaround *wacko*

6

Sunday, July 2nd 2023, 4:25pm

hey.. yes .. sorry, yes, i forgot that the krpano array item is another object
here an example :

Source code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<action name="mytest" type="javascript"  autorun="onstart">
	// this function runs with global scope !
	krpano.set("myvar", "1-2-3");
	console.log("myvar",krpano.get("myvar"));
	console.log("myvar",krpano.myvar); // same as above

	krpano.actions.txtsplit(krpano.get("myvar"), "-", "myarr");
	console.log("myarr",krpano.get("myarr"));
	console.log("myarr",krpano.myarr); // same as above

	console.log("myarr[0]",krpano.myarr.getItem(0));
	console.log("myarr[0]",krpano.myarr.getItem(0).index);
	console.log("myarr[0]",krpano.myarr.getItem(0).value);

	console.log(krpano.myarr.getArray());

	// console.log(krpano);
</action>


[Log] myvar – "1-2-3"
[Log] myvar – "1-2-3"
[Log] myarr – qb {isArray: true, isDynArray: false, createItem: function, …}
[Log] myarr – qb {isArray: true, isDynArray: false, createItem: function, …}
[Log] myarr[0] – Eb {_type: "base", registerattribute: function, registerSetterGetter: function, …}
[Log] myarr[0] – 0
[Log] myarr[0] – "1"
[Log] [Eb, Eb, Eb] (3)

note: getArray() doesn't get you [ "1", "2", "3" ], but only a js array of kprano "base" objects
https://krpano.com/docu/plugininterface/#baseobject

This post has been edited 4 times, last edit by "indexofrefraction" (Jul 3rd 2023, 8:01pm)


7

Tuesday, July 4th 2023, 12:05pm

console.log("myarr[0]",krpano.myarr.getItem(0));
console.log("myarr[0]",krpano.myarr.getItem(0).index);
console.log("myarr[0]",krpano.myarr.getItem(0).value);

Those are exactly the problems I had with the array. Did´nt know that the indices and values are inside another object *wacko*
Thanks again for clearing that out!

note: getArray() doesn't get you [ "1", "2", "3" ], but only a js array of kprano "base" objects
I´ll keep that in mind *thumbup*