Delete duplicate value from array of array - javascript

I have an object array and I want to avoid to duplicate value with a specific key value.
This is an objectArray.
var dept =
[
{
"department": [
{ "userName": "cds",
"userId": "33",
"userFirstname": "Chef",
"userLastname": "Jone"
},
{
"userName": "asset1",
"userId": "27",
"userFirstname": "Asset",
"userLastname": "Ann "
}
],
"comment": "",
"doc": null
},
{
"department": [
{
"userName": "audit1",
"userId": "32",
"userFirstname": "Audit",
"userLastname": "Kim"
},
{ "userName": "cds",
"userId": "33",
"userFirstname": "Chef",
"userLastname": "Jone"
}
],
"comment": "",
"doc": null
}
];
I tried to return the unduplicated object by javascript.
But the result seems different.
I can return the value from one object array. However, I feel difficulties to extract the values from array of array.
How to assign a filter array of an array?
Any help would be appreciated.
Thanks in advance.
function getUnique(dept, comp) {
const unique = dept
.map(e => e[comp])
// store the keys of the unique objects
.map((e, i, final) => final.indexOf(e) === i && i)
// eliminate the dead keys & store unique objects
.filter(e => dept[e]).map(e => dept[e]);
return unique;
}
return getUnique(dept,'userName');
I would get the result
[
{
"department": [
{ "userName": "cds",
"userId": "33",
"userFirstname": "Chef",
"userLastname": "Jone"
},
{
"userName": "asset1",
"userId": "27",
"userFirstname": "Asset",
"userLastname": "Ann "
}
],
"comment": "",
"doc": null
},
{
"department": [
{
"userName": "audit1",
"userId": "32",
"userFirstname": "Audit",
"userLastname": "Kim"
}
],
"comment": "",
"doc": null
}
];
However, I got the result
[
{
"department": [
{ "userName": "cds",
"userId": "33",
"userFirstname": "Chef",
"userLastname": "Jone"
},
{
"userName": "asset1",
"userId": "27",
"userFirstname": "Asset",
"userLastname": "Ann "
}
],
"comment": "",
"doc": null
}
]

You can use a combination of map and filter
var dept =[{"department": [{ "userName": "cds","userId": "33","userFirstname": "Chef","userLastname": "Jone"},{"userName": "asset1","userId": "27","userFirstname": "Asset","userLastname": "Ann "}],"comment": "","doc": null},{ "department": [{ "userName": "audit1","userId": "32","userFirstname": "Audit","userLastname": "Kim"},{ "userName": "cds","userId": "33","userFirstname": "Chef","userLastname": "Jone"}],"comment": "","doc": null}];
let tracker = {}
let op = dept.map(val => {
let department = val.department
let filtered = department.filter(({userId}) => {
if(tracker[userId] !== undefined ){
return false
} else {
tracker[userId] = userId
return true
}
})
return {...val,department: filtered}
})
console.log(op)

Related

How to get actual keys from MongoDB result array?

