如何通过JS中的第一个单词合并数组中的元素?可能吗?

时间:2015-05-25 09:52:02

标签: javascript arrays

我有以下数组

['.some_class &.green_mod','.some_class &.red_mod','another_class &.green_mod','another_class &.orange_mod']

我想从中获取此数组:

['.some_class &.green_mod &.red_mod','another_class &.green_mod &.orange_mod']

有可能吗?

4 个答案:

答案 0 :(得分:0)

你可以尝试:

var preArr = ['.some_class &.green_mod', '.some_class &.red_mod', 'another_class &.green_mod', 'another_class &.orange_mod'];
var newArr = [];
preArr.forEach(function (item) {
    var has = false;
    var preWords = item.split('&');
    for (var i = 0; i < newArr.length; ++i) {
        var newWords = newArr[i].split('&');
        if (newWords[0] == preWords[0]) {
            has = true;
            for (var j = 0; j < preWords.length; ++j) {
                if (newWords.indexOf(preWords[j]) < 0) {
                    newWords.push(preWords[j]);
                }
            }
            newArr[i] = newWords.join('&');
        }
    }
    if (!has) {
        newArr.push(item);
    }
});
console.log(newArr);

demo

答案 1 :(得分:0)

var test = (function() {
  var fmap1 = function(e) { return e.trim().split(/\s+/); };
  var fmap2 = function(e) { return e.join(" "); };
  var fsort = function(e1,e2) { return e1[0] == e2[0] ? 0 : e1[0] > e2[0] ? 1 : -1; };
  return function test(a) {
    var a1 = a.map(fmap1).sort(fsort);
    var s, a2 = [];
    for (var i = 0, l = a1.length; i < l; i++) {
      if (s != a1[i][0]) {
        s = a1[i][0];
        a2.push([s]);
      }
      a1.push.apply(a2[a2.length - 1], a1[i].slice(1));
    }
    return a2.map(fmap2);
  };
})();

var arr = ['  .some_class &.green_mod',' another_class &.green_mod','.some_class &.red_mod','another_class &.orange_mod'];
console.log(test(arr));

答案 2 :(得分:0)

这似乎对我有用:

var arr = ['.some_class &.green_mod','.some_class &.red_mod','another_class &.green_mod','another_class &.orange_mod'];
var obj = {};
var finalArr = [];
for(var i=0,c=arr.length;i<c;i++)
{
    var parts = arr[i].split(' ');
    var key = parts[0];
    if(!obj[key]) obj[key] = [];
    obj[key].push(parts.slice(1).join(' '));
}
var keys = Object.keys(obj);
for(var i=0,c=keys.length;i<c;i++)
{
    var key = keys[i];
    finalArr.push(key+' '+obj[key].join(' '))
}
console.log(finalArr);

基本上只是循环遍历每一个,并使用第一个单词作为要附加的字符串数组的键,然后遍历对象并将键与所有数组元素连接。

N.B。对key的引用是第一个单词

答案 3 :(得分:0)

对于密钥+分隔符+值等数据的解决方案,您确定密钥中没有重复,或者您不关心值是否重复

// maping function that treats part before separator as key and saves incremetaly 
// all values under that key (duplicated vales are possible)
// only one separator per input entry is allowed
function mapFun (el, obj, separator) {
    var e = el.split(separator);
    var key = e[0];
    var val = e[1];
    obj[key] = obj[key] ? obj[key] + separator + val : separator + val; 
}

function combineClasses (arr) {
    var result = [];
    var separator = ' &';
    var obj = Object.create(null);  // create empty object without any properties or inheritance chain

    for (var i=0; i<arr.length; i++) {
        mapFun(arr[i], obj, separator);
    }

    for (var p in obj) {
        result.push (p + obj[p]);
    }

    return result;
}
combineClasses(arr);

Bellow解决方案适用于之前的数据和数据,如键+分隔符+值1 +分隔符+值2,并且不允许相同键的重复值

function mapFun (el, separator, obj) {
    var parts = el.split(separator);
    var key = parts[0];
    if (!obj[key]) {
        obj[key] = Object.create(null);     
    }
    for (var i=1; i<parts.length; i++) {
        obj[key][parts[i]] = null;  
    }
}

function combineClasses (arr) {
    var result = [];
    var separator = ' &';
    var obj = Object.create(null);            // create empty object without any properties     

    for (var i=0; i<arr.length; i++) {
        mapFun(arr[i], separator, obj);
    }

    for (var p in obj) {
        var values = Object.keys(obj[p]).join(separator);
        result.push (p + separator + values);
    }
    return result;
}

combineClasses(arr);

arr是您的数据进入的地方

var arr = ['.some_class &.green_mod','.some_class &.red_mod','another_class &.green_mod','another_class &.orange_mod']