簡體   English   中英

在javascript中合並對象

[英]merging objects in javascript

我有2個對象數組如下:

arr1 =  [ { 'v1': 'abcde',
    'pv_45': 13018, 
    'geolocation': '17.340291,76.842807'
   }]

arr2 =[{ 'v1':'abcde',
    'pv_50': 13010,    
    geolocation: '17.340291,76.842807'
    }]

我想based on condition that 'v1' and 'geolocation' should be same如下的based on condition that 'v1' and 'geolocation' should be same合並上面的2數組int0單:

[{'v1':'abcde',
      'pv_45': 13018, 
      'pv_50': 13010,
      'geolocation': '17.340291,76.842807'}]

我使用_.extend,但它沒有檢查任何條件,它會合並。 請分享您的想法。 提前致謝。

你可以使用下划線js union and uniq來做到這一點。

var mergedArray = _.uniq(_.union(c1, c2), false, function(item, key, a){ return item; });

您可以執行以下操作:

var arr3 = [].concat.apply([], arr1, arr2);
var temp =_.groupBy(arr3, 'geolocation');
var result = Object.keys(_.groupBy(arr3, 'geolocation')).map(function(x)  {  return _.extend.apply(0, p[x]); })

如果你喜歡ES-6箭頭功能, result就變成了

Object.keys(_.groupBy(arr3, 'geolocation')).map((x) => _.extend.apply(0, p[x]);)

使用純JavaScript可以這樣做:

var arr1 =  [ { 'v1': 'abcde',
    'pv_45': 13018, 
    'geolocation': '17.340291,76.842807'
  }],
  arr2 =[{ 'v1':'abcde',
    'pv_50': 13010,    
    geolocation: '17.340291,76.842807'
  }],
  mergeOnV1Geo = function (arr1, arr2) {
    var mergeObj = {},
      merge = function (item) {
        var key = item.v1 + ',' + item.geolocation;
        // if this is the first object with this key
        // create a new object and copy v1, geolocation info
        if (!mergeObj[key]) {
          mergeObj[key] = {
            v1: item.v1,
            geolocation: item.geolocation
          };
        }
        // add other props
        Object.keys(item).forEach(function (prop) {
          if (!prop.match(/v1|geolocation/)) {
            mergeObj[key][prop] = item[prop];
          }
        });
      };
    arr1.forEach(merge);
    arr2.forEach(merge);

    // map back into an array
    return Object.keys(mergeObj).map(function (key) {
      return mergeObj[key];
    });

  };

mergeOnV1Geo(arr1, arr2);

ES6:使用spread operatorreduce

 arr1 = [{ v1: 'abcde', pv_45: 13018, geolocation: '17.340291,76.842807' }] arr2 =[{ v1:'abcde', pv_50: 13010, geolocation: '17.340291,76.842807' }] // Keys to be grouped ie whose values should be equal groupableKeys = ['v1', 'geolocation']; // Reducer that creates an Object with Key as the // groupable key values value1::value2 and Value as // the list of objects whose v1 and geolocation are same groupableReducer = (a, b) => { const uniqKey = groupableKeys.map(key => b[key]).join("::"); a[uniqKey] = [...(a[uniqKey] || []), b]; return a; } // Merges two objects using the spread operator mergableReducer = (a, b) => ({...a, ...b}) // Merge two arrays and start processing groupableKeyObject = [...arr1, ...arr2].reduce(groupableReducer, {}) output = Object.keys(groupableKeyObject) .map(key => groupableKeyObject[key].reduce(mergableReducer, {}) ) console.log(output); 

暫無
暫無

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

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