Combine matching object arrays - javascript

I am trying do combine the nested objects inside items with the same key.
Find 'top level' values that are duplicated,
Combine the duplicated 'top level' items into one object (including their children.
There should be no duplicate values inside the 'type' arrays
I tried it here https://jsfiddle.net/Lpq6huvw/410/
Input data:
[{
"a": "Mon",
"type": [{
"b": 1
}, {
"b": 3
}]
}, {
"a": "Mon",
"type": [{
"b": 2
}]
}, {
"a": "Tue",
"type": [{
"b": 40
}]
}, {
"a": "Tue",
"type": [{
"b": 50
}]
}, {
"a": "Wed",
"type": [{
"b": 30
}]
}]
Into this array:
[{
"a": "Mon",
"type": [{
"b": 1
}, {
"b": 3
},
{
"b": 2
}]
},
{
"a": "Tue",
"type": [{
"b": 40
},
{
"b": 50
}]
}, {
"a": "Wed",
"type": [{
"b": 30
}]
}]
I attempted this below, which maps all the duplicated items as ONE object. However, I want it to map each under its' 'top level' predecessor.
const z = _.uniqBy(_.filter(data.map(e=>e.a), v => _.filter(data.map(e=>e.a), v1 => v1 === v).length > 1))
const dupes = data.filter(itm => z.includes(itm.a))
const flat = _.flatMap(dupes, item =>
_(item.type)
.map(v => ({b: v.b}))
.value()
)

I personally find Javascript's built in functions work nice, and seem easier to follow than some of lodash functions.
eg.
var data = [{"a":"Mon","type":[{"b":1},{"b":3}]},{"a":"Mon","type":[{"b":2},{"b":3}]},{"a":"Tue","type":[{"b":40}]},{"a":"Tue","type":[{"b":50}]},{"a":"Wed","type":[{"b":30}]}];
var result = data.reduce((acc, val) => {
var found = acc.find((findval) => val.a === findval.a);
if (!found) acc.push(val)
else found.type = found.type.concat(
val.type.filter((f) => !found.type.find((findval) => f.b === findval.b)));
return acc;
}, []);
console.log(result);

Here's a answer w/o lodash:
function combine (input) {
const hash = input.reduce((result, current) => {
if (result[current['a']]) {
result[current['a']] = result[current['a']].concat(current['type'])
} else {
result[current['a']] = current['type']
}
return result
}, {})
return Object.keys(hash).map(key => {
return {
a: key,
type: hash[key]
}
})
}

ES6: you can iterate with Array#reduce, collect the items into a Map, and then convert back to an array with the spread syntax and Map#values:
const data = [{"a":"Mon","type":[{"b":1},{"b":3}]},{"a":"Mon","type":[{"b":2}]},{"a":"Tue","type":[{"b":40}]},{"a":"Tue","type":[{"b":50}]},{"a":"Wed","type":[{"b":30}]}];
const result = [...data.reduce((m, { a, type }) => {
const item = m.get(a) || { a, type: [] }; // use a Set to maintain uniqueness
item.type.push(...type);
return m.set(a, item);
}, new Map).values()]
.map(({ a, type }) => ({ // make types unique again
a,
type: [...type.reduce((m, o) => m.has(o.b) ? m : m.set(o.b, o), new Map).values()]
}));
console.log(result);
Lodash: Use _.groupBy() to get all objects with the same a property in one group. Map the groups, and merge each group using _.mergeWith(), and concat all type arrays.
Make another pass with map to make all items in type arrays unique.
const data = [{"a":"Mon","type":[{"b":1},{"b":3}]},{"a":"Mon","type":[{"b":2}]},{"a":"Tue","type":[{"b":40}]},{"a":"Tue","type":[{"b":50}]},{"a":"Wed","type":[{"b":30}]}];
const result = _(data)
.groupBy('a')
.map((group) => _.mergeWith({}, ...group, ((objValue, srcValue, key) =>
key === 'type' ? (objValue || []).concat(srcValue) : undefined
)))
.map((obj) => Object.assign(obj, { type: _.uniq(obj.type) }))
.value();
console.log(result);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.4/lodash.min.js"></script>

Related

Create an array of objects based on an object if one or more properties have multiple values differentiated by a comma

i'm trying to duplicate objects based on two properties that have multiple values differentiated by a comma.
For example:
I have an object
const obj = {
id: 1
date: "2021"
tst1: "111, 222"
tst2: "AAA, BBB"
}
And I would like the result to be an array of 2 objects in this case (because there are 2 values in tst1 OR tst2, these 2 properties will always have the same nr of values differentiated by a comma)
[{
id: 1,
date: "2021",
tst1: "111",
tst2: "AAA",
},
{
id: 1,
date: "2021",
tst1: "222",
tst2: "BBB",
}]
What I tried is this:
I created a temporary object
const tempObject = {
id: obj.id,
date: obj.date,
}
And then I would split and map the property that has multiple values, like this:
cont newObj = obj.tst1.split(",").map(function(value) {
let finalObj = {}
return finalObj = {
id: tempObject.id,
date: tempObject.date,
tst1: value,
})
And now, the newObj is an array of objects and each object contains a value of tst1.
The problem is I still have to do the same for the tst2...
And I was wondering if there is a simpler method to do this...
Thank you!
Here is an example that accepts an array of duplicate keys to differentiate. It first maps them to arrays of entries by splitting on ',' and then trimming the entries, then zips them by index to create sub-arrays of each specified property, finally it returns a result of the original object spread against an Object.fromEntries of the zipped properties.
const mapDuplicateProps = (obj, props) => {
const splitProps = props.map((p) =>
obj[p].split(',').map((s) => [p, s.trim()])
);
// [ [[ 'tst1', '111' ], [ 'tst1', '222' ]], [[ 'tst2', 'AAA' ], [ 'tst2', 'BBB' ]] ]
const dupeEntries = splitProps[0].map((_, i) => splitProps.map((p) => p[i]));
// [ [[ 'tst1', '111' ], [ 'tst2', 'AAA' ]], [[ 'tst1', '222' ], [ 'tst2', 'BBB' ]] ]
return dupeEntries.map((d) => ({ ...obj, ...Object.fromEntries(d) }));
};
const obj = {
id: 1,
date: '2021',
tst1: '111, 222',
tst2: 'AAA, BBB',
};
console.log(mapDuplicateProps(obj, ['tst1', 'tst2']));
Not sure if that's what you're searching for, but I tried making a more general use of what you try to do:
const duplicateProperties = obj => {
const properties = Object.entries(obj);
let acc = [{}];
properties.forEach(([key, value]) => {
if (typeof value === 'string' && value.includes(',')) {
const values = value.split(',');
values.forEach((v, i) => {
if (!acc[i]) {
acc[i] = {};
}
acc[i][key] = v.trim();
});
} else {
acc.forEach(o => o[key] = value);
}
});
return acc;
};
const obj = {
id: 1,
date: '2021',
tst1: '111, 222',
tst2: 'AAA, BBB',
};
console.log(duplicateProperties(obj));
You could start by determining the length of the result using Math.max(), String.split() etc.
Then you'd create an Array using Array.from(), returning the correct object for each value of the output index.
const obj = {
id: 1,
date: "2021",
tst1: "111, 222",
tst2: "AAA, BBB",
}
// Determine the length of our output array...
const length = Math.max(...Object.values(obj).map(s => (s + '').split(',').length))
// Map the object using the relevant index...
const result = Array.from({ length }, (_, idx) => {
return Object.fromEntries(Object.entries(obj).map(([key, value]) => {
const a = (value + '').split(/,\s*/);
return [key, a.length > 1 ? a[idx] : value ]
}))
})
console.log(result)
.as-console-wrapper { max-height: 100% !important; }

How to group and concat values by keys in JS

I try like a maniac to do some kind of a merge / sort by key on an array of object.
Don't know why but I cannot figure how to do that.
My app is in Ionic / Angular.
Here is what I have :
[
{
"2021": {
"a": "aText"
}
},
{
"2021": {
"b": "bText"
}
},
{
"2020": {
"z": "zText"
}
},
{
"2020": {
"y": "yText"
}
},
{
"2020": {
"x": "xText"
}
}
]
My goal is to get this :
[
{
"2021": { "a": "aText", "b": "bText" }
},
{
"2020": { "z": "zText", "y": "yText", "x": "xText" }
}
]
In other words, I'd like to regroup by year and concat them.
Does anybody have an idea on how to do that ?
If you just want a single object with years as keys then it's just a standard 'group by' with a nested loop to iterate the Object.entries() of each object. If you do want your originally posted output (an array of individual objects) you can just map() the entires of the returned grouped object and convert each to an object using Object.fromEntries()
const input = [
{ 2021: { a: 'aText' } },
{ 2021: { b: 'bText' } },
{ 2020: { z: 'zText' } },
{ 2020: { y: 'yText' } },
{ 2020: { x: 'xText' } },
];
const grouped_object = input.reduce(
(a, o) => (Object.entries(o).forEach(([y, o]) => (a[y] = { ...(a[y] ?? {}), ...o })), a),
{}
);
// if you just want a single object with years as keys
console.log(grouped_object);
const grouped_array = Object.entries(grouped_object)
.map(([year, data]) => ({[year]: data}));
// the output from your question
console.log(grouped_array);
.as-console-wrapper { max-height: 100% !important; top: 0; }
Or refactored to use a for...of loop and Object.assign()
const input = [
{ 2021: { a: 'aText' } },
{ 2021: { b: 'bText' } },
{ 2020: { z: 'zText' } },
{ 2020: { y: 'yText' } },
{ 2020: { x: 'xText' } },
];
const grouped_object = {};
for (const obj of input) {
for (const [year, data] of Object.entries(obj)) {
grouped_object[year] = Object.assign(grouped_object[year] ?? {}, data);
}
}
// if you just want a single object with years as keys
console.log(grouped_object);
// or avoiding computed properties
const grouped_array = Object.entries(grouped_object)
.map(([year, data]) => (o={}, o[year]=data, o));
// the output from your question
console.log(grouped_array);
.as-console-wrapper { max-height: 100% !important; top: 0; }
It would be better to use an object to group by year. This is an example that uses reduce to iterate over the array to produce that object.
const data=[{2021:{a:"aText"}},{2021:{b:"bText"}},{2020:{z:"zText"}},{2020:{y:"yText"}},{2020:{x:"xText"}}];
const out = data.reduce((acc, obj) => {
// Get the key and value from the object that in
// the current iteration
const [ [ key, value ] ] = Object.entries(obj);
// If the key doesn't exist on the accumulator (the initial
// object that we passed into the `reduce`) create an empty object
acc[key] = acc[key] || {};
// Update the value of that object property with
// the value of the object
acc[key] = { ...acc[key], ...value };
// Return the updated object for the next iteration
return acc;
// Here's the initial object that
// acts as the accumulator through all the iterations
}, {});
console.log(out);
Or using an array to hold the information for each year:
const data=[{2021:{a:"aText"}},{2021:{b:"bText"}},{2020:{z:"zText"}},{2020:{y:"yText"}},{2020:{x:"xText"}}];
const out = data.reduce((acc, obj) => {
const [ [ key, value ] ] = Object.entries(obj);
// Use an array instead of an object
acc[key] = acc[key] || [];
// Push the first element of the Object.values
// into the array
acc[key] = [ ...acc[key], Object.values(value)[0] ];
return acc;
}, {});
console.log(out);
Additional documentation
Object.entries
Object.values
Destructuring assignment
Spread syntax

How to group two arrays using key in node js

I have below two objects which I want to merge.
[
{
"response_code": 1,
"response_message": [{
"a": 1000,
"b": 1000001,
"c": 10000002
}]
}]
[
{
"response_code": 1,
"response_message": [{
"a": 2000,
"b": 2000001,
"c": 20000002
}]
}
]
I want to merge them like below by having only one value of response code and merge values of response message like below way.
{
"response_code": 1,
"response_message": [
{
"a": 1000,
"b": 1000001,
"c": 10000002
},
{
"a": 2000,
"b": 2000001,
"c": 20000002
}]
}
Really stuck with such complicated merging where I want only once the value of response code and merge the values of response message.
Here I want to remove response code value from other array and merge/group the response message value with fist array.
I whipped up a little function for you:
And in accordance with the concerns raised by you in the comments, along with the test case where response_message was a string, I have edited the code snippet to include multiple cases to test.
const inputs = [
[{
"response_code": 1,
"response_message": [{
"a": 1000,
"b": 1000001,
"c": 10000002
}]
},
{
"response_code": 1,
"response_message": [{
"p": 1000,
"q": 1000001,
"r": 10000002
}]
}
],
[{
"response_code": 1,
"response_message": [{
"a": 1000,
"b": 1000001,
"c": 10000002
}]
},
{
"response_code": 1,
"response_message": 'No data'
}
],
[{
"response_code": 1,
"response_message": 'No data'
},
{
"response_code": 1,
"response_message": 'No data'
}
]
]
const getGroupedArr = (arr) => {
const codeMap = arr.reduce((cMap,obj) => {
let existingMessageArr = cMap.get(obj.response_code);
let arrayToAdd = Array.isArray(obj.response_message) ? obj.response_message : [];
if(existingMessageArr){
existingMessageArr.push(...arrayToAdd);
} else {
cMap.set(obj.response_code,arrayToAdd);
}
return cMap;
},new Map());
const iterator = codeMap[Symbol.iterator]();
const resultArr = [];
for (let item of iterator) {
resultArr.push({
response_code: item[0],
response_message: item[1]
})
}
return resultArr;
}
inputs.forEach((inputArr,index) => {
console.log(`Result for input ${index+1}`,getGroupedArr(inputArr));
})
Notice that I used Map where in JS most people prefer objects because maps in JS are iterable, but with an object I would've had to do an extra Object.keys() step, so this makes is slightly more efficient than the object approach, though a little more verbose.
Also note that in the third case, when no object with a particular response_code has any data, the result would be an empty array rather than a string. In weakly typed environments like JS, it is always a good practice to maintain some type consistency (which actually makes the input value of 'No data' in response_code not ideal), otherwise you may need to put type checks everywhere (like in the edited funciton in the above snippet).
Same function can be used in a contraint you mentioned in the comments, when the objects with same response_code exist in two different arrays (the two input arrays can simply be merged into one):
const inputArr1 = [{
"response_code": 1,
"response_message": [{
"a": 1000,
"b": 1000001,
"c": 10000002
}]
}]
const inputArr2 = [{
"response_code": 1,
"response_message": [{
"p": 1000,
"q": 1000001,
"r": 10000002
}]
}]
const getGroupedArr = (arr) => {
const codeMap = arr.reduce((cMap,obj) => {
let existingMessageArr = cMap.get(obj.response_code);
let arrayToAdd = Array.isArray(obj.response_message) ? obj.response_message : [];
if(existingMessageArr){
existingMessageArr.push(...arrayToAdd);
} else {
cMap.set(obj.response_code,arrayToAdd);
}
return cMap;
},new Map());
const iterator = codeMap[Symbol.iterator]();
const resultArr = [];
for (let item of iterator) {
resultArr.push({
response_code: item[0],
response_message: item[1]
})
}
return resultArr;
}
console.log(getGroupedArr([...inputArr1,...inputArr2]));
I think you're looking for a groupby.
Check this good old post from stackoverflow and be aware of the different answers/ implementaions:
Most efficient method to groupby on an array of objects
const arrayOf0 = yourArray.filter(item => item.response_code===0)
const arrayOf1 = yourArray.filter(item => item.response_code===1)
const merged0 = {response_code: 0, response_message: []};
const merged1 = {response_code: 1, response_message: []};
arrayOf0.forEach(item => {
merged0.response_message.push(item.response_message[0]
})
arrayOf1.forEach(item => {
merged1.response_message.push(item.response_message[0]
})
Something like this?
I have resolved the array merging by below code.
const mergedArray = [];
myarray.forEach((obj, index) => {
var newObj = {}
if(index > 0){
if(mergedArray.length > 0){
for(let i=0; i<obj.response_message.length;i++){
mergedArray[0].response_message.push(obj.response_message[i]);
}
}
}else{
newObj["response_code"] = obj.response_code ;
newObj["response_message"] = obj.response_message;
mergedArray.push(newObj);
}
})

Map Json data by JavaScript

I have a Json data that I want to have in a different format.
My original json data is:
{
"info": {
"file1": {
"book1": {
"lines": {
"102:0": [
"102:0"
],
"105:4": [
"106:4"
],
"106:4": [
"107:1",
"108:1"
]
}
}
}
}
}
And I want to map it as following:
{
"name": "main",
"children": [
{
"name": "file1",
"children": [
{
"name": "book1",
"group": "1",
"lines": [
"102",
"102"
],
[
"105",
"106"
],
[
"106",
"107",
"108"
]
}
],
"group": 1,
}
],
"group": 0
}
But the number of books and number of files will be more. Here in the lines the 1st part (before the :) inside the "" is taken ("106:4" becomes "106"). The number from the key goes 1st and then the number(s) from the value goes and make a list (["106", "107", "108"]). The group information is new and it depends on parent-child information. 1st parent is group 0 and so on. The first name ("main") is also user defined.
I tried the following code so far:
function build(data) {
return Object.entries(data).reduce((r, [key, value], idx) => {
//const obj = {}
const obj = {
name: 'main',
children: [],
group: 0,
lines: []
}
if (key !== 'reduced control flow') {
obj.name = key;
obj.children = build(value)
if(!(key.includes(":")))
obj.group = idx + 1;
} else {
if (!obj.lines) obj.lines = [];
Object.entries(value).forEach(([k, v]) => {
obj.lines.push([k, ...v].map(e => e.split(':').shift()))
})
}
r.push(obj)
return r;
}, [])
}
const result = build(data);
console.log(result);
The group information is not generating correctly. I am trying to figure out that how to get the correct group information. I would really appreciate if you can help me to figure it out.
You could use reduce method and create recursive function to build the nested structure.
const data = {"info":{"file1":{"book1":{"lines":{"102:0":["102:0"],"105:4":["106:4"],"106:4":["107:1","108:1"]}}}}}
function build(data) {
return Object.entries(data).reduce((r, [key, value]) => {
const obj = {}
if (key !== 'lines') {
obj.name = key;
obj.children = build(value)
} else {
if (!obj.lines) obj.lines = [];
Object.entries(value).forEach(([k, v]) => {
obj.lines.push([k, ...v].map(e => e.split(':').shift()))
})
}
r.push(obj)
return r;
}, [])
}
const result = build(data);
console.log(result);
I couldn't understand the logic behind group property, so you might need to add more info for that, but for the rest, you can try these 2 functions that recursively transform the object into what you are trying to get.
var a = {"info":{"file1":{"book1":{"lines":{"102:0":["102:0"],"105:4":["106:4"],"106:4":["107:1","108:1"]}}}}};
var transform = function (o) {
return Object.keys(o)
.map((k) => {
return {"name": k, "children": (k === "lines" ? parseLines(o[k]) : transform(o[k])) }
}
)
}
var parseLines = function (lines) {
return Object.keys(lines)
.map(v => [v.split(':')[0], ...(lines[v].map(l => l.split(":")[0]))])
}
console.log(JSON.stringify(transform(a)[0], null, 2));

Convert an array of objects to a dictionary by letter [duplicate]

This question already has answers here:
Most efficient method to groupby on an array of objects
(58 answers)
Closed 3 years ago.
I have an array of objects that looks like this:
let stuff = [
{
"id": "48202847",
"name": "Doe"
},
{
"id": "17508",
"name": "Marie"
},
{
"id": "175796",
"name": "Robert"
},
{
"id": "175796",
"name": "Ronald"
},
]
What I want to get is a dictionary looking something like this:
{
"D": [{"id": "48202847", "name": "Doe"}],
"M": [{"id": "17508", "name": "Marie"}],
"R": [{"id": "175796", "name": "Robert"}, {"id": "175796", "name": "Ronald"}]
}
Notice how all the people whose name starts with "R" are listed under one key.
This is my function that creates a dictionary with the person's name as the key:
const byId = (array) =>
array.reduce((obj, item) => {
obj[item.name] = item
return obj
}, {})
But this obviously doesn't do what I want it to. I do have some ideas of how to make this possible, but they are extremely legacy and I would love to know how to do this right.
Any help is appreciated!
You need the first character, uppercase and an array for collecting the objects.
const byId = array =>
array.reduce((obj, item) => {
var key = item.name[0].toUpperCase(); // take first character, uppercase
obj[key] = obj[key] || []; // create array if not exists
obj[key].push(item); // push item
return obj
}, {});
let stuff = [{ id: "48202847", name: "Doe" }, { id: "17508", name: "Marie" }, { id: "175796", name: "Robert" }, { id: "175796", name: "Ronald" }],
result = byId(stuff)
console.log(result);
Here's a solution based on Set, map, reduce and filter:
let stuff = [{"id": "48202847","name": "Doe"},{"id": "17508","name": "Marie"},{"id": "175796","name": "Robert"},{"id": "175796","name": "Ronald"}];
let result = [...new Set(stuff.map(x => x.name[0]))]
.reduce((acc, val) => {
return acc = { ...acc,
[val]: stuff.filter(x => x.name.startsWith(val))
}
}, {});
console.log(result);
Great solution Nina! Could be made a little cleaner by utilizing the spread operator.
const byId = (array) =>
array.reduce((obj, item) => {
var key = item.name[0].toUpperCase();
return {
...obj,
[key]: obj[key] ? [...obj[key], item] : [item],
}
}, {});

Categories