Javascript 创建联系人数据数组的已清理联系人对象

Javascript 创建联系人数据数组的已清理联系人对象,javascript,arrays,object,lodash,Javascript,Arrays,Object,Lodash,我有一个包含如下对象的数组(简化版,实际数组包含更多数据): 如何使用javascript(lodash)创建以下内容: { id: '1', name: 'Joe', description: 'Farmer', locations: [ { type: 'home', value: '123' }, { type: 'home', value: '456' } ] } 目标是创建

我有一个包含如下对象的数组(简化版,实际数组包含更多数据):

如何使用javascript(lodash)创建以下内容:

{
    id: '1',
    name: 'Joe',
    description: 'Farmer',
    locations: [ {
        type: 'home',
        value: '123'
    }, {
        type: 'home',
        value: '456'
    } ]
}
目标是创建联系人数据数组的已清理联系人对象。因此,如果名称和描述为空,则需要替换它们。如果在de locations数组中找不到位置,则推送类似位置的对象。我确实使用u.merge得到了一些结果,问题是merge总是用最后一个值覆盖,并且不推送新值,所以结果如下:

{
    id: '1',
    name: 'Joe',
    description: '',
    locations: [ {
        type: 'home',
        value: '123'
    }]
}
// this will get you the items (from server/localstorage/whatever)
var items = getItems();

// group items by id (assuming that's our grouping denominator
var groupedItems = _.groupBy(items, 'id');

// then iterate through each item in group, and reduce them
var results = _.map(_.values(groupdItems), function(item) {
    var reduced =  _.reduce(item, function(result, num, key) {
        if (key === 'locations') {
           // join everything together, we'll unique them later
           result.locations = (result.locations || []).concat(num || []);  
        }
        else {
           // override all previous values for this key.
           // change this to improve behavior
           result[key] = num;
        }
        return result;
    }, {});
    // reduce locations to unique locations only
    // currently implemented by using JSON.stringified but could easily change
    reduced.locations = _.uniq(reduced.locations, function(loc) {
      return JSON.stringify(loc);
    });
    return reduced;
});

// results should now contains the transformed items.

关于如何处理冲突,您有很多未解决的问题

  • 最后一个属性是否应覆盖上一个属性?是否仅通过
    位置加入
  • 连接是否仅由
    id
    完成
  • 如何确定
    位置的唯一性?它是由
    类型
    还是组合而成
一旦您对这些问题有了答案,您可以使用以下内容:

{
    id: '1',
    name: 'Joe',
    description: '',
    locations: [ {
        type: 'home',
        value: '123'
    }]
}
// this will get you the items (from server/localstorage/whatever)
var items = getItems();

// group items by id (assuming that's our grouping denominator
var groupedItems = _.groupBy(items, 'id');

// then iterate through each item in group, and reduce them
var results = _.map(_.values(groupdItems), function(item) {
    var reduced =  _.reduce(item, function(result, num, key) {
        if (key === 'locations') {
           // join everything together, we'll unique them later
           result.locations = (result.locations || []).concat(num || []);  
        }
        else {
           // override all previous values for this key.
           // change this to improve behavior
           result[key] = num;
        }
        return result;
    }, {});
    // reduce locations to unique locations only
    // currently implemented by using JSON.stringified but could easily change
    reduced.locations = _.uniq(reduced.locations, function(loc) {
      return JSON.stringify(loc);
    });
    return reduced;
});

// results should now contains the transformed items.

也许这就足够了:

function customMerge(obj1, obj2) {
    for(var i in obj2) {
        if(typeof(obj1[i]) === 'object' && obj1[i].push) {
            for(var j in obj2[i]) {
                obj1[i].push(obj2[i][j]);
            }
            obj1[i] = arrayUnique(obj1[i]);
        } else {
            obj1[i] = obj2[i];
        }
    }
}

function arrayUnique(list, byKey) {
    var newList = [];
    var map = {};
    for(var i in list) {
        var data = list[i];
        var id = list[i].id || JSON.stringify(data);
        if(typeof(map[id]) === 'undefined') {
            map[id] = data;
        } else {
            customMerge(map[id], data);
        }
    }
    for(var i in map) {
        newList.push(map[i]);
    }
    return newList;
}
这将返回您期望的结果:

arrayUnique([
    {
        id: '1',
        name: 'Joe',
        description: 'Student',
        locations: [ {
            type: 'home',
            value: '123'
        } ]
    },
    {
        id: '1',
        name: 'Joe',
        description: 'Farmer',
        locations: [ {
            type: 'home',
            value: '456'
        } ]
    },
    {
        id: '1',
        name: 'Joe',
        description: '',
        locations: [ {
            type: 'home',
            value: '123'
        } ]
    }
])

你如何确定描述应该是“农民”而不是“学生”?这是我需要做出的牺牲,没有办法表明优先顺序。