From 50667dabc9bebae751b47961c705d6c52001a88e Mon Sep 17 00:00:00 2001 From: Oxyaction Date: Fri, 7 Apr 2017 20:27:50 +0300 Subject: [PATCH] fix merge data with sections closes #98 --- GiftedListView.js | 24 ++++++++++-------------- 1 file changed, 10 insertions(+), 14 deletions(-) diff --git a/GiftedListView.js b/GiftedListView.js index e80e80d..2ede614 100644 --- a/GiftedListView.js +++ b/GiftedListView.js @@ -4,7 +4,6 @@ var React = require('react'); var { ListView, - Platform, TouchableHighlight, View, Text, @@ -14,19 +13,16 @@ var { // small helper function which merged two objects into one -function MergeRecursive(obj1, obj2) { - for (var p in obj2) { - try { - if ( obj2[p].constructor==Object ) { - obj1[p] = MergeRecursive(obj1[p], obj2[p]); - } else { - obj1[p] = obj2[p]; - } - } catch(e) { - obj1[p] = obj2[p]; +function MergeRowsWithHeaders(obj1, obj2) { + const obj1Copy = { ...obj1 }; + Object.keys(obj2).forEach((key) => { + if (Array.isArray(obj2[key]) && Array.isArray(obj1Copy[key])) { + obj1Copy[key] = [...obj1Copy[key], ...obj2[key]]; + } else { + obj1Copy[key] = obj2[key]; } - } - return obj1; + }); + return obj1Copy; } var GiftedListView = React.createClass({ @@ -243,7 +239,7 @@ var GiftedListView = React.createClass({ this._setPage(this._getPage() + 1); var mergedRows = null; if (this.props.withSections === true) { - mergedRows = MergeRecursive(this._getRows(), rows); + mergedRows = MergeRowsWithHeaders(this._getRows(), rows); } else { mergedRows = this._getRows().concat(rows); }