I struggled with a problem for more than an hour, how can I turn this nested array
[
[
{
"name": "1",
}
],
[
{
"name": "a",
},
{
"name": "b",
}
]
]
into this:
[
{
name: '1',
},
{
id: 'a-b',
grouped: [
{
name: 'a',
},
{
name: 'b',
},
],
},
]
I don't mind using lodash. Not sure should I flatten it before anything else would make things easier.
You could use map() to form the id and grab the parts needed to reconstruct the new array.
const data = [
[{
"name": "1",
}],
[{
"name": "a",
},
{
"name": "b",
}
]
];
const result = [
...data[0],
{
id: data[1].map(r => r.name).join("-"),
grouped: data[1]
}
];
console.log(result);
to flatten the array is a good start. That will remove the superfluous dimension from the rawArray:
const newArray = array.flat()
Now you have an array with three simple objects. The first will remain unchanged. The second element of your finalArray needs to be an object, so let's create it:
const obj = {}
the obj has two keys: id and grouped. The property of id is a string that we can create like this:
obj.id = newArray[1].name + "-" + newArray[2].name
the property of grouped remains the same:
obj.grouped = array[1]
so the finalArray is now straight forward:
const finalArray = [ newArray[0], obj ]
Put it all together in a function:
const rawArray1 = [
[
{
"name": "1a",
}
],
[
{
"name": "a",
},
{
"name": "b",
}
]
]
const rawArray2 = [
[
{
"name": "1b",
}
],
[
{
"name": "aa",
},
{
"name": "bb",
}
]
]
transformArray( rawArray1 )
transformArray( rawArray2 )
function transformArray( array ){
const newArray = array.flat()
const obj = {}
obj.id = newArray[1].name + "-" + newArray[2].name
obj.grouped = array[1]
const finalArray = [ newArray[0], obj ]
console.log(finalArray)
return finalArray
}
I managed to solve it using simple forEach, push, and flat. It's more simple than I thought, I was confused and stuck with map and reduce.
let result = [];
[
[{
"name": "1",
}],
[{
"name": "a",
},
{
"name": "b",
}
]
].forEach((val) => {
const [{
name
}] = val
if (val.length === 1) {
result.push({
name,
})
} else if (val.length > 1) {
result.push({
id: val.map(val2 => val2.name).join('-'),
grouped: val
})
}
})
console.log(result.flat())
const array1 = [
[{ name: "1" }],
[
{ name: "a" },
{ name: "b" }
]
]
const array2 = [
[{ name: "2" }],
[
{ name: "aa" },
{ name: "bb" },
{ name: "cc" }
]
]
transformArray( array1 )
transformArray( array2 )
function transformArray( array ){
const result = []
// destructure first array element for the first object:
const [ nameObj ] = array[0]
result.push( nameObj )
// map each object of the second array element into an
// an array of names, and then join the names together:
const dataObj = {}
dataObj.id = array[1].map(obj => obj.name).join('-')
dataObj.grouped = array[1]
result.push( dataObj )
console.log( result )
return result
}
I need to create every combination of objects possible out of an array with types/identifiers. E.g.:
Input:
let inputArray =
[
{
'type': '1',
'values': [
'val1',
'val2'
]
},
{
'type': '2',
'values': [
'val1',
'val2',
'val3'
]
}
]
Output:
let outputArray =
[
[
{
'type': '1',
'value': 'val1'
},
{
'type': '2',
'value': 'val1'
}
],
[
{
'type': '1',
'value': 'val1'
},
{
'type': '2',
'value': 'val2'
}
],
[
{
'type': '1',
'value': 'val1'
},
{
'type': '2',
'value': 'val3'
}
],
[
{
'type': '1',
'value': 'val2'
},
{
'type': '2',
'value': 'val1'
}
],
[
{
'type': '1',
'value': 'val2'
},
{
'type': '2',
'value': 'val2'
}
],
[
{
'type': '1',
'value': 'val2'
},
{
'type': '2',
'value': 'val3'
}
],
]
And it needs to work dynamically depending on the number of types, and values inside the input array. I.E. the input array has another object, where that objects values array has 3 or 4 elements...
What I tried:
function doTheThing(inputArray) {
let outputArray = []
for (let element of inputArray) {
for (let value of element.values) {
outputArray.push({ type: element.type, value: value })
}
}
}
Which gives me every value possible, but doesn't give me every combination that I need...
You can create recursive function using simple for loop that exists when the value of n is larger then the length of the data or continues to increment it and then runs for loop.
let data = [{"type":"1","values":["val1","val2"]},{"type":"2","values":["val1","val2","val3"]}]
function f(data, n = 0, prev = []) {
const result = [];
if (n > data.length - 1) {
result.push(prev.slice())
return result;
}
const { type, values } = data[n];
for (let i = 0; i < values.length; i++) {
prev[n] = { type, value: values[i] }
result.push(...f(data, n + 1, prev))
}
return result;
}
const result = f(data);
console.log(JSON.stringify(result, 0, 4))
Or you could use the reduce method instead of the for loop and make recursive call as long as the n < data.length - 1
let data = [{"type":"1","values":["val1","val2"]},{"type":"2","values":["val1","val2","val3"]}]
function f(data, n = 0, prev = []) {
return data[n].values.reduce((r, value) => {
prev[n] = { type: data[n].type, value }
if (n === data.length - 1) {
r.push(prev.slice())
}
if (n < data.length - 1) {
r.push(...f(data, n + 1, prev))
}
return r;
}, [])
}
const result = f(data);
console.log(JSON.stringify(result, 0, 4))
This function can help us to extend a permutation:
let permute = (iterable, arr) => {
let vals = [ ...iterable ];
let result = [];
for (let v of arr)
for (let items of vals)
result.push([ v, ...items ]);
return result;
};
For example if we want to permute [ 'a', 'b' ], [ 1, 2 ], and [ '?', '!' ] we can do:
let permute = (iterable, arr) => {
let vals = [ ...iterable ];
let result = [];
for (let v of arr)
for (let items of vals)
result.push([ v, ...items ]);
return result;
};
let v1 = permute([ [] ], [ 1, 2 ]);
let v2 = permute(v1, [ 'a', 'b' ]);
let v3 = permute(v2, [ '?', '!' ]);
console.log(JSON.stringify({ v1, v2, v3 }));
You can use this function with inputArray after performing a basic transformation on it (to achieve the type of result you posted in your question). For an entry in inputArray like { type: '1', values: [ 'v1', 'v2' ] }, you need to convert that to a form like: [ { type: '1', value: 'v1' }, { type: '1', value: 'v2' } ].
// Your original data
let inputArray = [{'type': '1','values': ['val1','val2']},{'type': '2','values': ['val1','val2','val3']}];
// Function to format data as described
let format = arr => arr.map(({ type, values }) => values.map(value => ({ type, value })));
// The same `permute` function as before
let permute = (iterable, arr) => { let vals = [ ...iterable ]; let result = []; for (let v of arr) for (let items of vals) result.push([ v, ...items ]); return result; };
let formatted = format(inputArray);
let v1 = permute([ [] ], formatted[0]);
let v2 = permute(v1, formatted[1]);
console.log(JSON.stringify(v2, null, 2));
Having input like the below:
[
{
gameId: id_0,
groups: [1]
},
{
gameId: id_1,
groups: [2]
},
{
gameId: id_2,
groups: [1, 2]
},
{
gameId: id_3,
groups: [3]
}
]
I would like my reduce to result in an array of objects like:
[
{
group: 1,
data: [
id_0, id_2 // gameId
]
},
{
group: 2,
data: [
id_1, id_2
]
},
{
group: 3,
data: [
id_3
]
}
]
I was able to partially solve this by utilising array indexes.
The code I have currently is:
groupByArr = parameter => data => data.reduce((acc, curr) => {
curr[parameter].forEach(key => {
if (acc[key]) {
acc[key].push(curr)
} else {
acc[key] = [curr]
}
})
return acc
}, [])
which produces an array of arrays where main array index is the group id:
[
empty,
1: [
id_0, id_2
],
2: [
id_1, id_2
],
3: [
id_3
]
]
You can use Array.prototype.reduce() combined with Array.prototype.forEach() and Array.prototype.push() to return an Object and finally get the values with Object.values()
Code:
const data = [{gameId: 'id_0',groups: [1]},{gameId: 'id_1',groups: [2]},{gameId: 'id_2',groups: [1, 2]},{gameId: 'id_3',groups: [3]}]
const result = Object.values(data.reduce((acc, {gameId, groups}) => {
groups.forEach(group => {
acc[group] = acc[group] || { group, data: [] }
acc[group].data.push(gameId)
})
return acc
}, {}))
console.log(result)
.as-console-wrapper { max-height: 100% !important; top: 0; }
Let arr be the variable containing data in following format:
[
empty,
1: [
id_0, id_2
],
2: [
id_1, id_2
],
3: [
id_3
]
]
Then following code might do:-
for(let i=1;i<arr.length;i++){
arr[i-1] = {group:i,data:arr[i]};
}
arr[arr.length-1] = undefined; // To Prevent Unnecessary Errors
delete arr[arr.length-1]; // To Prevent Unnecessary Errors
arr.length -= -1;
Now arr will be in following format:-
[
{
group: 1,
data: [
id_0, id_2
]
},
{
group: 2,
data: [
id_1, id_2
]
},
{
group: 3,
data: [
id_3
]
}
]
Hope it helps. Tell me if I Misunderstood your question.
Use an object as acc, then add objects to it:
if (acc[key]) {
acc[key].data.push(curr)
} else {
acc[key] = { group: key, data: [curr] };
}
And finally, turn the returned object into an array:
const result = Object.values(hash);
Try this
const data = [
{gameId: 'id_0',groups: [1]},
{gameId: 'id_1',groups: [2]},
{gameId: 'id_2',groups: [1, 2]},
{gameId: 'id_3',groups: [3]}
]
const single_id = data.filter(i => i.groups.length === 1)
const multiple_ids = data.filter(i => i.groups.length > 1)
let res = [];
single_id.map(i => {
let data = {group: i.groups[0], data: [i.gameId]}
multiple_ids.forEach(o => o.groups.includes(i.groups[0]) && (data.data.push(o.gameId)))
res.push(data)
})
console.log(res)
`let b = [
{
gameId: "id_0",
groups: [1]
},
{
gameId: "id_1",
groups: [2]
},
{
gameId: "id_2",
groups: [1, 2]
},
{
gameId: "id_3",
groups: [3]
}
];
let d = new Map(); ;
b.forEach(function(a){
a.groups.forEach(function(a1){
if(d.get(a1)){
d.get(a1).push(a.gameId);
}else{
d.set(a1, [a.gameId]);
}
});
});`
You should try this one. Using reduce in grouped scenarios is the best thing JavaScript provides us
let arr = [
{
gameId: "id_0",
groups: [1]
},
{
gameId: "id_1",
groups: [2]
},
{
gameId: "id_2",
groups: [1, 2]
},
{
gameId: "id_3",
groups: [3]
}
];
const grouped = arr.reduce((acc, current) => {
for(let x of current.groups){
if(!acc[x]) {
acc[x] = {group: x, data:[current.gameId]}
} else {
acc[x].data.push(current.gameId)
}
}
return acc
},{});
console.log(Object.values(grouped))
I would like to break a array with users:
[
{name: "Carlos"},
{name: "Marcos"},
{name: "Fernando"},
{name: "Jhon"},
{name: "Loius"},
{name: "Jacob"},
]
And get something like this:
[
[
{name: "Jhon"},
{name: "Loius"},
{name: "Jacob"},
],
[
{name: "Carlos"},
{name: "Marcos"},
{name: "Fernando"},
]
]
The criterion for splitting them is that I want that each sub array to have a maximum of 3 users, but the number of sub arrays can be unlimited.
function splitIntoParts(input, maxElementsPerPart) {
const inputClone = [...input]; // create a copy because splice modifies the original array reference.
const result = [];
const parts = Math.ceil(input.length/maxElementsPerPart);
for(let i = 0; i < parts; i++) {
result.push(inputClone.splice(0, maxElementsPerPart));
}
return result;
}
console.log(splitIntoParts([
{name: "Carlos"},
{name: "Marcos"},
{name: "Fernando"},
{name: "Jhon"},
{name: "Loius"},
{name: "Jacob"},
{name: "Simon"},
], 3));
chunk is elegantly expressed using functional style
const chunk = (xs = [], n = 1) =>
xs.length <= n
? [ xs ]
: [ xs.slice (0, n) ] .concat (chunk (xs.slice (n), n))
const data =
[ 1, 2, 3, 4, 5, 6 ]
console.log (chunk (data, 1))
// [ [ 1 ], [ 2 ], [ 3 ], [ 4 ], [ 5 ], [ 6 ] ]
console.log (chunk (data, 2))
// [ [ 1, 2 ], [ 3, 4 ], [ 5, 6 ] ]
console.log (chunk (data, 3))
// [ [ 1, 2, 3 ], [ 4, 5, 6 ] ]
console.log (chunk (data, 4))
// [ [ 1, 2, 3, 4 ], [ 5, 6 ] ]
console.log (chunk ())
// [ [ ] ]
I think take and drop abstractions make the function read a littler nicer. Your opinion may vary.
const take = (xs = [], n = 1) =>
xs.slice (0, n)
const drop = (xs = [], n = 1) =>
xs.slice (n)
const chunk = (xs = [], n = 1) =>
xs.length <= n
? [ xs ]
: [ take (xs, n) ] .concat (chunk (drop (xs, n), n))
let data = [
{name: "Carlos"},
{name: "Marcos"},
{name: "Fernando"},
{name: "Jhon"},
{name: "Loius"},
{name: "Jacob"},
]
function toGroupsOf(n, array){
return Array.from(
{length: Math.ceil(array.length/n)}, //how many groups
(_,i) => array.slice(i*n, (i+1)*n) //get the items for this group
)
}
console.log(toGroupsOf(3, data));
.as-console-wrapper{top:0;max-height:100%!important}
or
function toGroupsOf(n, array){
var groups = Array(Math.ceil(array.length/n));
for(var i=0; i<groups.length; ++i)
groups[i] = array.slice(i*n, (i+1)*n);
return groups;
}
I have two arrays:
Array 1:
[
{ id: "abdc4051", date: "2017-01-24" },
{ id: "abdc4052", date: "2017-01-22" }
]
and array 2:
[
{ id: "abdc4051", name: "ab" },
{ id: "abdc4052", name: "abc" }
]
I need to merge these two arrays based on id and get this:
[
{ id: "abdc4051", date: "2017-01-24", name: "ab" },
{ id: "abdc4052", date: "2017-01-22", name: "abc" }
]
How can I do this without iterating trough Object.keys?
You can do it like this -
let arr1 = [
{ id: "abdc4051", date: "2017-01-24" },
{ id: "abdc4052", date: "2017-01-22" }
];
let arr2 = [
{ id: "abdc4051", name: "ab" },
{ id: "abdc4052", name: "abc" }
];
let arr3 = arr1.map((item, i) => Object.assign({}, item, arr2[i]));
console.log(arr3);
Use below code if arr1 and arr2 are in a different order:
let arr1 = [
{ id: "abdc4051", date: "2017-01-24" },
{ id: "abdc4052", date: "2017-01-22" }
];
let arr2 = [
{ id: "abdc4051", name: "ab" },
{ id: "abdc4052", name: "abc" }
];
let merged = [];
for(let i=0; i<arr1.length; i++) {
merged.push({
...arr1[i],
...(arr2.find((itmInner) => itmInner.id === arr1[i].id))}
);
}
console.log(merged);
Use this if arr1 and arr2 are in a same order
let arr1 = [
{ id: "abdc4051", date: "2017-01-24" },
{ id: "abdc4052", date: "2017-01-22" }
];
let arr2 = [
{ id: "abdc4051", name: "ab" },
{ id: "abdc4052", name: "abc" }
];
let merged = [];
for(let i=0; i<arr1.length; i++) {
merged.push({
...arr1[i],
...arr2[i]
});
}
console.log(merged);
You can do this in one line
let arr1 = [
{ id: "abdc4051", date: "2017-01-24" },
{ id: "abdc4052", date: "2017-01-22" }
];
let arr2 = [
{ id: "abdc4051", name: "ab" },
{ id: "abdc4052", name: "abc" }
];
const mergeById = (a1, a2) =>
a1.map(itm => ({
...a2.find((item) => (item.id === itm.id) && item),
...itm
}));
console.log(mergeById(arr1, arr2));
Map over array1
Search through array2 for array1.id
If you find it ...spread the result of array2 into array1
The final array will only contain id's that match from both arrays
This solution is applicable even when the merged arrays have different sizes.
Also, even if the matching keys have different names.
Merge the two arrays by using a Map as follows:
const arr1 = [
{ id: "abdc4051", date: "2017-01-24" },
{ id: "abdc4052", date: "2017-01-22" },
{ id: "abdc4053", date: "2017-01-22" }
];
const arr2 = [
{ nameId: "abdc4051", name: "ab" },
{ nameId: "abdc4052", name: "abc" }
];
const map = new Map();
arr1.forEach(item => map.set(item.id, item));
arr2.forEach(item => map.set(item.nameId, {...map.get(item.nameId), ...item}));
const mergedArr = Array.from(map.values());
console.log(JSON.stringify(mergedArr));
.as-console-wrapper { max-height: 100% !important; top: 0; }
Run the stack snippet to see the result:
[
{
"id": "abdc4051",
"date": "2017-01-24",
"nameId": "abdc4051",
"name": "ab"
},
{
"id": "abdc4052",
"date": "2017-01-22",
"nameId": "abdc4052",
"name": "abc"
},
{
"id": "abdc4053",
"date": "2017-01-22"
}
]
Here's an O(n) solution using reduce and Object.assign
const joinById = ( ...lists ) =>
Object.values(
lists.reduce(
( idx, list ) => {
list.forEach( ( record ) => {
if( idx[ record.id ] )
idx[ record.id ] = Object.assign( idx[ record.id ], record)
else
idx[ record.id ] = record
} )
return idx
},
{}
)
)
To use this function for the OP's case, pass in the arrays you want to join to joinById (notice lists is a rest parameter).
let joined = joinById(list1, list2)
Each list gets reduced to a single object where the keys are ids and the values are the objects. If there's a value at the given key already, it gets object.assign called on it and the current record.
Here's the generic O(n*m) solution, where n is the number of records and m is the number of keys. This will only work for valid object keys. You can convert any value to base64 and use that if you need to.
const join = ( keys, ...lists ) =>
lists.reduce(
( res, list ) => {
list.forEach( ( record ) => {
let hasNode = keys.reduce(
( idx, key ) => idx && idx[ record[ key ] ],
res[ 0 ].tree
)
if( hasNode ) {
const i = hasNode.i
Object.assign( res[ i ].value, record )
res[ i ].found++
} else {
let node = keys.reduce( ( idx, key ) => {
if( idx[ record[ key ] ] )
return idx[ record[ key ] ]
else
idx[ record[ key ] ] = {}
return idx[ record[ key ] ]
}, res[ 0 ].tree )
node.i = res[ 0 ].i++
res[ node.i ] = {
found: 1,
value: record
}
}
} )
return res
},
[ { i: 1, tree: {} } ]
)
.slice( 1 )
.filter( node => node.found === lists.length )
.map( n => n.value )
This is essentially the same as the joinById method, except that it keeps an index object to identify records to join. The records are stored in an array and the index stores the position of the record for the given key set and the number of lists it's been found in.
Each time the same key set is encountered, it finds the node in the tree, updates the element at it's index, and the number of times it's been found is incremented.
After joining, the idx object is removed from the array with the slice and any elements that weren't found in each set are removed. This makes it an inner join, you could remove this filter and have a full outer join.
Finally each element is mapped to it's value, and you have the joined arrays.
You could use an arbitrary count of arrays and map on the same index new objects.
var array1 = [{ id: "abdc4051", date: "2017-01-24" }, { id: "abdc4052", date: "2017-01-22" }],
array2 = [{ id: "abdc4051", name: "ab" }, { id: "abdc4052", name: "abc" }],
result = [array1, array2].reduce((a, b) => a.map((c, i) => Object.assign({}, c, b[i])));
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
If you have 2 arrays need to be merged based on values even its in different order
let arr1 = [
{ id:"1", value:"this", other: "that" },
{ id:"2", value:"this", other: "that" }
];
let arr2 = [
{ id:"2", key:"val2"},
{ id:"1", key:"val1"}
];
you can do like this
const result = arr1.map(item => {
const obj = arr2.find(o => o.id === item.id);
return { ...item, ...obj };
});
console.log(result);
To merge the two arrays on id, assuming the arrays are equal length:
arr1.map(item => ({
...item,
...arr2.find(({ id }) => id === item.id),
}));
We can use lodash here. _.merge works as you expected. It works with the common key present.
_.merge(array1, array2)
Non of these solutions worked for my case:
missing objects can exist in either array
runtime complexity of O(n)
notes:
I used lodash but it's easy to replace with something else
Also used Typescript (just remove/ignore the types)
import { keyBy, values } from 'lodash';
interface IStringTMap<T> {
[key: string]: T;
}
type IIdentified = {
id?: string | number;
};
export function mergeArrayById<T extends IIdentified>(
array1: T[],
array2: T[]
): T[] {
const mergedObjectMap: IStringTMap<T> = keyBy(array1, 'id');
const finalArray: T[] = [];
for (const object of array2) {
if (object.id && mergedObjectMap[object.id]) {
mergedObjectMap[object.id] = {
...mergedObjectMap[object.id],
...object,
};
} else {
finalArray.push(object);
}
}
values(mergedObjectMap).forEach(object => {
finalArray.push(object);
});
return finalArray;
}
You can use array methods
let arrayA=[
{id: "abdc4051", date: "2017-01-24"},
{id: "abdc4052", date: "2017-01-22"}]
let arrayB=[
{id: "abdc4051", name: "ab"},
{id: "abdc4052", name: "abc"}]
let arrayC = [];
arrayA.forEach(function(element){
arrayC.push({
id:element.id,
date:element.date,
name:(arrayB.find(e=>e.id===element.id)).name
});
});
console.log(arrayC);
//0:{id: "abdc4051", date: "2017-01-24", name: "ab"}
//1:{id: "abdc4052", date: "2017-01-22", name: "abc"}
Here is one-liner (order of elements in array is not important and assuming there is 1 to 1 relationship):
var newArray = array1.map(x=>Object.assign(x, array2.find(y=>y.id==x.id)))
I iterated through the first array and used the .find method on the second array to find a match where the id are equal and returned the result.
const a = [{ id: "abdc4051", date: "2017-01-24" },{ id: "abdc4052", date: "2017-01-22" }];
const b = [{ id: "abdc4051", name: "ab" },{ id: "abdc4052", name: "abc" }];
console.log(a.map(itm => ({...itm, ...b.find(elm => elm.id == itm.id)})));
You can recursively merge them into one as follows:
function mergeRecursive(obj1, obj2) {
for (var p in obj2) {
try {
// Property in destination object set; update its value.
if (obj2[p].constructor == Object) {
obj1[p] = this.mergeRecursive(obj1[p], obj2[p]);
} else {
obj1[p] = obj2[p];
}
} catch (e) {
obj1[p] = obj2[p];
}
}
return obj1;
}
arr1 = [
{ id: "abdc4051", date: "2017-01-24" },
{ id: "abdc4052", date: "2017-01-22" }
];
arr2 = [
{ id: "abdc4051", name: "ab" },
{ id: "abdc4052", name: "abc" }
];
mergeRecursive(arr1, arr2)
console.log(JSON.stringify(arr1))
Irrespective of the order you can merge it by,
function merge(array,key){
let map = {};
array.forEach(val=>{
if(map[val[key]]){
map[val[key]] = {...map[val[key]],...val};
}else{
map[val[key]] = val;
}
})
return Object.keys(map).map(val=>map[val]);
}
let b = [
{ id: "abdc4051", name: "ab" },
{ id: "abdc4052", name: "abc" }
];
let a = [
{ id: "abdc4051", date: "2017-01-24" },
{ id: "abdc4052", date: "2017-01-22" }
];
console.log(merge( [...a,...b], 'id'));
An approach if both two arrays have non-intersect items.
const firstArray = [
{ id: 1, name: "Alex", salutation: "Mr." },
{ id: 2, name: "Maria", salutation: "Ms." },
];
const secondArray = [
{ id: 2, address: "Larch Retreat 31", postcode: "123452" },
{ id: 3, address: "Lycroft Close 12D", postcode: "123009" },
];
const mergeArr = (arr1, arr2) => {
const obj = {};
arr1.forEach(item => {
obj[item.id] = item;
});
arr2.forEach(item => {
obj[item.id]
? (obj[item.id] = { ...obj[item.id], ...item })
: (obj[item.id] = item);
});
return Object.values(obj);
};
const output = mergeArr(firstArray, secondArray);
console.log(output);
Python 3 Solution for someone who lands on this page in hope of finding one
def merge(studentDetails, studentMark, merge_key):
student_details = {}
student_marks = {}
for sd, sm in zip(studentDetails, studentMark):
key = sd.pop(merge_key)
student_details[key] = sd
key = sm.pop(merge_key)
student_marks[key] = sm
res = []
for id, val in student_details.items():
# Merge three dictionary together
temp = {**{"studentId": id}, **val, **student_marks[id]}
res.append(temp)
return res
if __name__ == '__main__':
# Test Case 1
studentDetails = [
{"studentId": 1, "studentName": 'Sathish', "gender": 'Male', "age": 15},
{"studentId": 2, "studentName": 'kumar', "gender": 'Male', "age": 16},
{"studentId": 3, "studentName": 'Roja', "gender": 'Female', "age": 15},
{"studentId": 4, "studentName": 'Nayanthara', "gender": 'Female', "age": 16},
]
studentMark = [
{"studentId": 1, "mark1": 80, "mark2": 90, "mark3": 100},
{"studentId": 2, "mark1": 80, "mark2": 90, "mark3": 100},
{"studentId": 3, "mark1": 80, "mark2": 90, "mark3": 100},
{"studentId": 4, "mark1": 80, "mark2": 90, "mark3": 100},
]
# Test Case 2
array1 = [
{"id": "abdc4051", "date": "2017-01-24"},
{"id": "abdc4052", "date": "2017-01-22"}
]
array2 = [
{"id": "abdc4051", "name": "ab"},
{"id": "abdc4052", "name": "abc"}
]
output = merge(studentDetails, studentMark, merge_key="studentId")
[print(a) for a in output]
output = merge(array1, array2, merge_key="id")
[print(a) for a in output]
Output
{'studentId': 1, 'studentName': 'Sathish', 'gender': 'Male', 'age': 15, 'mark1': 80, 'mark2': 90, 'mark3': 100}
{'studentId': 2, 'studentName': 'kumar', 'gender': 'Male', 'age': 16, 'mark1': 80, 'mark2': 90, 'mark3': 100}
{'studentId': 3, 'studentName': 'Roja', 'gender': 'Female', 'age': 15, 'mark1': 80, 'mark2': 90, 'mark3': 100}
{'studentId': 4, 'studentName': 'Nayanthara', 'gender': 'Female', 'age': 16, 'mark1': 80, 'mark2': 90, 'mark3': 100}
{'studentId': 'abdc4051', 'date': '2017-01-24', 'name': 'ab'}
{'studentId': 'abdc4052', 'date': '2017-01-22', 'name': 'abc'}
Well... assuming both arrays are of the same length, I would probably do something like this:
var newArr = []
for (var i = 0; i < array1.length; i++ {
if (array1[i].id === array2[i].id) {
newArr.push({id: array1[i].id, date: array1[i].date, name: array2[i].name});
}
}
I was able to achieve this with a nested mapping of the two arrays and updating the initial array:
member.map(mem => {
return memberInfo.map(info => {
if (info.id === mem.userId) {
mem.date = info.date;
return mem;
}
}
}
There are a lot of solutions available for this, But, We can simply use for loop and if conditions to get merged arrays.
const firstArray = [
{ id: 1, name: "Alex", salutation: "Mr." },
{ id: 2, name: "Maria", salutation: "Ms." },
];
const secondArray = [
{ id: 1, address: "Larch Retreat 31", postcode: "123452" },
{ id: 2, address: "Lycroft Close 12D", postcode: "123009" },
];
let mergedArray: any = [];
for (const arr1 of firstArray) {
for (arr2 doc of secondArray) {
if (arr1.id === arr2.id) {
mergedArray.push({ ...arr1, ...arr2 });
}
}
}
console.log(mergedArray)
Here is converting the best answer (jsbisht) into a function that accepts the keys as arguments.
const mergeArraysByKeyMatch = (array1, array2, key1, key2) => {
const map = new Map();
array1.forEach((item) => map.set(item[key1], item));
array2.forEach((item) =>
map.set(item[key2], { ...map.get(item[key2]), ...item })
);
const merged = Array.from(map.values());
return merged;
};
A Typescript O(n+m) (which could be classified as O(n)) solution; without lodash:
// RequireAtLeastOne from https://stackoverflow.com/questions/40510611/typescript-interface-require-one-of-two-properties-to-exist/49725198#49725198
type RequireAtLeastOne<T, Keys extends keyof T = keyof T> = Pick<
T,
Exclude<keyof T, Keys>
> &
{
[K in Keys]-?: Required<Pick<T, K>> & Partial<Pick<T, Exclude<Keys, K>>>;
}[Keys];
export const mergeDualArraysOnKey = <
K extends PropertyKey,
T extends RequireAtLeastOne<{ [f in PropertyKey]?: unknown }, K>
>(
key: K,
...lists: [T[], T[]]
): T[] => {
const lookup: { [key in string]: number } = {};
return lists[0].concat(lists[1]).reduce((acc: T[], value: T, i: number) => {
const lookupKey = `${value[key]}`;
if (lookup.hasOwnProperty(lookupKey)) {
acc[lookup[lookupKey]] = Object.assign({}, acc[lookup[lookupKey]], value);
} else {
acc.push(value);
lookup[lookupKey] = acc.length - 1;
}
return acc;
}, []);
};
First concatenates the two arrays and then iterates through the newly created array. It uses a lookup table (object) to store the index of an item in the final merged array which has the same key and merges the objects inplace.
If this needed to be extended to handle more arrays, could use a loop or recursion as a wrapping function:
const mergeArrays = <
K extends PropertyKey,
T extends RequireAtLeastOne<{ [f in PropertyKey]?: unknown }, K>
>(
key: K,
...lists: T[][]
): T[] => {
if (lists.length === 1) {
return lists[0];
}
const l1 = lists.pop() || [];
const l2 = lists.pop() || [];
return mergeArrays(key, mergeDualArraysOnKey(key, l1, l2), ...lists);
};
with usage being:
const arr1 = [
{ id: "abdc4052", date: "2017-01-22" },
{ id: "abdc4052", location: "US" },
{ id: "abdc4051", date: "2017-01-24" },
{ id: "abdc4053", date: "2017-01-24" },
{ id: "abdc4054", date: "2017-01-24" },
{ id: "abdc4055", location: "US" },
];
const arr2 = [
{ id: "abdc4052", date: "2017-01-22" },
{ id: "abdc4052", name: "abc" },
{ id: "abdc4055", date: "2017-01-24" },
{ id: "abdc4055", date: "2017-01-24", name: "abcd" },
];
const arr3 = [{ id: "abdc4056", location: "US" }];
const arr4 = [
{ id: "abdc4056", name: "abcde" },
{ id: "abdc4051", name: "ab--ab" },
];
mergeArrays<
"id",
{
id: string;
date?: string;
location?: string;
name?: string;
}
>("id", arr1, arr2, arr3, arr4)
Base on your example, you can do it this way:
const arrayOne = [
{ id: "abdc4051", date: "2017-01-24" },
{ id: "abdc4052", date: "2017-01-22" }
]
const arrayTwo = [
{ id: "abdc4051", name: "ab" },
{ id: "abdc4052", name: "abc" }
]
const mergeArrays = () => {
arrayOne.forEach((item, i) => {
const matchedFound = arrayTwo.findIndex(a => a.id === item.id);
arrayOne[i] = {
...item,
...matchedFound,
}
});
};
mergeArrays();
console.log(arrayOne);
This is a version when you have an object and an array and you want to merge them and give the array a key value so it fits into the object nicely.
var fileData = [
{ "id" : "1", "filename" : "myfile1", "score" : 33.1 },
{ "id" : "2", "filename" : "myfile2", "score" : 31.4 },
{ "id" : "3", "filename" : "myfile3", "score" : 36.3 },
{ "id" : "4", "filename" : "myfile4", "score" : 23.9 }
];
var fileQuality = [0.23456543,0.13413131,0.1941344,0.7854522];
var newOjbect = fileData.map((item, i) => Object.assign({}, item, {fileQuality:fileQuality[i]}));
console.log(newOjbect);