0
Отвечен

script indexOf()

Romain LE CAN 5 лет назад в Bugs and problems обновлен a pivovarov 4 года назад 11

Hello,


i want to use a javascript function on my project but when i use it i can see on the log "function is not defined"

the function is indexOf() to return the position of an element in an array.

array.indexOf(element)

is it the right syntax in iridium ?

thanks for your help

На рассмотрении

Hello.

Example of using the methodindexOf():

var str = 'Быть или не быть, вот в чём вопрос.';
var count = 0;
var pos = str.indexOf('в');

while (pos !== -1) {
count++;
pos = str.indexOf('в', pos + 1);
}

IR.Log(count); // 3

in your example you use a string but i want to use it with an array is it ok too ? 

Hello.

The argument can be a substring. If you have an array, convert it to string.

Array.prototype.indexOf is not implemented in Iridium. I wrote a script that contains lot of functions that are missed in Iridium.

https://github.com/bladerunner2020/js-ext/blob/master/js-ext.js

You could take Array.prototype.indexOf form this script. )

it's not too easy because i don't want to count something but make a matrix between 2 arrays, first is list of drivers and second list of physical switch, the position on each array indicate the virtual tag where i store the dimming value. and i need to know the position in the array not in a string because the number of character change every time i will change elements of  array.

script is not complete, it's just to show you want i need to do.


function send (pos, value)
{
if(value == 1)
{
var virtualdimmer = "Server.Tags.virtual " + pos;
var dimvalue = IR.GetVariable(virtualdimmer);
} else {
var dimvalue = value;
}
var detaildriver = arraydriver[pos].split("_");
var detaillength = detaildriver.length;
IR.Log("detaildriver : " + detaildriver + " detail length : " + detaillength);
for (var j = 0 ; j < detaillength ; j++)
{
var stringdriver = detaildriver[j];
// IR.Log("j : " + j + " send driver : " + stringdriver);
IR.GetDevice("hdl").Set(stringdriver, dimvalue);
};
}

function matrixlight(numuv, onoff)
{
var position = arrayuv.indexOf(numuv);
while (position != -1)
{
if(onoff == 1)
{
var virtualdimmer = "Server.Tags.virtual " + position;
var dimmer = IR.GetVariable(virtualdimmer);
} else {
var dimmer = 0;
}

IR.Log("send : position=" + position + " dimmer=" + dimmer);
send(position, dimmer)
position = arrayuv.indexOf(numuv, position + 1);
}

};

Hello.

Iridium uses the ECMAScript Edition 3 specification. The indexOf method is added to work with arrays only in ECMAScript 5.1 specification (ECMA-262).

You can use the following code to access the array by index:

var array = ['hello', 'send', 'test', 65535];
var index;
for (index = 0;
index < array.length;
++index)
{
IR.Log(index + ": " + array[index]);
}

var first = array[0]; // the first element of the array

IR.Log("First: " + first);

When will you update the JS engine to a newer version of ECMA? There are many functions in the newer versions that make life a lot easier.

In the meantime you can extend the array prototype like so:

if (!Array.prototype.indexOf) {
  Array.prototype.indexOf = function(value) {
    for (var i = 0; i < this.length; i++) {
      if (this[i] == value) {
        return i;
      }
    }
    return -1;
  };
}

And call it like you wanted in your code:

var position = arrayuv.indexOf(numuv);

The upgrade version of JavaScipt in iRidium is not planned. Perhaps the situation will change in the future.

Сервис поддержки клиентов работает на платформе UserEcho