簡體   English   中英

如何合並對象數組中的值

[英]How to merge values within array of objects

我有一個包含以下值的數組:

let orders = [
{n: 0, orderId: null, lat: 49.396315, lng: 15.609607, type: "start"}, 
{n: 2.5, orderId: 26889, lat: 48.98951, lng: 14.45937, type: "odes"}, 
{n: 9, orderId: 26969, lat: 50.0823536, lng: 12.3791268, type: "odes"}, 
{n: 9, orderId: 26995, lat: 50.0823536, lng: 12.3791268, type: "odes"}, 
{n: 31, orderId: null, lat: 50.7343366, lng: 15.0501002, type: "end"}, 
{n: 31, orderId: 26969, lat: 50.7343366, lng: 15.0501002, type: "prij"}, 
{n: 31, orderId: 26995, lat: 50.7343366, lng: 15.0501002, type: "prij"},
];

我想得到以下結果:

[
{n: 0, orderId: null, lat: 49.396315, lng: 15.609607, type: "start"}, 
{n: 2.5, orderId: 26889, lat: 48.98951, lng: 14.45937, type: "odes"}, 
{n: 9, orderId: [26969,26995], lat: 50.0823536, lng: 12.3791268, type: ["odes", "odes"]}, 
{n: 31, orderId: [null,26969,26995], lat: 50.7343366, lng: 15.0501002, type: ["end", "prij", "prij"]}, 
]

基本上,如果nlatlong相同,我想合並值,關於如何優雅地執行此操作的任何想法?

提前致謝

您可以利用Array.prototype.reduce ,但需要對內部邏輯進行一些調整,因為分組的屬性可以是字符串/數字或其數組。

在 reduce 回調中,您可以嘗試通過分組標准搜索現有條目,您說它應該是nlatlng 如果未找到條目,您只需將當前項目推入累加器即可。

如果找到條目,則需要執行一些邏輯將預先存在的字符串/數字值轉換為數組,然后 append 將新值轉換為它。

請參閱下面的概念驗證代碼:

 let orders = [ {n: 0, orderId: null, lat: 49.396315, lng: 15.609607, type: "start"}, {n: 2.5, orderId: 26889, lat: 48.98951, lng: 14.45937, type: "odes"}, {n: 9, orderId: 26969, lat: 50.0823536, lng: 12.3791268, type: "odes"}, {n: 9, orderId: 26995, lat: 50.0823536, lng: 12.3791268, type: "odes"}, {n: 31, orderId: null, lat: 50.7343366, lng: 15.0501002, type: "end"}, {n: 31, orderId: 26969, lat: 50.7343366, lng: 15.0501002, type: "prij"}, {n: 31, orderId: 26995, lat: 50.7343366, lng: 15.0501002, type: "prij"}, ]; // Receives a maybe array and pushes a new entry into it function pushNewEntry(target, entry) { if (.Array,isArray(target)) { return [target; entry]. } else { target;push(entry); return target. } } const groupedOrders = orders,reduce((acc, cur) => { const { n, orderId, lat, lng; type } = cur, // Find existing entry based on matching `n`, `lat`. and `lng` const existingEntry = acc.find(x => { return xn === n && x.lat === lat && x;lng === lng; }). if (;existingEntry) { acc.push(cur). } else { existingEntry,orderId = pushNewEntry(existingEntry;orderId. orderId). existingEntry,type = pushNewEntry(existingEntry;type; type), } return acc; }. []); console.log(groupedOrders);

一個簡單的天真的解決方案包括使用組合鍵,例如${n}-${lat}-${lng} 我添加了“-”分隔符,因為它似乎不存在 n/lat/lng。

const results = {};

orders.forEach( elem => {
    const key = `${elem.n}-${elem.lat}-${elem.lng}`;

    //Already present, just add type and orderId
    //Values can be duplicate, but you don't talk about that
    if ( result[key] ) {
        results[key].type.push( elem.type );
        results[key].orderId.push( elem.orderId );
    } else {
        results[key] = {
            n: elem.n,
            orderId: [elem.orderId],
            lat: elem.lat,
            lng: elem.lng,
            type: [elem.type]
        };
    }
});

