Adding attributes to existing json payload using javascript - javascript

I am getting json response as below. I want to add "**
schemas":[
"urn:ietf:params:scim:schemas:core:2.0:User", "urn:ietf:params:scim:schemas:extension:enterprise:2.0:User"
],
"id":"xyz",
**
to each resource how to do this using javascript. Can anyone help.
{
"Resources": [
{
"emails": {
"type": "bobsmith01#company.com",
"value": "Personal"
},
"name": {
"familyName": "Bob-update3",
"givenName": "Smith"
},
"detail": "SUCCESS",
"userName": "bobsmith01#company.com",
"status": "200"
},
{
"emails": {
"type": "samgomes#company.com",
"value": "Personal"
},
"name": {
"familyName": "gomes",
"givenName": "sam"
},
"detail": "SUCCESS",
"userName": "samgomes#company.com",
"status": "200"
}
]
}
Required output is as below.
{
"Resources": [
{
"schemas":[
"urn:ietf:params:scim:schemas:core:2.0:User",
"urn:ietf:params:scim:schemas:extension:enterprise:2.0:User"
],
"id":"xyz",
"emails": {
"type": "bobsmith01#company.com",
"value": "Personal"
},
"name": {
"familyName": "Bob-update3",
"givenName": "Smith"
},
"detail": "SUCCESS",
"userName": "bobsmith01#company.com",
"status": "200"
}
{
"schemas":[
"urn:ietf:params:scim:schemas:core:2.0:User",
"urn:ietf:params:scim:schemas:extension:enterprise:2.0:User"
],
"id":"abc",
"emails": {
"type": "samgomes#company.com",
"value": "Personal"
},
"name": {
"familyName": "gomes",
"givenName": "sam"
},
"detail": "SUCCESS",
"userName": "samgomes#company.com",
"status": "200"
}
]
}

You can use ES6 JS spread operator (...) and Array.map to achieve this:
const orgObject = {
"Resources": [{
"emails": {
"type": "bobsmith01#company.com",
"value": "Personal"
},
"name": {
"familyName": "Bob-update3",
"givenName": "Smith"
},
"detail": "SUCCESS",
"userName": "bobsmith01#company.com",
"status": "200"
},
{
"emails": {
"type": "samgomes#company.com",
"value": "Personal"
},
"name": {
"familyName": "gomes",
"givenName": "sam"
},
"detail": "SUCCESS",
"userName": "samgomes#company.com",
"status": "200"
}
]
}
const copiedObject = {...orgObject}; // Copy object to avoid mutation preferably use lodash.cloneDeep
// loop through the object.Resources using map
copiedObject.Resources = copiedObject.Resources.map(res => {
return {
"schemas": [
"urn:ietf:params:scim:schemas:core:2.0:User",
"urn:ietf:params:scim:schemas:extension:enterprise:2.0:User"
],
"id": "xyz",
...res // Use spread operator to copy the existing object properties from Resources
}
});
console.log(copiedObject)
.as-console-wrapper{
top:0;
max-height:100% !important;
}

Related

How to add "collection" field to metadata for Solana NFTs with Visual Studio Code

I'm trying to add the collection field (name and family) to my metadata using VSC. Not sure where to begin or how to do it. Any help appreciated. Thanks!
{
"name": "Solflare X NFT",
"symbol": "",
"description": "Celebratory Solflare NFT for the Solflare X launch",
"seller_fee_basis_points": 0,
"image": "https://www.arweave.net/abcd5678?ext=png",
"animation_url": "https://www.arweave.net/efgh1234?ext=mp4",
"external_url": "https://solflare.com",
"attributes": [
{ "trait_type": "web", "value": "yes" },
{ "trait_type": "mobile", "value": "yes" },
{ "trait_type": "extension", "value": "yes" }
],
"collection": { "name": "Solflare X NFT", "family": "Solflare" },
"properties": {
"files": [
{
"uri": "https://www.arweave.net/abcd5678?ext=png",
"type": "image/png"
},
{
"uri": "https://watch.videodelivery.net/9876jkl",
"type": "unknown",
"cdn": true
},
{ "uri": "https://www.arweave.net/efgh1234?ext=mp4", "type": "video/mp4" }
],
"category": "video",
"creators": [
{ "address": "SOLFLR15asd9d21325bsadythp547912501b", "share": 100 }
]
}
}

