从javascript数组列表中创建匹配队列

时间:2017-04-16 09:58:28

标签: javascript arrays

我有基于数据和设备需要执行的作业列表。 这是我的样本数据

device = [ device0, device1, device2 ]
device0 = {
    group: [MATCH1, MATCH2]
}
device1 = {
    group: [MATCH1, MATCH2, MATCH3]
}
device2 = {
    group: [MATCH2, MATCH3]
}
data = {
  "destination": "destination",
  "text": "Test send message",
  "hlr": "MATCH2"
}

如何修复我的代码,以便我的数据可以匹配并执行任务到设备。对不起,这是我的第一个问题。我还在学习编程,而且我的英语不流利。 这是我的示例代码:

const jobs = []
jobs.push(data)
        if (jobs.length > 0) {
            let task= jobs.shift()
            device.push(device.splice(0, 1)[0]); // swap device position
            dataLoop: for (var i = 0; i < device.length; i++) {
                itemLoop: for (var j = 0; j < device[0].group.length; j++) { // cycle through until find match
                    if (device[0].group[j].toString() === data.hlr.toString()) {
                        console.log(device[0].group[j] + ':' + data.hlr.toString()); // output should be MATCH2 : MATCH2
                        break dataLoop;
                    }
                }
                device.push(device.splice(0, 1)[0]);
            }
            device[0].sendData({ // using first device to perform task
                receiver: data.destination,
                text: data.text,
                request_status: true
            }, function(err, ref) {
                setTimeout(function() {
                   if (err) { console.log(err) }
                   console.log(ref)
                }, 5000)
            });
        }

1 个答案:

答案 0 :(得分:0)

Theres a typo:

itemLoop: for (var j = 0; j < device[i].group.length; j++) { 

必须是

itemLoop: for (var j = 0; j < device[0].group.length; j++) { //