2017-09-20 1 views
1

Ich habe ein Array von Objekten, mit Datum Saiten verkeilt, wie so angelegt:Zusammenführen von Objekten unterschiedlicher Längen

[ 
    { 
     '2017-08-21': {row: 0, artind: 25, qind: 20}, 
     '2017-08-22': {row: 1, artind: 5, qind: 12}, 
     '2017-08-23': {row: 11, artind: 3, qind: 0}, 
     '2017-08-24': {row: 45, artind: 25, qind: 43}, 
     '2017-08-25': {row: 13, artind: 0, qind: 27}, 
     '2017-08-26': {row: 2, artind: 2, qind: 2}, 
     '2017-08-27': {row: 1, artind: 12, qind: 27}, 
     '2017-08-28': {row: 19, artind: 0, qind: 0}, 
     '2017-08-29': {row: 11, artind: 25, qind: 7} 
    }, 
    { 
     '2017-08-24': {row: 1, artind: 34, qind: 8}, 
     '2017-08-25': {row: 0, artind: 5, qind: 3}, 
     '2017-08-26': {row: 7, artind: 22, qind: 0} 
    }, 
    { 
     '2017-08-22': {row: 55, artind: 5, qind: 2}, 
     '2017-08-23': {row: 13, artind: 25, qind: 0}, 
     '2017-08-24': {row: 1, artind: 0, qind: 0}, 
     '2017-08-25': {row: 6, artind: 8, qind: 0}, 
     '2017-08-26': {row: 0, artind: 12, qind: 89}, 
     '2017-08-27': {row: 11, artind: 29, qind: 5} 
    } 
] 

Ich brauche alle Werte in jedem Objekt zu summieren. Also am Ende, ich brauche ein Objekt, das wie so aussieht:

{ 
    '2017-08-21': {row: 0, artind: 25, qind: 20}, 
    '2017-08-22': {row: 56, artind: 10, qind: 14}, 
    '2017-08-23': {row: 24, artind: 28, qind: 0}, 
    '2017-08-24': {row: 47, artind: 59, qind: 51}, 
    '2017-08-25': {row: 19, artind: 13, qind: 30}, 
    '2017-08-26': {row: 9, artind: 36, qind: 91}, 
    '2017-08-27': {row: 12, artind: 41, qind: 32}, 
    '2017-08-28': {row: 19, artind: 0, qind: 0}, 
    '2017-08-29': {row: 11, artind: 25, qind: 7} 
} 

Die einzige Bibliothek für Unterstützung, die ich wirklich verwenden ist Underscore.js, so sind diese Antworten auf jeden Fall zu schätzen!

Dank

+0

Ihre Syntax ist ungültig. Ist Ihr erster Codeblock ein riesiges Objekt mit Zifferntasten? Oder ein Array von Arrays? ... oder etwas? – chazsolo

+0

@chazsolo behoben. Es ist ein Array von Objekten – bspckl

+0

Ein Array von Objekten, die mit einer Datumszeichenkette versehen sind? – chazsolo

Antwort

1

Sie können die unten reduzieren verwenden Methode, um Ihr Problem zu lösen.

const a = [{ 
    '2017-08-21': { row: 0, artind: 25, qind: 20 }, 
    '2017-08-22': { row: 1, artind: 5, qind: 12 }, 
    '2017-08-23': { row: 11, artind: 3, qind: 0 }, 
    '2017-08-24': { row: 45, artind: 25, qind: 43 }, 
    '2017-08-25': { row: 13, artind: 0, qind: 27 }, 
    '2017-08-26': { row: 2, artind: 2, qind: 2 }, 
    '2017-08-27': { row: 1, artind: 12, qind: 27 }, 
    '2017-08-28': { row: 19, artind: 0, qind: 0 }, 
    '2017-08-29': { row: 11, artind: 25, qind: 7 } 
}, 
{ 
    '2017-08-24': { row: 1, artind: 34, qind: 8 }, 
    '2017-08-25': { row: 0, artind: 5, qind: 3 }, 
    '2017-08-26': { row: 7, artind: 22, qind: 0 } 
}, 
{ 
    '2017-08-22': { row: 55, artind: 5, qind: 2 }, 
    '2017-08-23': { row: 13, artind: 25, qind: 0 }, 
    '2017-08-24': { row: 1, artind: 0, qind: 0 }, 
    '2017-08-25': { row: 6, artind: 8, qind: 0 }, 
    '2017-08-26': { row: 0, artind: 12, qind: 89 }, 
    '2017-08-27': { row: 11, artind: 29, qind: 5 } 
}]; 