remove items from one object those are matched with other object items

i have an object structure is like this as below
"designProjects": [
{
"projectNumber": "number1",
"name": "test1"
},
{
"projectNumber": "number2",
"name": "test2"
},
{
"projectNumber": "number3",
"name": "test3"
},
]
i have another object that is having structure like this as below
"allProjects": [
{
"project": {
"name": "test1",
"number": "number1"
},
"employee": {
"displayName": "name1"
},
"projectRoleName": "Editor"
},
{
"project": {
"name": "test2",
"number": "number2"
},
"employee": {
"displayName": "name2"
},
"projectRoleName": "Editor"
},
]
I am kind of looking the results like as below
"designProjects": [
{
"projectNumber": "number3",
"name": "test3"
},
]
here the results is designProjects is only having one because the project number and name matched with project array of allprojects object. Is there way we can achieve this results in react js.
Any suggestions or ideas would be very grateful to me, Many thanks in advance
You would just combine .filter with .some, something like:
let d = {
"designProjects": [
{
"projectNumber": "number1",
"name": "test1"
},
{
"projectNumber": "number2",
"name": "test2"
},
{
"projectNumber": "number3",
"name": "test3"
},
]
}
let a = {
"allProjects": [
{
"project": {
"name": "test1",
"number": "number1"
},
"employee": {
"displayName": "name1"
},
"projectRoleName": "Editor"
},
{
"project": {
"name": "test2",
"number": "number2"
},
"employee": {
"displayName": "name2"
},
"projectRoleName": "Editor"
},
]
};
console.log(
d.designProjects.filter((designProject) => {
return !a.allProjects.some((project) => designProject.projectNumber === project.project.number && designProject.name === project.project.name);
})
);
You can combine filter and some
filter is used to return a new filtered array based on a condition
some will be the condition and return as soon as it finds a match
const designProjects = [{
"projectNumber": "number1",
"name": "test1"
},
{
"projectNumber": "number2",
"name": "test2"
},
{
"projectNumber": "number3",
"name": "test3"
},
];
const allProjects = [{
"project": {
"name": "test1",
"number": "number1"
},
"employee": {
"displayName": "name1"
},
"projectRoleName": "Editor"
},
{
"project": {
"name": "test2",
"number": "number2"
},
"employee": {
"displayName": "name2"
},
"projectRoleName": "Editor"
},
]
const cleaned = designProjects.filter((x) => {
return !allProjects.some(y => y.project.number === x.projectNumber);
});
console.info(cleaned);

Paasing an object into an array of arrays

Have an object that needs to be pass inside the array of source using javascript,throwing an error spec is undefined when using push function. will push function work for this scenario
var a = [
"name": "ben",
"type": "male",
"appType": "human",
"spec": {
"view": "instanceview",
"sink": {
"source": [{
"data": {
"path": "google/path",
"name": "test",
"Id": "11234",
},
"ref": "www.xyz.com",
"id": "isdfjsbfjsfb",
"resourceType": "app"
}
],
},
},
}]
var b = {
"data": {
"path": "google/path",
"name": "goldengate",
"Id": "11234vndslknvlsmnv",
},
"ref": "www.xyz.com",
"id": "6452367e5375",
"resourceType": "app"
}
a.spec.sink.source.push(b);
would expect b to be pushed to source
An array with string keys is not a valid structure, you need to convert a to an object
var a = { // <-- here
"name": "ben",
"type": "male",
"appType": "human",
"spec": {
"view": "instanceview",
"sink": {
"source": [
{
"data": {
"path": "google/path",
"name": "test",
"Id": "11234",
},
"ref": "www.xyz.com",
"id": "isdfjsbfjsfb",
"resourceType": "app"
}
],
},
},
} // <-- here

Find specific array, deep in a nested Mongo collection

