I have an object array with duplicate key / value pairs. I have used .map() to manipulate it's structure. I'd like to combine these duplicate pairs by adding the property System.Title as an additional object to the item that shares the same Title.
var doctypes = [{
"System": {
"Title": "FIS NetImage"
},
"Title": "Notices",
}, {
"System": {
"Title": "OmniView"
},
"Title": "Notices",
}, {
"System": {
"Title": "Nautilus"
},
"Title": "Reports",
}, {
"System": {
"Title": "FIS NetImage"
},
"Title": "Statements",
}]
var modDocTypes = doctypes.map(modDocType => ({
Title: modDocType.Title,
System: [{
Title: modDocType.System.Title
}]
}))
console.log(modDocTypes)
// DESIRED OUTCOME:
//[
// {
// "Title": "Notices",
// "System": [
// {
// "Title": "FIS NetImage"
// },
// {
// "Title": "OmniView"
// }
// ]
// },
// {
// "Title": "Reports",
// "System": [
// {
// "Title": "Nautilus"
// }
// ]
// },
// {
// "Title": "Statements",
// "System": [
// {
// "Title": "FIS NetImage"
// }
// ]
// }
//]
How can I accomplish this?
Because the input array objects and output array objects are not one-to-one, you'll have to use reduce instead to combine the identical Titles:
const input = [{
"System": {
"Title": "FIS NetImage"
},
"Title": "Notices",
}, {
"System": {
"Title": "OmniView"
},
"Title": "Notices",
}, {
"System": {
"Title": "Nautilus"
},
"Title": "Reports",
}, {
"System": {
"Title": "FIS NetImage"
},
"Title": "Statements",
}];
const output = input.reduce((a, { Title, System }) => {
const foundTitleObj = a.find(obj => obj.Title === Title);
if (foundTitleObj) {
foundTitleObj.System.push(System);
return a;
}
const newTitleObj = {
Title,
System: [ System ],
};
a.push(newTitleObj);
return a;
}, []);
console.log(output);
Related
I have a data array, there are multiple objects in this array, and the data object is an array. There may be more than one object in this array. How can I copy these objects under a single array?
const test = []
const data = [
{
"_id": "124141",
"name": "test",
"data": [
{
"price":10,
"title": "sda"
},
]
},
{
"_id": "2525",
"name": "test2",
"data": [
{
"price":20,
"title": "asdas"
},
]
}
]
[{
"price":10,
"title": "sda"
},
{
"price":20,
"title": "asdas"
},
]
If this is the output I expect, it should be like this. how can I do that
const data = [
{
"_id": "124141",
"name": "test",
"data": [
{
"price":10,
"title": "sda"
},
{
"price":99,
"title":"aaaaa"
}
]
},
{
"_id": "2525",
"name": "test2",
"data": [
{
"price":20,
"title": "asdas"
},
]
}
];
console.log(data.map(e => e.data).flat());
// OR
console.log(data.flatMap(e => e.data));
I have a nested array like below. There are about 100 de objects in the array. The de objects also have deg[0] array but most likely I will only have the first index. Now the trick is that the de are subset of deg. Which means each deg can have say 10 de. How can I retrieve the deg and there associated de and map it into a new array like:
newArray = [
deg1: [
{de1},
{de2}
],
deg2: [
{de1},
{de2}
]
]
Here is my nested array. I posted four but the list is over a 100.
{
"name": "Report",
"id": "2YYUEZ6I1r9",
"dse1": [
{
"de1": {
"name": "Number",
"id": "HjMOngg3kuy",
"de1-av": [
{
"value": "FHaQMPv9zc7",
"attribute": {
"id": "uwVkIP7PZDt"
}
},
{
"value": "something",
"attribute": {
"id": "FHaQMPv9zc7"
}
}
],
"deg1": [
{
"name": "TB",
"id": "2XJB1JO9qX8"
}
]
}
},
{
"de2": {
"name": "Number of",
"id": "a3dtGETTawy",
"de2-av": [
{
"value": "FHaQMPv9zc7",
"attribute": {
"id": "uwVkIP7PZDt"
}
},
{
"value": "something",
"attribute": {
"id": "FHaQMPv9zc7"
}
}
],
"deg1": [
{
"name": "Secondary",
"id": "w99RWzXHgtw"
}
]
}
},
{
"de1": {
"name": "Number of",
"id": "a3dtGETTawy",
"de1av": [
{
"value": "FHaQMPv9zc7",
"attribute": {
"id": "uwVkIP7PZDt"
}
},
{
"value": "something",
"attribute": {
"id": "FHaQMPv9zc7"
}
}
],
"deg2": [
{
"name": "Secondary",
"id": "w99RWzXHgtw"
}
]
}
},
{
"de2": {
"name": "Number of",
"id": "a3dtGETTawy",
"de2av": [
{
"value": "FHaQMPv9zc7",
"attribute": {
"id": "uwVkIP7PZDt"
}
},
{
"value": "something",
"attribute": {
"id": "FHaQMPv9zc7"
}
}
],
"deg2": [
{
"name": "Tertiary",
"id": "w99RWzXHgtw"
}
]
}
}
]
}
Group array of objects by property (this time a property to be matched by a reg exp) using Array.reduce.
Update: Ignoring missing keys.
var input={name:"Report",id:"2YYUEZ6I1r9",dse1:[{de1:{name:"Number",id:"HjMOngg3kuy","de1-av":[{value:"FHaQMPv9zc7",attribute:{id:"uwVkIP7PZDt"}},{value:"something",attribute:{id:"FHaQMPv9zc7"}}],deg1:[{name:"TB",id:"2XJB1JO9qX8"}]}},{de2:{name:"Number of",id:"a3dtGETTawy","de2-av":[{value:"FHaQMPv9zc7",attribute:{id:"uwVkIP7PZDt"}},{value:"something",attribute:{id:"FHaQMPv9zc7"}}],deg1:[{name:"Secondary",id:"w99RWzXHgtw"}]}},{de1:{name:"Number of",id:"a3dtGETTawy",de1av:[{value:"FHaQMPv9zc7",attribute:{id:"uwVkIP7PZDt"}},{value:"something",attribute:{id:"FHaQMPv9zc7"}}],deg2:[{name:"Secondary",id:"w99RWzXHgtw"}]}},{de2:{name:"Number of",id:"a3dtGETTawy",de2av:[{value:"FHaQMPv9zc7",attribute:{id:"uwVkIP7PZDt"}},{value:"something",attribute:{id:"FHaQMPv9zc7"}}],deg2:[{name:"Tertiary",id:"w99RWzXHgtw"}]}}]}
var reg = new RegExp("^de[0-9]+$");
var reg2 = new RegExp("^deg[0-9]+$");
let obj = input['dse1'].reduce(function(agg, item) {
// do your group by logic below this line
var key = Object.keys(item).find(function(key) {
return key.match(reg) ? key : null;
})
if (key) {
var key2 = Object.keys(item[key]).find(function(key) {
return key.match(reg2) ? key : null;
})
agg[key] = agg[key] || [];
if (key2) {
var to_push = {}
to_push[key2] = item[key][key2]
agg[key].push(to_push)
}
}
// do your group by logic above this line
return agg
}, {});
console.log(obj)
.as-console-wrapper {
max-height: 100% !important;
}
I have this JSON data:
{
"data": [
{
"category": {
"documentId": "c8kr0cv012vtr8vm3iug",
"title": "Art"
},
"subcategories": [
{
"documentId": "c8kr7nv012vtr8vm3l8g",
"title": "Architecture"
},
{
"documentId": "c8kr7nv012vtr8vm3lag",
"title": "Dance"
},
{
"documentId": "c8kr7nv012vtr8vm3lbg",
"title": "Fashion"
}
]
},
{
"category": {
"documentId": "c8kr0cv012vtr8vm3iqg",
"title": "Business"
},
"subcategories": [
{
"documentId": "c8kr3d7012vtr8vm3jjg",
"title": "Crypto"
},
{
"documentId": "c8kr3d7012vtr8vm3jj0",
"title": "Finance"
},
{
"documentId": "c8kr3d7012vtr8vm3jkg",
"title": "Marketing"
}
]
}
]
}
I have tagview in my mobile screen so user can select multiple subcategory at a same time by select deselect so if i choose 2 subcategory from Art and 2 from Business then expected post payload should be like below
{
"data": [
{
"category": "c8kr0cv012vtr8vm3iug",
"subcategories": [
"c8kr7nv012vtr8vm3l8g",
"c8kr7nv012vtr8vm3lag"
]
},
{
"category": "c8kr0cv012vtr8vm3iqg",
"subcategories": [
"c8kr3d7012vtr8vm3jjg",
"c8kr3d7012vtr8vm3jj0"
]
}
]
}
So my code is like below
const tempDefaultPayload = dataPayload.map(x => {
if (x.category) {
x.subcategories?.push(subcategory);
if (userData?.userInterests?.map(v => v.category.documentId === category)) {
const arrayNewData: any = [];
const newData: any = userData?.userInterests?.map(i =>
i.subcategories?.map(k => {
arrayNewData.push(k?.documentId);
return k?.documentId;
}),
);
x.subcategories?.push(...arrayNewData);
}
return x;
}
return x;
})
When I run above code payload is not passing correct
[
{
"category": "c8kr0cv012vtr8vm3iug",
"subcategories": [
"c8kr7nv012vtr8vm3l8g",
"c8kr7nv012vtr8vm3lag",
"c8kr3d7012vtr8vm3jjg",
"c8kr3d7012vtr8vm3jj0"
]
},
{
"category": "c8kr0cv012vtr8vm3iqg",
"subcategories": [
"c8kr7nv012vtr8vm3l8g",
"c8kr7nv012vtr8vm3lag",
"c8kr3d7012vtr8vm3jjg",
"c8kr3d7012vtr8vm3jj0"
]
}
]
It is passing all selected subcategory in both array any idea how can I solve this ?
If you have the selected tag ids in the array selectedTags and your json as a variable called data:
const tempDefaultPayload = {
data: data.data.map(x => {
"category": x.category.documentId,
"subcategories": x.subcategories.map(subcat =>
subcat.documentId).filter(item => selectedTags.includes(item))
})
}
Playground example
I have this kind of json.
let arr1 = [
{
"packageReference": "1234",
"displayName": "Business",
"description": "Includes...",
"promotion": {
"packageReference": "1234",
"displayName": "$100 Standard",
"optionGroup": [
{
"displayName": "Access",
},
{
"displayName": "Contract"
},
{
"displayName": "Equipment"
},
{
"displayName": "Features"
},
{
"displayName": "Fees",
}
]
}
}
]
I need to remove only the object in the arr1[0].promotion.optionGroup where the displayName is 'Fees' and to return the new object without him.
You could do it by filtering the sub array like so:
let arr1 = [
{
"packageReference": "1234",
"displayName": "Business",
"description": "Includes...",
"promotion": {
"packageReference": "1234",
"displayName": "$100 Standard",
"optionGroup": [
{
"displayName": "Access",
},
{
"displayName": "Contract"
},
{
"displayName": "Equipment"
},
{
"displayName": "Features"
},
{
"displayName": "Fees",
}
]
}
}
];
arr1 = arr1.map(e => {
e['promotion']['optionGroup'] =
e['promotion']['optionGroup'].filter(s => s['displayName'] != 'Fees');
return e;
});
console.log(arr1);
// Get new array without the Fees one
const newGroup = arr1[0].promotion.optionGroup.filter(group => group.displayName !== 'Fees');
// Put new group into the object
arr1[0].promotion.optionGroup = newGroup;
Could also do it without creating a variable, but added it for cleanness.
I have stored the data in the following manner
[
{
"metadata": {
"subtitle": "kit",
"id": "0063400000yCqRfAAK",
"title": "#9864478"
},
"entityTitle": "Opportunities"
},
{
"metadata": {
"subtitle": "Acceleration",
"id": "0063400000yCqRfAAK",
"title": "#9882278"
},
"entityTitle": "Reports"
}
]
I need to transform this into :
[
{
"entityTitle": "Opportunities",
"searchResults": [
{
"subtitle": "kit",
"id": "0063400000yCqRfAAK",
"title": "#9864478"
}
]
},
{
"entityTitle": "Reports",
"searchResults": [
{
"subtitle": "Acceleration",
"id": "0063400000yCqRfAAK",
"title": "#9882278"
}
]
}
]
I have used _.groupBy , like this
_.groupBy(recentData, function(data) {
return data.entityTitle;
});
and I got :
{
"Opportunities": [
{
"metadata": {
"subtitle": "kit",
"id": "0063400000yCqRfAAK",
"title": "#9864478"
},
"entityTitle": "Opportunities"
}
],
"Reports": [
{
"metadata": {
"subtitle": "Acceleration",
"id": "0063400000yCqRfAAK",
"title": "#9882278"
},
"entityTitle": "Reports"
}
]
}
I need to store the recently searched data into an array, so I am storing the data like this.Can anyone help please me get the data into proper format?
Just a simple mapping will suffice:
const data = [
{
"metadata": {
"subtitle": "kit",
"id": "0063400000yCqRfAAK",
"title": "#9864478"
},
"entityTitle": "Opportunities"
},
{
"metadata": {
"subtitle": "Acceleration",
"id": "0063400000yCqRfAAK",
"title": "#9882278"
},
"entityTitle": "Reports"
}
];
const transformation = data.map( item => ({
"entityTitle": item.entityTitle,
"searchResults": [
item.metadata
]
}));
console.log( transformation );
But I would advice against storing the search results this way.
I would store the current search results in a seperate array and just have the indexes of the entity inside the entities array, so that there's a clear seperation between the searches and the entities, since the entity should not care about if it's being searched or not.
In case there's multiple entities of the same name, we can replace the mapping by a reduction:
const data = [
{
"metadata": {
"subtitle": "kit",
"id": "0063400000yCqRfAAK",
"title": "#9864478"
},
"entityTitle": "Opportunities"
},
{
"metadata": {
"subtitle": "Acceleration",
"id": "0063400000yCqRfAAK",
"title": "#9882278"
},
"entityTitle": "Reports"
},
{
"metadata": {
"subtitle": "kat",
"id": "0045100000yCqRfAAK",
"title": "#98640100"
},
"entityTitle": "Opportunities"
}
];
const transformation = Object.values(
data.reduce(( groups, item ) => {
const entityTitle = item.entityTitle;
if ( !groups[ entityTitle ] ) groups[ entityTitle ] = { entityTitle, "searchResults": [] };
groups[ entityTitle ].searchResults.push( item.metadata );
return groups;
}, {} )
);
console.log( transformation );
Use a chain and map the groups created by _groupBy. The search results within each group also need mapping to remove the metadata property
let res =
_.chain(data)
.groupBy('entityTitle')
.map(function(results, title) {
return {entityTitle: title, searchResults: _.map(results, 'metadata') };
})
.value();
console.log(res)
.as-console-wrapper {
max-height: 100%!important;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.9.1/underscore-min.js">
</script>
<script>
data = [{
"metadata": {
"subtitle": "kit",
"id": "0063400000yCqRfAAK",
"title": "#9864478"
},
"entityTitle": "Opportunities"
},
{
"metadata": {
"subtitle": "foo",
"id": "foo",
"title": "#foo"
},
"entityTitle": "Opportunities"
},
{
"metadata": {
"subtitle": "Acceleration",
"id": "0063400000yCqRfAAK",
"title": "#9882278"
},
"entityTitle": "Reports"
}
]
</script>