a.reduce((result, item) => { 
    Object.keys(item).forEach((key) => { 
    if (result[key]) { 
     result[key] = { 
     row: result[key].row + item[key].row, 
     artind: result[key].artind + item[key].artind, 
     qind: result[key].qind + item[key].qind }; 
    } else { 
     result[key] = item[key]; 
    } 
    }); 
    return result; 
}, {}); 

EDIT: Just Formating

+0

aber ich stimme der obigen Lösung zu –

0

Ich bin mir nicht sicher, dass dies der beste Weg ist, aber das ist die Art, wie ich es tun würde.

Angenommen, die Variable detain enthält Ihre Daten und der Datenausgang ist Ihre Ausgabe.

var datain = [ 
    { 
    '2017-08-21': {row: 0, artind: 25, qind: 20}, 
    '2017-08-22': {row: 1, artind: 5, qind: 12}, 
    '2017-08-23': {row: 11, artind: 3, qind: 0}, 
    '2017-08-24': {row: 45, artind: 25, qind: 43}, 
    '2017-08-25': {row: 13, artind: 0, qind: 27}, 
    '2017-08-26': {row: 2, artind: 2, qind: 2}, 
    '2017-08-27': {row: 1, artind: 12, qind: 27}, 
    '2017-08-28': {row: 19, artind: 0, qind: 0}, 
    '2017-08-29': {row: 11, artind: 25, qind: 7} 
    }, 
    { 
    '2017-08-24': {row: 1, artind: 34, qind: 8}, 
    '2017-08-25': {row: 0, artind: 5, qind: 3}, 
    '2017-08-26': {row: 7, artind: 22, qind: 0} 
    }, 
    { 
    '2017-08-22': {row: 55, artind: 5, qind: 2}, 
    '2017-08-23': {row: 13, artind: 25, qind: 0}, 
    '2017-08-24': {row: 1, artind: 0, qind: 0}, 
    '2017-08-25': {row: 6, artind: 8, qind: 0}, 
    '2017-08-26': {row: 0, artind: 12, qind: 89}, 
    '2017-08-27': {row: 11, artind: 29, qind: 5} 
    } 
]; 

var dataout = { }; 

for(set=0; set<datain.length; set++) 
{ 
    for(date in datain[set]) 
    { 
     for(field in datain[set][date]) 
     { 
      if (typeof dataout[date] === 'undefined') { dataout[date] = { }; } 
      if (typeof dataout[date][field] === 'undefined') { dataout[date][field] = 0; } 
      dataout[date][field] += datain[set][date][field]; 
     } 
    } 
} 
console.log(data out); 

Dies gibt Ihnen wenig strukturelle Flexibilität, also hängt es wirklich davon ab, ob sich Ihre Daten ändern oder nicht.

0

Hier ist eine Lösung mit Lodash, die Underscore sehr ähnlich ist. Sie sollten in der Lage sein, die Anrufe sehr einfach zu übersetzen (oder verwenden Sie einfach Lodash, da es schneller und besser ist;))

Im Grunde, was Sie tun möchten, ist Ihre verschachtelten Objekte glätten, finden Sie eine Möglichkeit, Sie nach ihren Daten gruppieren können, und dann SumBy für die internen Werte.

Lassen Sie mich wissen, wenn einer der folgenden Code unklar ist.

