2016-11-04 30 views
0

我遇到數組問題。合併並返回唯一陣列問題

我的要求: 我有一個對象說

data = { 
192.168.2.1: ["alpha", "beta", "delta"], 
192.168.2.2: ["alpha"], 
192.168.2.3: ["delta"], 
192.168.2.4: [] 
} 

我要合併的所有值(陣列)組合成一個陣列,這樣我可以從用戶界面讀取。

所需的輸出:[α,β,δ]

當前實現:

var allControllerList = []; 
var uniqueControllerList = []; 


    $.each(data, function(i, el){ 
     allControllerList = allControllerList.concat(el); 
    }); 

    $.each(allControllerList, function(index, el) { 
     if($.inArray(el, uniqueControllerList) === -1) uniqueControllerList.push(el); 
    }); 

如果我想讀它的UI,我需要再次做到這一點:

  <select id='ssid-list' multiple='multiple'> 
       <% _.each(uniqueControllerList, function(ssid, index) { %> 
       <option value='<%=controllerIp+ssid%>'> 
        <%=ssid%> 
       </option> 
       <% }); %> 
      </select> 

我在讀數組三次,我w作爲尋找更有效的實施。 (Underscore,jQuery或JS)。

感謝,

+1

什麼是α,β,δ?對象還是原始類型? – acontell

回答

3

使用Underscore.js

您可以使用_.union

var data = { 
 
    "192.168.2.1": ["alpha", "beta", "delta"], 
 
    "192.168.2.2": ["alpha"], 
 
    "192.168.2.3": ["delta"], 
 
    "192.168.2.4": [] 
 
} 
 

 
//exctract values from the object 
 
var values = _.values(data); 
 

 
//use .apply() to give _.union() an array of arrays 
 
var joinedData = _.union.apply(null, values); 
 

 
console.log(joinedData);
<script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.3/underscore-min.js"></script>

使用香草的JavaScript

你可以簡單地加入everyt興奮,然後篩選唯一性。跨瀏覽器兼容IE9及以上(所以,沒有IE8)。

var data = { 
 
    "192.168.2.1": ["alpha", "beta", "delta"], 
 
    "192.168.2.2": ["alpha"], 
 
    "192.168.2.3": ["delta"], 
 
    "192.168.2.4": [] 
 
} 
 

 
var seenItems = {}; 
 

 
//Object.values() is not widely supported, otherwise it would have been better 
 
//_.values() can be used instead but this solution is showcasing pure JS 
 
var joinedData = Object.keys(data) 
 
    .reduce(function(memo, key){ 
 
    //combine all arrays into one 
 
    return memo.concat(data[key]); 
 
    }, []) 
 
    .filter(function(item) { 
 
    //filter the array by keeping track of what you've seen or not. 
 
    var result = !seenItems[item]; 
 

 
    seenItems[item] = true; 
 
    
 
    return result; 
 
    }); 
 

 
console.log(joinedData);

使用ES6

沒有得到廣泛的支持在瀏覽器中,特別是如果你想支持的東西比現在的瀏覽器更舊。但是,在某些時候,因爲它有多容易,在此添加它。

const data = { 
 
    "192.168.2.1": ["alpha", "beta", "delta"], 
 
    "192.168.2.2": ["alpha"], 
 
    "192.168.2.3": ["delta"], 
 
    "192.168.2.4": [] 
 
} 
 

 
// combine all arrays together 
 
const dupedData = [].concat(...Object.values(data)); 
 

 
//use a Set to remove duplicates and turn it into an array 
 
const deDupedData = Array.from(new Set(dupedData)); 
 

 
console.log(deDupedData);

1

我相信這樣的事情可以幫助

let data = { 
    '192.168.2.2': ['alpha'], 
    '192.168.2.3': ['delta'], 
    '192.168.2.4': ['beta'], 
    '192.168.2.5': ['alpha'] 
} 

let key = Object.keys(data) 

const array = [] 
for (key in data) { 
    if (data[key]) { 
    array.push(data[key].toString()) 
    } 
} 

console.log(array)