JavaScript How to merge elements in array by first words in JS? Is it possible? merge elements array,merge elements array php,merge array elements

I have the following array

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

I want to get this array from it:

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

Is it possible?

Answer:1

you can try:

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

Answer:2
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));
Answer:3

This seems to work for me:

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);

Basically just loop through each one, and use the first word as a key to an array of strings to be appended, then loop through the object and join the keys with all of their array elements.

N.B. References to key are the first word

Answer:4

Some operations in javascript returns unexpected results. One is extremely strange: [] + 1 = "1" Can anybody explain why it works like that?

Some operations in javascript returns unexpected results. One is extremely strange: [] + 1 = "1" Can anybody explain why it works like that?

  1. javascript array plus array
  2. javascript array plus operator
  3. javascript array plus object
  4. javascript array plus number
  5. array javascript plus sign
  6. javascript empty array plus empty object

i have this way of getting tomorrows date. var tomorrow = date.getDate() + "/" + month + "/" + date.getFullYear(); this returns 2/10/2013 where as i want it to be 02/10/2013 (with the zero for ...

i have this way of getting tomorrows date. var tomorrow = date.getDate() + "/" + month + "/" + date.getFullYear(); this returns 2/10/2013 where as i want it to be 02/10/2013 (with the zero for ...

I want to make text area that will handle image drop event on it from the desktop. I found that I could attach event to html element, but it doesn't work properly. I don't find any error, but it ...

I want to make text area that will handle image drop event on it from the desktop. I found that I could attach event to html element, but it doesn't work properly. I don't find any error, but it ...

  1. jquery drag drop file upload
  2. jquery drag drop list
  3. jquery drag drop table rows
  4. jquery drag drop example
  5. jquery drag drop file
  6. jquery drag drop image
  7. jquery drag drop event
  8. jquery drag drop plugin
  9. jquery drag drop upload
  10. jquery drag drop order
  11. jquery drag drop clone
  12. jquery drag drop get dropped element
  13. jquery drag drop
  14. jquery drag drop sort
  15. jquery drag drop table
  16. jquery drag drop mobile
  17. jquery drag drop grid
  18. jquery drag drop sortable
  19. jquery drag drop image upload
  20. jquery drag drop list items

How to call an swipe event?. I want to trigger an event in swipe. The below code is works fine in normal query event. don't know how to achieve in meteor event. $('.your-class').on('swipe', ...

How to call an swipe event?. I want to trigger an event in swipe. The below code is works fine in normal query event. don't know how to achieve in meteor event. $('.your-class').on('swipe', ...