let items = [ 
 
    { 
 
     '2017-08-21': {row: 0, artind: 25, qind: 20}, 
 
     '2017-08-22': {row: 1, artind: 5, qind: 12}, 
 
     '2017-08-23': {row: 11, artind: 3, qind: 0}, 
 
     '2017-08-24': {row: 45, artind: 25, qind: 43}, 
 
     '2017-08-25': {row: 13, artind: 0, qind: 27}, 
 
     '2017-08-26': {row: 2, artind: 2, qind: 2}, 
 
     '2017-08-27': {row: 1, artind: 12, qind: 27}, 
 
     '2017-08-28': {row: 19, artind: 0, qind: 0}, 
 
     '2017-08-29': {row: 11, artind: 25, qind: 7} 
 
    }, 
 
    { 
 
     '2017-08-24': {row: 1, artind: 34, qind: 8}, 
 
     '2017-08-25': {row: 0, artind: 5, qind: 3}, 
 
     '2017-08-26': {row: 7, artind: 22, qind: 0} 
 
    }, 
 
    { 
 
     '2017-08-22': {row: 55, artind: 5, qind: 2}, 
 
     '2017-08-23': {row: 13, artind: 25, qind: 0}, 
 
     '2017-08-24': {row: 1, artind: 0, qind: 0}, 
 
     '2017-08-25': {row: 6, artind: 8, qind: 0}, 
 
     '2017-08-26': {row: 0, artind: 12, qind: 89}, 
 
     '2017-08-27': {row: 11, artind: 29, qind: 5} 
 
    } 
 
] 
 

 
const groupedItems = _(items) 
 
    // get the nested objects, break them out, and give them date and value keys that we can use later 
 
    .map(item =>{ 
 
     const keys = Object.keys(item); 
 
     return _.map(keys, key => { 
 
     return { 
 
      date: key, // i.e. date: 2017-08-22 
 
      value: item[key] // i.e. value: {row: 1, artind: 5, qind: 12} 
 
     } 
 
     }) 
 
    }) 
 
    // flatten all the nested items into one array 
 
    .flatten() 
 
    // group by the date key we created earlier i.e. 2017-08-22 : [array with 2 entries] 
 
    .groupBy(item => item.date) 
 
    // take each of these dateItems, and internally sum their individual nested values, then return 
 
    .map(dateItem => { 
 
     const date = dateItem[0].date; // just grab the first date value, we'll use that as our final key 
 
     const row = _.sumBy(dateItem, innerItem => innerItem.value.row); 
 
     const artind = _.sumBy(dateItem, innerItem => innerItem.value.artind); 
 
     const qind = _.sumBy(dateItem, innerItem => innerItem.value.qind); 
 
     const returnObject = { 
 
     [date]: {row, artind, qind} 
 
     }; 
 
     return(returnObject); 
 
    }) 
 
    // call valueOf to execute the chain 
 
    .valueOf(); 
 

 

 
    console.log(groupedItems);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.4/lodash.js"></script>

0

Ich gehe nur in die Liste der Antworten hinzuzufügen. Nur dieses Snippet fügt Standardwerte hinzu, die gegen undefined geschützt sind.

var arr = [ 
 
    { 
 
     '2017-08-21': { artind: 25, qind: 20 }, 
 
     '2017-08-22': { row: 1, artind: 5, qind: 12 }, 
 
     '2017-08-23': { row: 11, artind: 3, qind: 0 }, 
 
     '2017-08-24': { row: 45, artind: 25, qind: 43 }, 
 
     '2017-08-25': { row: 13, artind: 0, qind: 27 }, 
 
     '2017-08-26': { row: 2, artind: 2, qind: 2 }, 
 
     '2017-08-27': { row: 1 }, 
 
     '2017-08-28': { row: 19, artind: 0, qind: 0 }, 
 
     '2017-08-29': { row: 11, artind: 25, qind: 7 } 
 
    }, 
 
    { 
 
     '2017-08-24': { row: 1, artind: 34, qind: 8 }, 
 
     '2017-08-25': { row: 0, artind: 5, qind: 3 }, 
 
     '2017-08-26': { row: 7, artind: 22, qind: 0 } 
 
    }, 
 
    { 
 
     '2017-08-22': { artind: 5, qind: 2 }, 
 
     '2017-08-23': { row: 13, artind: 25, qind: 0 }, 
 
     '2017-08-24': { row: 1}, 
 
     '2017-08-25': { row: 6, artind: 8, qind: 0 }, 
 
     '2017-08-26': { row: 0, artind: 12, qind: 89 }, 
 
     '2017-08-27': { row: 11, artind: 29, qind: 5 } 
 
    } 
 
]; 
 
var acc = _.reduce(arr, function (acc, elem) { 
 
      _.each(elem, function (value, key) { 
 
       _.defaults(value, {row: 0, artind:0 , qind: 0}); 
 
       if (_.has(acc, key)) { 
 
        acc[key].row += value.row; 
 
        acc[key].artind += value.artind; 
 
        acc[key].qind += value.qind; 
 
       } 
 
       else { 
 
        acc[key] = value; 
 
       } 
 
      }); 
 
      return acc; 
 
      }, {}); 
 
console.log(acc);
<script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.3/underscore-min.js"></script>

.

Verwandte Themen