當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


d3.js d3.permute()用法及代碼示例

D3.js中的d3.permute()函數用於使用給定的指定索引數組對給定數組的元素進行置換,並且它返回包含每個給定索引的對應元素的數組。

用法:

d3.permute(array, Index)

參數:此函數接受上麵提到並在下麵描述的兩個參數:


  • Array:它是在其上執行置換的元素數組。
  • Index:是給定索引,用於對給定數組元素進行置換,並相應地將給定數組的元素返回到數組中。

返回值:它返回排列的元素數組。

以下程序說明了D3.js中的d3.permute()函數。

示例1:在下麵的代碼中,將使用給定的指定索引數組對給定數組的元素進行置換,並返回包含每個給定索引的對應元素的數組。

<html> 
  
<head> 
    <title> 
      Getting permuted array of elements 
  </title> 
</head> 
  
<body> 
    <script src='https://d3js.org/d3.v4.min.js'> 
  </script> 
  
    <script> 
        // Initialising the arrays of elements 
        var Array1 = ["gfg", "Geek", "Geeks", "GeeksforGeeks"]; 
        var Array2 = ["a", "ab", "abc", "abcd"]; 
  
        // Initialising the indexes 
        var Index1 = [1, 2, 0, 3]; 
        var Index2 = [3, 1, 2, 0]; 
  
        // Calling to d3.permute() function 
        A = d3.permute(Array1, Index1); 
        B = d3.permute(Array2, Index2); 
  
        // Getting permuted array of elements 
        document.write(A + "<br>"); 
        document.write(B + "<br>"); 
    </script> 
</body> 
  
</html>

輸出:

Geek, Geeks, gfg, GeeksforGeeks
abcd, ab, abc, a

示例2:

<html> 
  
<head> 
    <title> 
      Getting permuted array of elements 
  </title> 
</head> 
  
<body> 
    <script src='https://d3js.org/d3.v4.min.js'> 
  </script> 
  
    <script> 
        // Taking the array of elements and  
        // indexes as the parameters 
        // for the function d3.permute() 
        A = d3.permute(["Hi", "Hello", "Greet"], [1, 2, 0]); 
        B = d3.permute(["Ram", "Shyam", "Geeta", "Hari"], 
                       [3, 1, 2, 0]); 
  
        // Getting permuted array of elements 
        document.write(A + "<br>"); 
        document.write(B + "<br>"); 
    </script> 
</body> 
  
</html>

輸出:

Hello, Greet, Hi
Hari, Shyam, Geeta, Ram

參考: https://devdocs.io/d3~4/d3-array#permute



相關用法


注:本文由純淨天空篩選整理自Kanchan_Ray大神的英文原創作品 D3.js | d3.permute() function。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。