I have a constructor in JavaScript which contains 2 properties Key and Values array:

function Test(key, values) {
    this.Key = key;
    this.Values = values.map(values);
}

Then I created an array of Test objects:

 var testObjectArray = [];
 testObjectArray.push(new Test(1, ['a1','b1']), new Test(2, ['a1','b2']));

Now I want to map the testObjectArray to single key-value pair array which will be similar to :

[
    { "Key" : "1", "Value" : "a1" },
    { "Key" : "1", "Value" : "b1" },
    { "Key" : "2", "Value" : "a2" },
    { "Key" : "2", "Value" : "b2" },
]

How can I achieve this using array's map function?

share|improve this question

70% accept rate
That doesn't seem like a good use case for .map() to me. – Pointy Oct 25 '12 at 21:12
I'm guessing you mean .push? – Keyser Oct 25 '12 at 21:15
feedback

4 Answers

I'm sure there are other ways, but here's something with plain Javascript that does what you want:

http://jsfiddle.net/KXBRw/

function Test(key, values) {
    this.Key = key;
    this.Values = values;//values.map(values);
}

function getCombinedTests(testObjectArray) {
    var all = [];
    for (var i = 0; i < testObjectArray.length; i++) {
        var cur = testObjectArray[i];
        for (var j = 0; j < cur.Values.length; j++) {
            all.push({"Key": ""+cur.Key, "Value": cur.Values[j]});
        }
    }
    return all;
}

var testObjectArray1 = [];
testObjectArray1.push(new Test(1, ['a1','b1']), new Test(2, ['a1','b2']));

var combined = getCombinedTests(testObjectArray1);

console.log(combined);
share|improve this answer
feedback

var mapped = []; $.each(testObjectArray, function(key, value) { for(x in value.Values) { mapped.push({ Key: value.Key, Value: x }); } });

share|improve this answer
feedback

You could use .reduce(), .concat() and .map() for this.

var result = testObjectArray.reduce(function(res, obj) {
    return res.concat(obj.Values.map(function(val) {
        return {"Key":obj.Key, "Value":val};
    }));
}, []);

Not sure what values.map(values); was supposed to do though.

DEMO: http://jsfiddle.net/BWNGr/

[
    {
        "Key": 1,
        "Value": "a1"
    },
    {
        "Key": 1,
        "Value": "b1"
    },
    {
        "Key": 2,
        "Value": "a1"
    },
    {
        "Key": 2,
        "Value": "b2"
    }
]

If you're super strict about not creating unnecessary Arrays, you can tweak it a little and use .push() instead of .concat().

var result = testObjectArray.reduce(function(res, obj) {
    res.push.apply(res, obj.Values.map(function(val) {
        return {"Key":obj.Key, "Value":val};
    }));
    return res;
}, []);

DEMO: http://jsfiddle.net/BWNGr/1/

share|improve this answer
feedback

I guess you are misunderstanding map(). her is a very simple example:

a = [1, 2, 3]
b = a.map(function (i) { return i + 1 })
// => [2, 3, 4]

Her is the MDN documentation for map: http://goo.gl/RWHX1. So you should rethink the usage of map in your case. By the way - your example is not working, because values is not a function.

Here is a possible solution:

res = [];
a = [['a1','b1'],['a1','b2']];

for (var i = 0; i < a.length; ++i) {
  for(var j = 0; j < a[i].length; ++j) {
    res.push({"Key": i + 1 , "Value" : a[i][j]});
  }
}
share|improve this answer
feedback

Your Answer

 
or
required, but never shown
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.