2017-04-11 72 views
0

阵列指望我从一个API我想基于的几个条件

data: { 
    "total_slots": int, 
    "occupied_count": int, 
    "occupied_slots" : [int] e.g.[452,453,459] (here each int in the array signfies an occupied slot), 
    "slots_down" : [int] e.g.[460,462] (here each int in the array signfies a down slot) 
} 

以下结果我想以下条件

VAR OCCUPIED, length of the list which are occupied minus length of common slots in occupied and slots_down 
VAR TOTAL_SLOTS = total slots (which are 31 i think) - slots which are down 
VAR AVAILABLE = (31 - length(slots_down)) - length(slots occupied AND not down) 

槽是31是固定的。

var ALL_SLOTS = [452, 453, 454, 455, 456, 457, 458, 459, 460, 461, 462, 463, 464, 465, 466, 508, 509, 510, 511, 512, 513, 514, 515, 516, 517, 518, 519, 520, 521, 522, 523]; 

我如何能满足或/与条件像VAR AVAILABLE = (31 - length(slots_down)) - length(slots occupied AND not down)

这是我当前的代码

s = JSON.parse(s); 
console.log(s); 
SLOTS_DOWN = s.slots_down; 
var total_slots = s.total_slots; 
SLOTS_DOWN = s.slots_down.length; 
console.log('down slots are ' + SLOTS_DOWN); 
total_slots = parseInt(total_slots) - parseInt(SLOTS_DOWN); 
var occupied = s.occupied_count; 
var available = parseInt(total_slots) - parseInt(occupied) - parseInt(SLOTS_DOWN); 
+0

是什么'slots_down'? –

+0

也需要一组slots_down。 – dev8080

+0

与你的代表的用户应该知道共享代码 – Rajesh

回答

2

根据这些:

VAR占据,其占用共同 插槽减去长度在被占领和列表的长度slots_down VAR TOTAL_SLOTS =总的插槽(这是31我认为) - 插槽下降 VAR AVAILABLE =(31 - 长度(slots_down)) - 长度(插槽占用和不下来)

你可以试试这个:

var OCCUPIED = data.occupied_count - 
      data.occupied_slots.filter(function(elem){ 
             return data.slots_down.indexOf(elem)>-1; 
       }).length; 

var TOTAL_SLOTS = data.total_slots.length - data.slots_down.length; 


var AVAILABLE = ((data.total_slots.length - data.slots_down.length)) - 
      data.occupied_slots.filter(function(elem){ 
      return data.slots_down.indexOf(elem)==-1; 
     }).length 
1

这应该工作:

data = { 
 
     "total_slots": 124, 
 
     "occupied_count": 3, 
 
     "occupied_slots" : [245, 326, 256], 
 
     "slots_down" : [245, 136] 
 
    } 
 
    var down_but_not_occ = 0; 
 
    data.slots_down.map(function(v, i){ 
 
     if(data.occupied_slots.indexOf(v) === -1){ 
 
    \t    down_but_not_ocC++; 
 
     }; 
 
    }) 
 

 
    var available_slots = parseInt(data.total_slots) - data.occupied_slots.length - down_but_not_occ; 
 
    console.log(available_slots);

如果你错了,

total_slots = parseInt(total_slots) - parseInt(SLOTS_DOWN); // slots down subtracted 
var occupied = s.occupied_count; 
var available = parseInt(total_slots) - parseInt(occupied) - parseInt(SLOTS_DOWN); // slots down AGAIN subtracted 
+0

那占用和总插槽呢? –

+0

@AliZia现在这应该工作 –