20

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?

2
  • That doesn't seem like a good use case for .map() to me. Commented Oct 25, 2012 at 21:12
  • I'm guessing you mean .push? Commented Oct 25, 2012 at 21:15

6 Answers 6

25

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

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

Here is the MDN documentation for map: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map. 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]});
  }
}
Sign up to request clarification or add additional context in comments.

Comments

2

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);

Comments

0

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/

Comments

0

You can achieve this by using the following for each loop where each key value pair will be pushed to an array.

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

1 Comment

Please add a bit explanation
0

So, I am learning coding and was trying to learn more about the map() implementation. I came across this and these answers made me think of a way to utilize map() to do what the OP originally wanted. Is it the best way to do it? I don't know, but it technically works similar to what I think OP's goal was and helped me to understand how map() works a bit better.

let anArray = [{thingy: 'thing', num: 1, bool: true, color: 'blue', numTwo: 3}];
let newArray = anArray.map((item) => {
    return item;
});

console.log(newArray) will display the same thing anArray displays.

Please let me know if I'm wrong!

Comments

0

This is not for this question, but can be related to similar questions: JavaScript Map with index:

nums.map((num, index) => num + index)
// [1, 1, 1] => [1, 2, 3]

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.