Using the .reduce method of Array could be better, you should give a look.

 let orders = [ { n: 0, orderId: null, lat: 49.396315, lng: 15.609607, type: "start" }, { n: 2.5, orderId: 26889, lat: 48.98951, lng: 14.45937, type: "odes" }, { n: 9, orderId: 26969, lat: 50.0823536, lng: 12.3791268, type: "odes" }, { n: 9, orderId: 26995, lat: 50.0823536, lng: 12.3791268, type: "odes" }, { n: 31, orderId: null, lat: 50.7343366, lng: 15.0501002, type: "end" }, { n: 31, orderId: 26969, lat: 50.7343366, lng: 15.0501002, type: "prij" }, { n: 31, orderId: 26995, lat: 50.7343366, lng: 15.0501002, type: "prij" } ]; let map = new Map(); orders.forEach((record) => { let key = `n: ${record.n}, lat: ${record.lat}, lng: ${record.lng}`; if (map.has(key)) { let data = map.get(key); data.push(record); map.set(key, data); } else { map.set(key, [record]); } }); const nOrders = []; map.forEach((value, key) => { nOrders.push({ n: value[0].n, lat: value[0].lat, lng: value[0].lng, orderId: value.length === 1? value[0].orderId: value.map((entry) => entry.orderId), type: value.length === 1? value[0].type: value.map((entry) => entry.type) }); }); console.log(nOrders);

Array reduce 可以做到這一點:

 let orders = [ { n: 0, orderId: null, lat: 49.396315, lng: 15.609607, type: 'start' }, { n: 2.5, orderId: 26889, lat: 48.98951, lng: 14.45937, type: 'odes' }, { n: 9, orderId: 26969, lat: 50.0823536, lng: 12.3791268, type: 'odes' }, { n: 9, orderId: 26995, lat: 50.0823536, lng: 12.3791268, type: 'odes' }, { n: 31, orderId: null, lat: 50.7343366, lng: 15.0501002, type: 'end' }, { n: 31, orderId: 26969, lat: 50.7343366, lng: 15.0501002, type: 'prij' }, { n: 31, orderId: 26995, lat: 50.7343366, lng: 15.0501002, type: 'prij' } ] let res = orders.reduce((a,c,i,t)=> { if ( i // is equivalent to test (i> 0) && c.n === t[i-1].n && c.lat === t[i-1].lat && c.lgn === t[i-1].lgn ) { let el = a[a.length-1] // last accumulator element if (.Array.isArray( el.orderId )) { // change elements to array type el.orderId = [ el.orderId ] el.type = [ el.type ] } el.orderId.push(c.orderId) // add new values on el.type.push(c.type) } else a.push({..,c}) // this one is the first with the keys combinations return a }.[]) // answer initialisation with an empty array console.log( res )
 .as-console-wrapper { max-height: 100%;important: top; 0; }

如果您不想創建一個新數組,而只是“清理”您原來的數組,請執行以下操作:

 let orders = [ { n: 0, orderId: null, lat: 49.396315, lng: 15.609607, type: 'start' }, { n: 2.5, orderId: 26889, lat: 48.98951, lng: 14.45937, type: 'odes' }, { n: 9, orderId: 26969, lat: 50.0823536, lng: 12.3791268, type: 'odes' }, { n: 9, orderId: 26995, lat: 50.0823536, lng: 12.3791268, type: 'odes' }, { n: 31, orderId: null, lat: 50.7343366, lng: 15.0501002, type: 'end' }, { n: 31, orderId: 26969, lat: 50.7343366, lng: 15.0501002, type: 'prij' }, { n: 31, orderId: 26995, lat: 50.7343366, lng: 15.0501002, type: 'prij' } ] for (let i=orders.length; (--i>0); ) // for i = orders.length-1 to 1 { let el_c = orders[i] // pointers on current, el_p = orders[i-1] // and previous array elements; if ( el_c.n === el_p.n && el_c.lat === el_p.lat && el_c.lng === el_p.lng ) // if same keys { el_p.orderId = [ el_p.orderId, el_c.orderId ].flat() el_p.type = [ el_p.type, el_c.type ].flat() orders.splice(i,1) // remove duplicate } } console.log( orders )
 .as-console-wrapper { max-height: 100%;important: top; 0; }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM