2017-10-19 57 views
0

我有一個不可預知的數組數量,如下圖所示。js的linq:如何獲得多個陣列的笛卡爾乘積

arrays

使用LINQ對JS我想獲得這些陣列即笛卡爾積。

{ 
    {AttributeTypeId: 10, AttributeId: 34, AttributeName: "b11 13-128"} 
    {AttributeTypeId: 11, AttributeId: 56, AttributeName: "21/uk4"}, 
    {AttributeTypeId: 13, AttributeId: 69, AttributeName: "Boy"} 
}, 
    {AttributeTypeId: 10, AttributeId: 33, AttributeName: "b10 13-128"}, 
    {AttributeTypeId: 11, AttributeId: 56, AttributeName: "21/uk4"}, 
    {AttributeTypeId: 13, AttributeId: 69, AttributeName: "Boy"} 
}, 
{ 
    {AttributeTypeId: 10, AttributeId: 38, AttributeName: "G01 13-102"}, 
    {AttributeTypeId: 11, AttributeId: 56, AttributeName: "21/uk4"}, 
    {AttributeTypeId: 13, AttributeId: 69, AttributeName: "Boy"} 
}, 
{ 
    {AttributeTypeId: 10, AttributeId: 34, AttributeName: "b11 13-128"}, 
    {AttributeTypeId: 11, AttributeId: 54, AttributeName: "19/uk3"}, 
    {AttributeTypeId: 13, AttributeId: 69, AttributeName: "Boy"} 
} 
.... 
etc. 

我該如何實現它?

+0

歡迎堆棧溢出。請參閱幫助中心的[問]。這不是一個代碼寫入服務。 –

回答

1

對於結果中想要的每個數組,只需添加一個select。

var result = Enumerable.from(data[0]).selectMany(a => 
    Enumerable.from(data[1]).selectMany(b => 
     Enumerable.from(data[2]).select(c => 
      [a, b, c] 
     ) 
    ) 
).toArray(); 

這是相當於查詢語法:

from a in data[0] 
from b in data[1] 
from c in data[2] 
select [a, b, c] 
+0

謝謝你!不幸的是我不能投票你的答案,因爲我是一個新用戶 –