2016-11-04 64 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)