Merging two arrays of objects
By : sunshine21
Date : March 29 2020, 07:55 AM
hope this fix your issue You can achieve this using jQuery extend if that's an option. code :
var extendedArray = $.extend({}, arr1, arr2);
$.extend(arr1, arr2);
|
JavaScript Merging 3 Arrays of Objects into 1 Array of Objects by ID and Name
By : Nelson Rodriguez Ros
Date : March 29 2020, 07:55 AM
like below fixes the issue There are a number of ways you could do this. Here is a very old-school but fast way to do it. It involves two steps: Take all the arrays and merge them into an object using the specified key ('Client ID' in this example). Take that merged object and convert it back to an array. code :
var arr1 = [
{"Client ID":"1", "Client Name":"ABC", D1:"some data", D2:"more data"},
{"Client ID":"2", "Client Name":"DEF", D1:"some data", D2:"more data"},
{"Client ID":"3", "Client Name":"GHI", D1:"some data", D2:"more data"}
];
var arr2 = [
{"Client ID":"1", "Client Name":"ABC", D3:"and more data", D4:"more more data"},
{"Client ID":"2", "Client Name":"DEF", D3:"and more data", D4:"more more data"},
{"Client ID":"3", "Client Name":"GHI", D3:"and more data", D4:"more more data"}
];
var arr3 = [
{"Client ID":"1", "Client Name":"ABC", D5:"other data", D6:"extra Data"},
{"Client ID":"2", "Client Name":"DEF", D5:"other data", D6:"extra Data"},
{"Client ID":"3", "Client Name":"GHI", D5:"other data", D6:"extra Data"}
];
console.log( mergeArrays( 'Client ID', [ arr1, arr2, arr3 ] ) );
function mergeArrays( key, arrays ) {
// First merge the arrays into an object
// indexed by the specified key
var merged = {};
arrays.forEach( function( array ) {
array.forEach( function( item ) {
var id = item[key];
var target = merged[id];
if( ! target ) target = merged[id] = {};
for( var name in item ) {
if( item.hasOwnProperty(name) ) {
target[name] = item[name];
}
}
});
});
// Now convert the merged object back to an array
var result = [];
for( var id in merged ) {
if( merged.hasOwnProperty(id) ) {
result.push( merged[id] );
}
}
return result;
}
|
Transform multiple arrays of objects into a single array while merging the objects on common key
By : Caio Cesar
Date : March 29 2020, 07:55 AM
|
Merging 2 arrays of objects to create a Map of Objects
By : ben leppard
Date : March 29 2020, 07:55 AM
help you fix your problem You could iterate the outer object's keys and then the inner arrays. If an result object does not exist, create one with the wanted keys and zero values.
|
Merging two arrays of structures using iOS swift
By : D shokry
Date : March 29 2020, 07:55 AM
This might help you How can I merge two (first and second) arrays of structures based on a key in structure (name). While merging I need to replace the existing element of first array with second array element, if any value changed in that element. code :
import Foundation
struct Example: Codable {
var name: String
var dob: String
var address: String
}
var first: [Example] = []
var second: [Example] = []
first.append(Example(name: "Arun", dob: "01-01-1994", address: "Tirupati"))
first.append(Example(name: "Balaji", dob: "01-01-1994", address: "Tirupati"))
first.append(Example(name: "Prasanth", dob: "01-01-1994", address: "Tirupati"))
second.append(Example(name: "Arun", dob: "01-01-1994", address: "Kadapa"))
second.append(Example(name: "Balaji", dob: "01-01-1994", address: "Tirupati"))
second.append(Example(name: "Prasanth", dob: "01-01-1994", address: "Tirupati"))
second.append(Example(name: "Harsha", dob: "01-01-1994", address: "Tirupati"))
first = second + first.filter { element in
return !second.contains { $0.name == element.name }
}
|