2017-06-11 64 views
0
[{ 
    "_id": { 
     "year": 2017, 
     "month": 4 
    }, 
    "Confirm": 0 
}, { 
    "_id": { 
     "year": 2017, 
     "month": 4 
    }, 
    "Expired": 25 
}, { 
    "_id": { 
     "year": 2017, 
     "month": 4 
    }, 
    "Pending": 390 
}, { 
    "_id": { 
     "year": 2017, 
     "month": 5 
    }, 
    "Pending": 1400 
}] 

上面的數組包含相同的值月和年。從MongoDB Aggregate生成。我想將它們合併成一個單獨的對象,並保留它們所擁有的任何鍵和值。數組對象中的相同值的組副本

預期輸出:

[{ 
    month: 4, 
    year: 2017, 
    Expired: 25, 
    Pending: 390 
}, { 
    month: 5, 
    year: 2017, 
    Pending: 1400 
}] 

我喜歡最快的執行落實。 Underscorejs或土着是受歡迎的。謝謝

+0

你想如何合併它們?提供你的'期望的輸出' – Rowland

+0

有些鍵可以匹配而不是整個對象......它們是否也作爲一個對象合併? – zer00ne

+0

是@ zer00ne。我是否必須引入另一個變量 – Muhaimin

回答

0

您可以使用用於集合Map,然後Array.from提取最終的對象:

function merge(data) { 
 
    return Array.from(data.reduce((acc, o) => { 
 
     const k = o._id.year * 100 + o._id.month; 
 
     const v = acc.get(k) || Object.assign({}, o._id); 
 
     for (let prop in o) { 
 
      if (prop !== '_id') v[prop] = o[prop]; 
 
     }    
 
     return acc.set(k, v); 
 
    }, new Map), ([k, v]) => v); 
 
} 
 
// Sample data 
 
const data = [{ 
 
    "_id": { 
 
     "year": 2017, 
 
     "month": 4 
 
    }, 
 
    "Confirm": 0 
 
}, { 
 
    "_id": { 
 
     "year": 2017, 
 
     "month": 4 
 
    }, 
 
    "Expired": 25 
 
}, { 
 
    "_id": { 
 
     "year": 2017, 
 
     "month": 4 
 
    }, 
 
    "Pending": 390 
 
}, { 
 
    "_id": { 
 
     "year": 2017, 
 
     "month": 5 
 
    }, 
 
    "Pending": 1400 
 
}]; 
 

 
const result = merge(data); 
 

 
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

0

這運行在O(N * logN)排序和O(N)合併JSON。 希望這適合你!

var obj = [{ 
 
    _id: { 
 
     year: 2017, 
 
     month: 5, 
 
    }, 
 
    Pending: 1400, 
 
}, { 
 
    _id: { 
 
     year: 2017, 
 
     month: 4, 
 
    }, 
 
    Expired: 25, 
 
}, { 
 
    _id: { 
 
     year: 2017, 
 
     month: 4, 
 
    }, 
 
    Pending: 390, 
 
}, { 
 
    _id: { 
 
     year: 2017, 
 
     month: 4, 
 
    }, 
 
    Confirm: 0, 
 
}]; 
 

 
function compare(a, b) { 
 
    return a._id.year !== b._id.year 
 
     ? a._id.year - b._id.year 
 
     : a._id.month - b._id.month; 
 
} 
 

 
var sorted = obj.sort(compare); 
 

 
function join(a, b) { 
 
    return { 
 
     _id: a._id, 
 
     Pending: (a.Pending? a.Pending : 0) + (b.Pending? b.Pending : 0), 
 
     Confirm: (a.Confirm? a.Confirm : 0) + (b.Confirm? b.Confirm : 0), 
 
     Expired: (a.Expired? a.Expired : 0) + (b.Expired? b.Expired : 0), 
 
    }; 
 
} 
 

 
var compressed = sorted.filter(function (value, index) { 
 
    if (!sorted[index + 1]) { 
 
     return true; 
 
    } 
 
    if (compare(value, sorted[index + 1]) === 0) { 
 
     sorted[index + 1] = join(value, sorted[index + 1]); 
 
     return false; 
 
    } 
 
    return true; 
 
}); 
 

 
console.log(compressed); 
 

 
// if you want month and year formatted: 
 

 
console.log(compressed.map(function (o) { 
 
    const result = { 
 
     month: o._id.month, 
 
     year: o._id.year, 
 
    }; 
 
    if (o.Pending !== undefined) result.Pending = o.Pending; 
 
    if (o.Confirm !== undefined) result.Confirm = o.Confirm; 
 
    if (o.Expired !== undefined) result.Expired = o.Expired; 
 
    return result; 
 
}));

1

這需要一點點挑開,但它是線性的:

const ary = [{ 
    "_id": { 
     "year": 2017, 
     "month": 4 
    }, 
    "Confirm": 0 
}, { 
    "_id": { 
     "year": 2017, 
     "month": 4 
    }, 
    "Expired": 25 
}, { 
    "_id": { 
     "year": 2017, 
     "month": 4 
    }, 
    "Pending": 390 
}, { 
    "_id": { 
     "year": 2017, 
     "month": 5 
    }, 
    "Pending": 1400 
}]; 

const result = Object.values(ary.reduce((acc, cur) => { 
    const { month, year } = cur._id; 
    const key = `${month}-${year}`; 
    const obj = Object.assign({}, cur); 
    delete obj._id; 
    acc[key] = Object.assign(acc[key] || { month, year }, obj); 
    return acc; 
}, {})); 

console.log(result); 
+0

我認爲這隻適用於數組已經排序的 –

+0

@DanielPérez,當你使用密鑰時不需要排序。 – trincot

+0

我想這是沒有必要與mongodb查詢,在這種情況下,應該更新問題,添加mongodb標籤等 –

相關問題