1

I'm trying to take an array of many arrays that contain many objects and manipulate it into an array of objects.

So, let's say I have an array that looks like this:

[
    [
        {Name: 'Josh', email: 'josh@gmail.com', Points: 33},
        {Name: 'Doug', email: 'doug@gmail.com', Points: 12}
    ],
    [
        {Name: 'Josh', email: 'josh@gmail.com', Points: 11},
        {Name: 'Doug', email: 'doug@gmail.com', Points: 18}
    ],
    [
        {Name: 'Josh', email: 'josh@gmail.com', Points: 2},
        {Name: 'Doug', email: 'doug@gmail.com', Points: 27}
    ]
]​

The Desired outcome would be an array of objects that has a 'Points' property to hold an array of the points. So it would look like this:

[
    {Name: 'Josh', email: 'josh@gmail.com', Points: [33, 11, 2]},
    {Name: 'Doug', email: 'doug@gmail.com', Points: [12, 18, 27]}
]

This problem seems simple, but I can't seem to figure out the best way of doing this. If you want this data in a fiddle, I made this for you to play with: http://jsfiddle.net/Qhxzz/1/

4

2 回答 2

2

请注意,这取决于输入结构是静态的(这里没有递归)。假设您没有使用任何库:

var result = [];

for (var i = 0; i < initial.length; i++) {
    var innerArray = initial[i];

    for (var j = 0; j < innerArray.length; j++) {
        var item = innerArray[j];

        var found = false;
        // search result for an object with a matching email prop
        for (var k = 0; k < result.length; k++) {
            if (result[k].email === item.email) {
                found = true;

                // add this Point to the Points array
                result[k].Points.push(item.Points);
            }
        }

        if (!found) {
            // convert Points to an array of Points
            item.Points = [item.Points];

            result.push(item);
        }
    }
}

http://jsfiddle.net/Qhxzz/2/

于 2012-10-18T16:28:38.147 回答
2

.reduce()这是使用and的一种方法.forEach()(如果您支持旧版浏览器,则需要补丁。)

var consolidated = data.reduce(function(holder, arr) {
    arr.forEach(function(obj) {
        if (obj.email in holder.emails)
            holder.emails[obj.email].Points.push(obj.Points);
        else {
            holder.emails[obj.email] = obj;
            holder.result.push(obj);
            obj.Points = [obj.Points];
        }
    });
    return holder;
}, {emails:{},result:[]}).result;

实际上,我们可以使用 来展平原始 Array concat.apply(),并留下一个整体 Array 进行迭代。

flat = [].concat.apply([], data);

这会将您data变成这样的结构:

[{Name:"Josh", email:"josh@gmail.com", Points:33},
 {Name:"Doug", email:"doug@gmail.com", Points:12},
 {Name:"Josh", email:"josh@gmail.com", Points:11},
 {Name:"Doug", email:"doug@gmail.com", Points:18},
 {Name:"Josh", email:"josh@gmail.com", Points:2},
 {Name:"Doug", email:"doug@gmail.com", Points:27}]

并且通过消除对内部.forEach().

var consolidated = flat.reduce(function(holder, obj) {
    if (obj.email in holder.emails)
        holder.emails[obj.email].Points.push(obj.Points);
    else {
        holder.emails[obj.email] = obj;
        holder.result.push(obj);
        obj.Points = [obj.Points];
    }
    return holder;
}, {emails:{}, result:[]}).result;
于 2012-10-18T16:42:22.077 回答