INPUT:
let array =[
{name:"xyz", email:"xyz#gmail.com", date:"22/10/2020" },
{name:"abc", email:"abc#gmail.com", date:"29/12/2020" },
{name:"efg", email:"efg#gmail.com", date:"20/01/2021" },
{name:"pqr", email:"pqr#gmail.com", date:"12/08/2020", age:"20" },
{name:"stu", email:"stu#gmail.com", date:"19/09/2020", age:"21" },
]
After performing certain operations I got the above array of objects as output from mongodb collection. From that array I want to get all keys and compare each and every object in an array and if any property present in any object not there in another object then I want to assign the property with empty string value.
To perform that operation I have written the code as shown below-
const keys = array.reduce(
(acc, curr) => (Object.keys(curr).forEach((key) => acc.add(key)), acc),
new Set()
);
const output = array.map((item) =>
[...keys].reduce((acc, key) => ((acc[key] = item[key] ?? ""), acc), {})
);
By performing above operations along with my original keys I am getting unnecessary keys like "$__", "$isNew", "_doc"
MY OUTPUT:
[
{
"$__": {
"activePaths": {
"paths": {
"_id": "init"
},
"states": {
"ignore": {},
"default": {},
"init": {
"_id": true
},
"modify": {},
"require": {}
},
"stateNames": [
"require",
"modify",
"init",
"default",
"ignore"
]
},
"strictMode": false,
"skipId": true,
"selected": {},
"fields": {},
"exclude": null
},
"$isNew": false,
"_doc": {
"_id": "62e6791b049e103f612f0882",
"name": "pqr",
"email": "pqr#gmail.com",
"date": "12/8/2020",
"age": "20",
},
"name": "pqr",
"email": "pqr#gmail.com",
"date": "12/8/2020",
"age": "20",
},
.
.
.
.
EXPECTED OUTPUT:
[
{
"name": "xyz",
"email": "xyz#gmail.com",
"date": "22/10/2020",
"age": ""
},
{
"name": "abc",
"email": "abc#gmail.com",
"date": "29/12/2020",
"age": ""
},
{
"name": "efg",
"email": "efg#gmail.com",
"date": "20/01/2021",
"age": ""
},
{
"name": "pqr",
"email": "pqr#gmail.com",
"date": "12/08/2020",
"age": "20"
},
{
"name": "stu",
"email": "stu#gmail.com",
"date": "19/09/2020",
"age": "21"
}
]
If I am using same code for normal array instead of resultant array from mongoodb I am getting correct output.
Please help me in resolving the above issue.
Query
if you have few fields and you know their names you can do it on server like bellow, in simple way
check if field (if missing it will be false) if its there keep old value, else add it with ""
the bellow doesn't work with boolean fields, if you have those you should check if type="missing" but in your case you dont have boolean fields so it will be ok
Playmongo
aggregate(
[{"$set":
{"name": {"$cond": ["$name", "$name", ""]},
"email": {"$cond": ["$email", "$email", ""]},
"date": {"$cond": ["$date", "$date", ""]},
"age": {"$cond": ["$age", "$age", ""]}}}])

How to check for duplicate data sets in JSON payload using JavaScript?

I would like to find duplicate data sets from below payload using combination of 'NAME', 'ID'. If a set exist more than 3 times, I need to return NAME, ID of duplicated data set.
{
"Test": "1",
"value": [
{
"NAME": "ABCD",
"ID": "1234",
"ACTIVE": "true"
},
{
"NAME": "EFGH",
"ID": "5678",
"ACTIVE": "true"
},
{
"NAME": "EFGH",
"ID": "5678",
"ACTIVE": "true"
},
{
"NAME": "EFGH",
"ID": "5678",
"ACTIVE": "true"
},
{
"NAME": "ABCD",
"ID": "1234",
"ACTIVE": "true"
},
{
"NAME": "ABCD",
"ID": "1234",
"ACTIVE": "true"
},
{
"NAME": "IJKL",
"ID": "91011",
"ACTIVE": "true"
}
]
}
Expected output:
["ABCD:1234", "EFGH:5678"]
Try this. You can improve this further by performance wise, if you work around a bit.
var data = {
"Test": "1",
"value": [
{
"NAME": "ABCD",
"ID": "1234",
"ACTIVE": "true"
},
{
"NAME": "ABCD",
"ID": "1234",
"ACTIVE": "true"
},
{
"NAME": "ABCD",
"ID": "1234",
"ACTIVE": "true"
},
{
"NAME": "EFGH",
"ID": "5678",
"ACTIVE": "true"
},
{
"NAME": "IJKL",
"ID": "91011",
"ACTIVE": "true"
}
]
};
var objArray = data.value;
var duplicates = []; // duplicates will be stored here.
for(var i=0, iLen = objArray.length; i<iLen;i++){
var obj = objArray[i];
var filtered = objArray.filter(function(arrVal) {
return arrVal.NAME === obj.NAME && arrVal.ID === obj.ID ;
});
var dup = obj.NAME + ":" + obj.ID;
if(filtered.length>=3 && duplicates.indexOf(dup) < 0) {
duplicates.push(dup);
}
}
this questuon was answered multiple times on SO. You create array from the json and compare the elements of the array.
How to remove all duplicates from an array of objects?
According to your sample output I believe it's "at least 3 times" rather than "more than 3 times".
Below snippet can product the expected output with the sample data.
const data = {
"Test": "1",
"value": [
{
"NAME": "ABCD",
"ID": "1234",
"ACTIVE": "true"
},
{
"NAME": "EFGH",
"ID": "5678",
"ACTIVE": "true"
},
{
"NAME": "EFGH",
"ID": "5678",
"ACTIVE": "true"
},
{
"NAME": "EFGH",
"ID": "5678",
"ACTIVE": "true"
},
{
"NAME": "ABCD",
"ID": "1234",
"ACTIVE": "true"
},
{
"NAME": "ABCD",
"ID": "1234",
"ACTIVE": "true"
},
{
"NAME": "IJKL",
"ID": "91011",
"ACTIVE": "true"
}
]
};
const occurrence = {}; // key: count
const result = [];
for (const item of data.value) {
const key = `${item.NAME}:${item.ID}`;
occurrence[key] = (occurrence[key] || 0) + 1;
if (occurrence[key] >= 3) {
result.push(key);
}
}
console.log(result);

how to remove sessionID:null value objects from the array nested inside the array of objects in js

[
{
"_id": "5edfb4e587a1873120735dcf",
"firstname": "abc",
"lastname": "abc",
"sessions": [
{
"_id": "5efc68d146d8330a449e7108",
"sessionID": null
},
{
"_id": "5efc68e646d8330a449e710a",
"sessionID": null
}
]
},
{
"_id": "5eedf5685bdb7d33c83186e7",
"firstname": "sam",
"lastname": "ple",
"sessions": [
{
"_id": "5efc692d46d8330a449e710c",
"sessionID": null
}
]
},
{
"_id": "5ef04df83e41dd5b78fe6908",
"firstname": "User",
"lastname": "name1",
"sessions": [
{
"_id": "5efc6a8846d8330a449e710e",
"sessionID": null
},
{
"_id": "5efc6abd46d8330a449e7110",
"sessionID": null
}
]
},
{
"_id": "5efe0d0c7300073244d765d9",
"sessions": [],
"firstname": "User",
"lastname": "name1"
}
]
You need to map over your array and filter out sessions with null
let originalArray = [
{
"_id": "5edfb4e587a1873120735dcf",
"firstname": "abc",
"lastname": "abc",
"sessions": [
{
"_id": "5efc68d146d8330a449e7108",
"sessionID": null
},
{
"_id": "5efc68e646d8330a449e710a",
"sessionID": null
}
]
},
.... the rest of your orignal object
]
let newArray = originalArray.map(item => {
let sessions = item.sessions.filter(session => {
return session.sessionID !== null
})
return {
...item,
sessions
}
})

get key:value pair inside sub-blocks

I have the below output from Postman or hitting end point(we can say).
{
"SearchResult": {
"total": 11,
"resources": [
{
"id": "12345",
"name": "GuestType",
"description": "Identity group ",
},
{
"id": "56789",
"name": "Admin",
"description": "",
},
]
}
}
I want to extract "id" and "name" from these values. I see the values are inside sub-blocks.
How to extract these key-value using java-script that need to be put in "Tests" tab in postman?
var obj={
"SearchResult": {
"total": 11,
"resources": [
{
"id": "12345",
"name": "GuestType",
"description": "Identity group ",
},
{
"id": "56789",
"name": "Admin",
"description": "",
},
]
}
}
obj.SearchResult.resources.forEach((o)=>console.log(o.id,o.name));
Below code will return array of objects with id and name only.... Happy coding :)
let data = {
"SearchResult":
{
"total": 11,
"resources": [
{ "id": "12345", "name": "GuestType", "description": "Identity group ", },
{ "id": "56789", "name": "Admin", "description": "", }
]
}
}
let ids = data.SearchResult.resources.map(obj => {
id: obj.id,
name: obj.name
});
Try
let ids = data.SearchResult.resources.map(obj => obj.id);
let names = data.SearchResult.resources.map(obj => obj.name);
let data={ "SearchResult": { "total": 11, "resources": [ { "id": "12345", "name": "GuestType", "description": "Identity group ", }, { "id": "56789", "name": "Admin", "description": "", }, ] } }
let ids = data.SearchResult.resources.map(obj => obj.id);
let names = data.SearchResult.resources.map(obj => obj.name);
console.log(ids);
console.log(names);

