3

I have two JavaScript arrays below that both have the same number of entries, but that number can vary.

[{"branchids":"5006"},{"branchids":"5007"},{"branchids":"5009"}]      
[{"branchnames":"GrooveToyota"},{"branchnames":"GrooveSubaru"},{"branchnames":"GrooveFord"}] 

I want to combine these two arrays so that I get

[{"5006":"GrooveToyota"},{"5007":"GrooveSubaru"},{"5008":"GrooveFord"}]

I'm not sure how to put it into words but hopefully someone understands. I would like to do this with two arrays of arbitrary length (both the same length though).

Any tips appreciated.

4
  • They guaranteed to be in the same order? Commented Oct 12, 2011 at 15:17
  • Sorry, I edited it just now that was a typo. Commented Oct 12, 2011 at 15:18
  • Uh, in your example they're all 5006, which makes it even easier :( Commented Oct 12, 2011 at 15:18
  • Yes that was the typo I was referring to. Commented Oct 12, 2011 at 15:19

4 Answers 4

6

It's kind of a zip:

function zip(a, b) {
    var len = Math.min(a.length, b.length),
        zipped = [],
        i, obj;
    for (i = 0; i < len; i++) {
        obj= {};
        obj[a[i].branchids] = b[i].branchnames;
        zipped.push(obj);
    }
    return zipped;
}

Example (uses console.log ie users)

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

3 Comments

Do you mean 'i' instead of [0] in the fourth line from bottom?
Using [0] there won't work, as it will get GrooveToyota and 5006 each time. Also there is a syntax error. Here is a working version of your zip approach: jsfiddle.net/K5Mur
@robert, you are correct sir. But it was a typo I've since fixed.
3
var ids = [{"branchids":"5006"},{"branchids":"5007"},{"branchids":"5009"}];
var names = [{"branchnames":"GrooveToyota"},{"branchnames":"GrooveSubaru"},{"branchnames":"GrooveFord"}];
var combined = [];

for (var i = 0; i < ids.length; i++) {
    var combinedObject = {};
    combinedObject[ids[i].branchids] = names[i].branchnames;
    combined.push(combinedObject);
}

combined; // [{"5006":"GrooveToyota"},{"5006":"GrooveSubaru"},{"5006":"GrooveFord"}]

Comments

0

Personally, I would do it IAbstractDownvoteFactor's way (+1), but for another option, I present the following for your coding pleasure:

var a = [{"branchids":"5006"},{"branchids":"5007"},{"branchids":"5009"}];
var b = [{"branchnames":"GrooveToyota"},{"branchnames":"GrooveSubaru"},{"branchnames":"GrooveFord"}];
var zipped = a.map(function(o,i){ var n={};n[o.branchids]=b[i].branchnames;return n;});

Comments

0

similar to @robert solution but using Array.prototype.map

var ids = [{"branchids":"5006"},{"branchids":"5007"},{"branchids":"5009"}], names = [{"branchnames":"GrooveToyota"},{"branchnames":"GrooveSubaru"},{"branchnames":"GrooveFord"}], merged = ids.map(function (o, i) { var obj = {}; obj[o.branchids]=names[i].branchnames; return obj; });

merged; //[{5006: "GrooveToyota"}, {5006: "GrooveSubaru"}, {5006:"GrooveFord"}]

Cheers!

Comments

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.