1

Is it possible (and if so how) to merge tags with the same value so that one tag has an array of all its galleries?

For example, begin with this:

[{
    tag: "Kings",
    galleries: [
        "2016 Kings Draft Night"
    ]
}, {
    tag: "Draft",
    galleries: [
        "2016 Kings Draft Night"
    ]
}, {
    tag: "Kings",
    galleries: [
        "2016-17 Sacramento Kings Uniforms"
    ]
}]

and end with this:

[{
    tag: "Kings",
    galleries: [
        "2016 Kings Draft Night", "2016-17 Sacramento Kings Uniforms"
    ]
}, {
    tag: "Draft",
    galleries: [
        "2016 Kings Draft Night"
    ]
}]

Any help is much appreciated. Stumped on this

6 Answers 6

2

You can filter() to concat() existing tag galleries, while at the same time remove the unnecessary ones.

var result = data.filter(function(item) {
  var ref = this[item.tag];
  if(!ref) {
    return (this[item.tag] = item);
  }
  ref.galleries = ref.galleries.concat(item.galleries);
}, {});

var data = [{
  tag: "Kings",
  galleries: [
    "2016 Kings Draft Night"
  ]
}, {
  tag: "Draft",
  galleries: [
    "2016 Kings Draft Night"
  ]
}, {
  tag: "Kings",
  galleries: [
    "2016-17 Sacramento Kings Uniforms"
  ]
}];

var result = data.filter(function(item) {
  var ref = this[item.tag];
  if(!ref) {
    return (this[item.tag] = item);
  }
  ref.galleries = ref.galleries.concat(item.galleries);
}, {});

document.write('<pre>' + JSON.stringify(result, 0, 4) + '</pre>');

Sign up to request clarification or add additional context in comments.

1 Comment

Not exactly clean or easy to understand for an inexperienced coder, but I really like this. Probably almost optimal as well.
1

I'm sure there are even faster ways to do this. But here is what I came up with:

var data = [{ tag: "Kings", galleries: [ "2016 Kings Draft Night" ] }, { tag: "Draft", galleries: [ "2016 Kings Draft Night" ] }, { tag: "Kings", galleries: [ "2016-17 Sacramento Kings Uniforms" ]}]


var newData = {};
data.forEach(function(a) {
  if (!newData[a.tag]) {
    newData[a.tag] = a.galleries;
  } else {
    newData[a.tag] = newData[a.tag].concat(a.galleries);
  }
});

var complete = [];
Object.keys(newData).forEach(function(key) {
  complete.push({
    tag: key,
    galleries: newData[key]
  });
});

console.log(complete);

1 Comment

It worked brother thank you. could not for the life of me figure that out!!
0

you can try like this.

var objA = [{
tag: "Kings",
galleries: [
    "2016 Kings Draft Night"
]
}, {
tag: "Draft",
galleries: [
    "2016 Kings Draft Night"
]
}, {
tag: "Kings",
galleries: [
    "2016-17 Sacramento Kings Uniforms"
]
}]


var result = [];

for (var property in objA) {
if (objA.hasOwnProperty(property)) {
    if (result.filter(function(e) { if( e.tag == objA[property].tag) return    e.galleries.push(objA[property].galleries[0]) }).length > 0) {
    }
    else
    {
      result.push(objA[property]);
    }

   }
  }

 console.log(result);

plunker : http://plnkr.co/edit/7AijNY4Tb4DSDaiEQEEB?p=preview

Comments

0

Merge function :

function merge(a){
 var r = {},n = [];
 for(var d in a) 
  r[a[d].tag] = (r[a[d].tag]) ? r[a[d].tag].concat(a[d].galleries) : a[d].galleries;
 for(var d in r) n.push({'tag':d,'galleries':r[d]});
 return(n);
}

And a test :

a = [{tag: "Kings",galleries: ["2016 Kings Draft Night"]},{tag: "Draft",galleries: ["2016 Kings Draft Night"]},{tag: "Kings",galleries: ["2016-17 Sacramento Kings Uniforms"]}];
console.log(merge(a));

Comments

0

You can use .filter(), .reduce(), .map(), rest element, spread element

var arr = [{
  tag: "Kings",
  galleries: [
    "2016 Kings Draft Night"
  ]
}, {
  tag: "Draft",
  galleries: [
    "2016 Kings Draft Night"
  ]
}, {
  tag: "Kings",
  galleries: [
    "2016-17 Sacramento Kings Uniforms"
  ]
}];

var filterTags = (a, type) => a.filter(o => o.tag === type)
  .reduce((curr, next) => {
    [...curr.galleries] = [...curr.galleries, ...next.galleries];
    return curr
  });

var res = ["Kings", "Draft"].map(val => filterTags(arr, val));

console.log(res);

Comments

0
for (var i = 0; i < obj.length-1; i++){
    var currTag = obj[i].tag;  // get first element

   for (var j = i + 1; j < obj.length; j++){ // Search for matching tags
       var newTag = obj[j].tag; 
       if (currTag === newTag){
         array1 = obj[i].galleries;  // get gallery for prevElement
         array2 = obj[j].galleries; // get gallery for curr element
         Array.prototype.push.apply(array1, array2); // merge arrays
         obj.splice(j,1); // remove the curr array from obj
       }
   }  
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.