我有两个对象数组
id_0
我试图在对象数组中找到重复的key2
并合并array = [
{
"id_0":356,
"name":"India",
"key1":150,
"key2":200
},
{
"id_0":748,
"name":"Swaziland",
"key1":140,
"key2":180
}
]
的重复对象和值。
我希望结果是:
Math.abs()
如何找到重复值并合并数组中的重复键和值?
答案 0 :(得分:3)
试试这个fiddle
var array = [
{
"id_0":356,
"name":"India",
"key1":150
},
{
"id_0":356,
"name":"India",
"key2":200
},
{
"id_0":356,
"name2":"china",
"key2":200
}
]
function mergeArray( arr )
{
var outputObj = {};
for ( var counter = 0; counter < arr.length; counter++ )
{
var obj = arr[ counter ];
for( var key in obj )
{
if ( !outputObj[ key ] )
{
outputObj[ key ] = obj[ key ];
}
}
}
return outputObj;
}
console.log( mergeArray( array ) );
编辑fiddle以符合您的“更新”要求
var array = [
{
"id_0":356,
"name":"India",
"key1":150
},
{
"id_0":356,
"name":"India",
"key2":200
},
{
"id_0":400,
"name2":"china",
"key2":200
},
{
"id_0":400,
"name2":"china",
"key2":200
}
]
function mergeArray( arr )
{
var outputObj = {};
for ( var counter = 0; counter < arr.length; counter++ )
{
var obj = arr[ counter ];
for( var key in obj )
{
if ( !outputObj[ key ] )
{
outputObj[ key ] = obj[ key ];
}
}
}
return outputObj;
}
function collateArray( arr )
{
var outputObj = {};
var result = [];
for ( var counter = 0; counter < arr.length; counter++ )
{
var obj = arr[ counter ];
var id_0value = obj[ "id_0" ];
if ( !outputObj[ id_0value ] )
{
outputObj[ id_0value ] = [];
}
outputObj[ id_0value ].push( obj );
}
console.log( outputObj );
for ( var key in outputObj )
{
result.push( mergeArray( outputObj[ key ] ) );
}
return result;
}
console.log( collateArray( array ) );
答案 1 :(得分:1)
您可以根据需要使用Array.prototype.reduce()缩小阵列。
可以使用Object.assign()合并重复项目。
var array = [
{ 'id_0': 356, 'name': 'India', 'key1': 150 },
{ 'id_0': 356, 'name': 'India', 'key2': 200 }
];
var result = array.reduce(function(prev, item) {
var newItem = prev.find(function(i) {
return i.id_0 === item.id_0;
});
if (newItem) {
Object.assign(newItem, item);
} else {
prev.push(item);
}
return prev;
}, []);
console.log(result);
Object.assign是ES6的一部分。如果它不适合您,只需将其替换为:
for (var attrname in item) {
newItem[attrname] = item[attrname];
};
答案 2 :(得分:0)
使用临时Object
作为存储key => item
的地图可以使时间复杂度达到O(n)
:
var arr = [
{
"id_0":356,
"name":"India",
"key1":150
},
{
"id_0":356,
"name":"India",
"key2":200
},
{
"id_0":748,
"name":"Swaziland",
"key1":140
},
{
"id_0":748,
"name":"Swaziland",
"key2":180
}
];
// items: the array to be merged to unique.
// attrName: the attribute's name that is for distinguish,
// isOverwrite:decides whether the value will be overwrite by later ones or not. Default to false.
function getUnique(items, attrName, isOverwrite) {
// Map to keep reference to objects by its id.
var store = {};
// The array for output result.
var result = [];
isOverwrite = !!isOverwrite;
items.forEach(function(item) {
var id = item[attrName];
var key;
// Try to fetch item by id from store.
var target = store[id];
// If target item exist in store, its dulplicated, we need to merge it.
if (typeof target !== 'undefined') {
// If it's presented, we need to merge it into existing one.
for (key in item) {
if (!item.hasOwnProperty(key)) {
continue;
}
// If isOverwrite is true, always use the newest value, otherwise,
// we only apply values that are not exist in target yet.
if (isOverwrite || typeof target[key] === 'undefined') {
target[key] = item[key];
}
}
} else {
// If its not in store yet, put it a clone into result, and to map for
// later reference.
var clone = {};
for (key in item) {
if (item.hasOwnProperty(key)) {
clone[key] = item[key];
}
}
store[id] = clone;
// Also put it into the result array.
result.push(clone);
}
});
return result;
}
console.log(getUnique(arr, 'id_0'));
&#13;