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

Create a cleaned up contact object of an array of contact data

本文关键字:联系人 对象 数组 数据 一个 创建      更新时间:2023-09-26

我有一个数组,里面有这样的对象(简单的版本,实际的数组有更多的数据):

[
    {
        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'
        } ]
    }
]

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

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

目标是创建一个联系人数据数组的已清理联系人对象。因此,它需要替换名称和描述等单个属性,以防它们为空。并推送位置之类的对象,以防在解位置数组中找不到它。我确实使用_.merge得到了一些结果,问题是merge总是覆盖最后一个值,而不推送新值,所以结果会是这样的:

{
    id: '1',
    name: 'Joe',
    description: '',
    locations: [ {
        type: 'home',
        value: '123'
    }]
}

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

  • 最后一个属性是否应该覆盖前一个属性?你只想通过locations加入吗
  • 连接是否仅由id完成
  • 如何确定locations中的唯一性?是通过typevalue还是组合

一旦你对这些问题有了答案,你就可以使用这样的东西:

// 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'
        } ]
    }
])