I have a nested Mongo collection which looks like the following:
{
"_id": "BaiD76JR3gZFPKchn",
"ownerAdmin": "NsgdNHh84XrBJALAK",
"activities": [
{
"id": "a1",
"email": "itsarmin#gmail.com",
"type": "image",
"description": "Another one",
"createdAt": "2017-10-25T21:39:44.769Z",
"pins":
"action": "https://www.dropbox.com/as"
},
{
"id": "a2",
"email": "itsarmin#gmail.com",
"type": "image",
"description": "Comment This image For me",
"createdAt": "2017-10-25T18:42:00.484Z",
"action": "https://www.dropbox.com/s/bty2ds"
},
{
"id": "a3"
"email": "armin#four1five.com",
"type": "progress",
"description": "dasd",
"createdAt": "2017-10-24T17:28:11.748Z",
"action": 50
}
],
"createdAt": "2017-10-24T17:27:54.828Z"
}
I'm trying to query using Collection.findOne("BaiD76JR3gZFPKchn",), to return an object in "activities" using 2 field "id:a2" AND "type:image", so I end up with :
{
"id": "a2",
"email": "itsarmin#gmail.com",
"type": "image",
"description": "Comment This image For me",
"createdAt": "2017-10-25T18:42:00.484Z",
"action": "https://www.dropbox.com/s/bty2ds"
}
Try adding projection into your query.
db.collection.findOne( { _id: "BaiD76JR3gZFPKchn" },
{ activities: { $elemMatch: { id: "a2",type:"image"} }, _id: 0 })

Sort nested array by properties [duplicate]

This question already has answers here:
Sort array of objects by string property value
(57 answers)
Closed 8 years ago.
I have this array and I was wondering if I could sort it with the properties comments.count and likes.count separately.
For example I could just have a function to call using likes.count parameter or comments.count parameter.
{
"attribution": null,
"tags": [
],
"type": "",
"location": null,
"comments": {
"count": 2,
"data": [
{
"created_time": "1385670850",
"text": "Holy shit",
"from": {
"username": "someone",
"profile_picture": "http://url.com/profiles/profile_191120775_75sq_1365830292.jpg",
"id": "191120775"
},
"id": "599372997379438683"
},
{
"created_time": "1385680581",
"text": "",
"from": {
"username": "someone",
"profile_picture": "http://url.com/profiles/profile_1523167_75sq_1389544912.jpg",
"id": "1523167"
},
"id": "599454624038205610"
}
]
},
"likes": {
"count": 6,
"data": [
{
"username": "someone",
"profile_picture": "http://url.com/profiles/profile_2169761_75sq_1389075971.jpg",
"id": "2169761"
},
{
"username": "rashmityagi",
"profile_picture": "http://url.com/profiles/profile_393770264_75sq_1388911033.jpg",
"id": "393770264"
},
{
"username": "tylerferweda",
"profile_picture": "http://url.com/profiles/profile_191120775_75sq_1365830292.jpg",
"id": "191120775"
},
{
"username": "cbolts",
"profile_picture": "http://url.com/profiles/profile_1523167_75sq_1389544912.jpg",
"id": "1523167"
}
]
}
},
{
"attribution": null,
"tags": [
],
"type": "",
"location": null,
"comments": {
"count": 10,
"data": [
{
"created_time": "1385670850",
"text": "Holy shit",
"from": {
"username": "someone",
"profile_picture": "http://url.com/profiles/profile_191120775_75sq_1365830292.jpg",
"id": "191120775"
},
"id": "599372997379438683"
},
{
"created_time": "1385680581",
"text": "",
"from": {
"username": "someone",
"profile_picture": "http://url.com/profiles/profile_1523167_75sq_1389544912.jpg",
"id": "1523167"
},
"id": "599454624038205610"
}
]
},
"likes": {
"count": 20,
"data": [
{
"username": "someone",
"profile_picture": "http://url.com/profiles/profile_2169761_75sq_1389075971.jpg",
"id": "2169761"
},
{
"username": "rashmityagi",
"profile_picture": "http://url.com/profiles/profile_393770264_75sq_1388911033.jpg",
"id": "393770264"
},
{
"username": "tylerferweda",
"profile_picture": "http://url.com/profiles/profile_191120775_75sq_1365830292.jpg",
"id": "191120775"
},
{
"username": "cbolts",
"profile_picture": "http://url.com/profiles/profile_1523167_75sq_1389544912.jpg",
"id": "1523167"
}
]
}
},
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort
json_array.sort(function(a, b) {
if(a.comments.count < b.comments.count) {
return -1;
}
if(a.comments.count > b.comments.count) {
return 1;
}
return 0;
});
You can also modify it for likes.count
Underscore.js provides great utility functions for working with arrays and objects. For your case you could use _.sortBy(...) See http://underscorejs.org/#sortBy for more details. For simple object properties it is enough to specify the object property name als last parameter.

Categories