I'm facing a challenge trying to merge two arrays into an array of objects.
For example:
arr1 = [a,b,c];
arr2 = [a,a,a,b,b,c,d,d];
The desired combination:
combinedArr = [
{name: a, amount: 3},
{name: b, amount: 2},
{name: c, amount: 1}
];
Note that only values from arr1 should be included, any values in arr2 not present in arr1 are omitted. (in this case, it's "d")
It's worth mentioning that I'm working with After Effect's Extendscript which limits me to the 3rd Edition of the ECMA-262 Standard, hence conventional javascript functions like concat, slice and join are unavailable.
I've made attempts but haven't been able to crack it.. I believe there's a solution involving just a couple of clever loops through the arrays.
Thanks, Simon
EDIT: I see how I may have caused confusion by not sharing my own efforts at solving the problem. My apologies for omitting that, I hastily wrote this question on my phone while traveling.
I've already received some great responses which I appreciate, to clarify my intentions, here's what I initially tried before asking (without simplification, directly from the code):
var createMarkerList = function() {
var subList = _createMarkerListSub(); //arr1 in this scenario
var masterList = _createMarkerListMaster(); //arr2 in this scenario
var output = [];
for(var i=0;i<subList.length;i++){
var uniqueMarker = subList[i];
output.push({
name: uniqueMarker,
amount: 0,
});
}
for(var i=0;i<masterList.length;i++){
var genericMarker = masterList[i];
if(output[i].name == genericMarker){
output[i].amount = output[i].amount +1;
}
}
}
I want to emphasize that I didn't seek an easy way out by simply asking for the solution without attempting it myself, I genuinely struggled to come up with a solution on my own.