0

I'm trying to combine two arrays into an array of objects and I'm struggling with that.

Example:

arr1 = [a,b,c];
arr2 = [a,a,a,b,b,c,d,d];

The way I'd like to have these two combined:

combinedArr = [
    {name: a, amount: 3}, 
    {name: b, amount: 2}, 
    {name: c, amount: 1}
];

Note that only values stored in arr1 should be integrated, any arr2 values non-existent in arr1 are just left out. (in this example, it's "d")

Also important to know is that I'm using this for After Effect's Extendscript which limits me to the 3rd Edition of the ECMA-262 Standard, leaving me with good old classic javascript. Functionalities like concat, slice and join are not available.

I've tried my way around but I can't figure it out.. I'm sure its possible to achieve a solution with just two or three smart loops through the arrays.

Thanks in advance, Simon

EDIT: I have created confusion by not adding my own attempts at my problem. I'm sorry I didn't, I've been thinking about it overnight and wrote this question on my phone on the train.

I've already received amazing answers that I'm very happy about, just to prove I had no ill meaning about my question, I'll post what I had written before (not simplified, but from the actual code):

var createMarkerList = function() {
    var subList = _createMarkerListSub(); //in this example arr1
    var masterList = _createMarkerListMaster(); //in this example arr2
    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;
        }
    }
}

Please note that I did't try to get the easy way around by just asking you for the answer without trying to figure it out, I simply couldn't get my head around it for some reason.

3
  • 3
    Have you tried anything? Commented Sep 25, 2017 at 6:55
  • I agree with Rajesh, and this is just something so basic, i would recommend to read a javascript tutorial first Commented Sep 25, 2017 at 7:06
  • @Simon, remember it is very important to share your effort. It can be code/ algorithms/ searched links. Unless you share it, you question will look like a requirement and not a problem statement. This might attract unwanted votes/comments Commented Sep 25, 2017 at 7:25

2 Answers 2

2

you will have to iterate over both arrays and keep a count of all elements in arr1.

var arr1 = ['a','b','c'];
var arr2 = ['a','a','a','b','b','c','d','d'];
var combinedObject= {}, combinedArray = [];
for(var i=0; i<arr1.length; i++)
   combinedObject[arr1[i]] = 0;

for(var i=0; i<arr2.length; i++)
   if(combinedObject.hasOwnProperty(arr2[i]))
 combinedObject[arr2[i]]++;

for(var key in combinedObject)
   combinedArray.push({'name': key, 'amount':combinedObject[key]});

console.log(combinedArray);

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

7 Comments

Should we answer such questions that show no sign of input?
@Rajesh its arguable, but maybe OP is not getting how to do it so the answer will help him understand. one can hope that OP would look and try to understand the solution rather than just copy it. The other option would be to just ignore the question but that won't help anyone anyways.
@Dij Yes it is arguable, but OP hasn't even shown his attempt at it. OP mentioned I've tried my way around but I can't figure it out, but didn't showed what he/she attempted.
@Dij The reason why I do not prefer to answer such post is that it promotes spoon-feeding. People put their input data and expected output. This to me is requirement and not a problem statement. Sharing effort makes it a problem statement as we know what mistake has been made. This will also help us in understanding the level of user and the answer can be formatted accordingly. But this is just my POV.
@Rajesh, thank you for sorting this out. I understand that the way the question was posed it looked a lot like a demand. Its important to talk about this issue and I'll learn from it!
|
2

You could use a hash table and use it for counting.

You need to loop over arr1 tocreate the hash table and the result set and a second loop over the item to count and increment the amount property.

var arr1 = ['a', 'b', 'c'],
    arr2 = ['a', 'a', 'a', 'b', 'b', 'c', 'd', 'd'],
    hash = {},
    result = [],
    i;
    
for (i = 0; i < arr1.length; i++) {
    hash[arr1[i]] = { name: arr1[i], amount: 0 };
    result.push(hash[arr1[i]]);
}

for (i = 0; i < arr2.length; i++) {
    hash[arr2[i]] && hash[arr2[i]].amount++;
}

console.log(result);

1 Comment

Thank you so much! Both of the answers worked, but I wasn't aware of hash tables. I'll read into it!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.