Need help in lodash combinations to get the below object

Below is my object First step is to check the socialAuth array of objects and get the platformName which has the showStats value as false.
I have accomplished the step1 as below
var arrList2 = _.pluck(_.where(creatorObj.socialAuth, {'showStats': false}), "platformName");
['Twitter'] is the output of the arrList2
var creatorObj =
{
"_id": "55e5b32f3874c964cc3dfe2e",
"firstName": "xyz",
"lastName": "abc",
"socialStats": [
{
"reach": 205976,
"_id": "asdfasdfasdf",
"profileUrl": null,
"engagements": 126,
"platformName": "Twitter"
}
],
"socialAuth": [
{
"screenName": "abc",
"userId": "12341234",
"_id": "55e5b3573874c964cc3dfe33",
"showStats": false,
"platformName": "Twitter"
},
{
"channelTitle": "xyz",
"channelId": "sdfgsdfgsdfg",
"_id": "55e5a040991c1321a5b9bd79",
"showStats": true,
"platformName": "Youtube"
}
]
};
Second step is to check the above arrList2 with the sociaStats array and remove the value from it and print the object again.
I need help in this second step.
I need the resulting object as
var creatorObj =
{
"_id": "55e5b32f3874c964cc3dfe2e",
"firstName": "xyz",
"lastName": "abc",
"socialStats": [],
"socialAuth": [
{
"screenName": "abc",
"userId": "12341234",
"_id": "55e5b3573874c964cc3dfe33",
"showStats": false,
"platformName": "Twitter"
},
{
"channelTitle": "xyz",
"channelId": "sdfgsdfgsdfg",
"_id": "55e5a040991c1321a5b9bd79",
"showStats": true,
"platformName": "Youtube"
}
]
};
You need to use _.remove() to remove the elements from array based on your condition.
Demo
var creatorObj = {
"_id": "55e5b32f3874c964cc3dfe2e",
"firstName": "xyz",
"lastName": "abc",
"socialStats": [{
"reach": 205976,
"_id": "asdfasdfasdf",
"profileUrl": null,
"engagements": 126,
"platformName": "Twitter"
}],
"socialAuth": [{
"screenName": "abc",
"userId": "12341234",
"_id": "55e5b3573874c964cc3dfe33",
"showStats": false,
"platformName": "Twitter"
}, {
"channelTitle": "xyz",
"channelId": "sdfgsdfgsdfg",
"_id": "55e5a040991c1321a5b9bd79",
"showStats": true,
"platformName": "Youtube"
}]
};
var arrList2 = _.pluck(_.where(creatorObj.socialAuth, {
'showStats': false
}), "platformName");
creatorObj.socialStats = _.remove(creatorObj.socialStats, function(n) {
return !_.includes(arrList2, n.platformName);
});
console.log(creatorObj);
document.write(JSON.stringify(creatorObj));
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/3.10.1/lodash.js"></script>

Categories

Resources