合并并返回唯一的数组问题

时间:2016-11-04 19:48:21

标签: javascript jquery arrays underscore.js

我的数组有问题。

我的要求: 我有一个对象说

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

我想将所有值(数组)合并到一个数组中,以便我可以从UI中读取它。

  

期望输出:[alpha,beta,delta]

当前实施:

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>

我正在阅读一个数组三次,我正在寻找更高效的实现。 (Underscore,jQuery或JS)。

谢谢,

2 个答案:

答案 0 :(得分:3)

使用Underscore.js

您可以使用_.union

&#13;
&#13;
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);
&#13;
<script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.3/underscore-min.js"></script>
&#13;
&#13;
&#13;

使用vanilla JavaScript

您可以简单地加入所有内容,然后过滤唯一性。 IE9及以上版本的跨浏览器兼容(因此,没有IE8)。

&#13;
&#13;
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);
&#13;
&#13;
&#13;

使用ES6

浏览器不受广泛支持,特别是如果您想支持比当前浏览器更早的内容。但是,在某些时候,因为它很容易在这里添加它。

&#13;
&#13;
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);
&#13;
&#13;
&#13;

答案 1 :(得分: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)