I want to use immutability-helper to update the object contained in my variable, the variable contains the following..
{
"_id": "XXXXX",
"_rev": "XXXXX",
"keys": {
"component": "TEST",
"type": "system"
},
"content": {
"services": {
"event": {
"url": "https://example.net"
},
"copy": {
"url": "https://example.net"
},
"humley": {
"url": "https://example.net",
"credentials": {
"user": "TEST",
"password": "TEST"
}
}
},
"settings": {
"processing": false,
"syncTimeout": {
"interval": 1,
"intervalUnit": "minutes"
},
"products": {
"hum": {
"copySyncDate": "2017-01-21T13:20:12.633Z",
"eventSyncDate": "",
"workspaceSyncDate": ""
}
}
},
"syncTimeout": {
"interval": 1,
"intervalUnit": "minutes"
}
}
}
I am looking to update the values
"hum": {
"copySyncDate": "2017-01-21T13:20:12.633Z",
"eventSyncDate": "",
"workspaceSyncDate": ""
I believe I can use the update function to update and store the new data in a new variable -
const documentUpdate = update(document, XXX);
But I can't work out what XXX needs to be, hope someone can help..
Thanks
You can do something like this:
const documentUpdate = update(
document,
{
content: {
settings: {
products: {
hum: {
$merge: {
copySyncDate: 'value',
}
}
}
}
}
},
)
You could use $set instead of $merge if you want to replace the entire block.
Related
I'm trying to convert the Cognito user attributes I get from CognitoIdentityServiceProvider listUsersInGroup to plain object but I didn't found any library or AWS function that does it... then I tried to implement it by myself
That's what I came up with:
{
...user,
Attributes: user.Attributes.map((x) => ({ [x.Name]: x.Value })),
}
But that makes an array with objects and I'm trying to create an object with all the attributes...
[
{
"sub": "dasfdasfd-vcfdgfd",
},
{
"website": "aba",
},
{
"address": "new",
},
]
here is an example of the user's data (the attributes can be different from user to user):
user a:
[
{
"Name": "sub",
"Value": "dasfdasfd-vcfdgfd",
},
{
"Name": "website",
"Value": "aba",
},
{
"Name": "address",
"Value": "new",
},
{
"Name": "email_verified",
"Value": "false",
},
{
"Name": "phone_number_verified",
"Value": "false",
}
]
user b:
[
{
"Name": "custom:age",
"Value": "0",
},
{
"Name": "custom:height",
"Value": "0",
},
{
"Name": "email",
"Value": "dsafdsa#gmail.com",
}
]
You can use reduce
{
...user,
Attributes: user.Attributes.reduce((acc, { Name, Value }) => ({...acc, [Name]: Value }), {}),
}
Seems pretty simple just use loop. FYI : Array's map function always returns the array
function getAttributes(data){
let attributes = {};
for(let x of data){
attributes[x["name"]] = x["value"];
}
return attributes;
}
{
...user,
Attributes: getAttributes(user.Attributes)
}
I am trying to "push" information inside the object based on certain conditions.
I need to insert/push/assign more information within the property "materials" but I am not sure how to do this.
This is the structure of my current object:
const MYOBJECT = {
"status": "Work",
"present": true,
"materials": [
{
"link1": {
"url": "www.google.com",
}
},
{
"driveFile": {
"driveFile": {
"id": "xyz123456fghtfsdag"
},
"shareMode": "VIEW"
}
},
],
"dueDate": {
"day": resource.date_day,
"month": resource.date_month,
"year": resource.date_year
},
}
I am trying to add/push, if a condition is met, the variables shown below ("additional1" & "additional2") inside the object property "materials" without replacing/deleting any other information within the object:
var additional1 = { "link2": { "url": "www.yahoo.com", } }
var additional2 = { "link3": { "url": "www.bing.com", } }
I am looking for the END result to look like this (assuming the "if statements" / conditions are met):
const MYOBJECT = {
"status": "Work",
"present": true,
"materials": [
{
"link1": {
"url": "www.google.com",
}
},
// Insert variable addition1
{
"link2": {
"url": "www.yahoo.com",
}
},
// Insert variable addition2
{
"link3": {
"url": "www.bing.com",
}
},
{
"driveFile": {
"driveFile": {
"id": "xyz123456fghtfsdag"
},
"shareMode": "VIEW"
}
},
],
"dueDate": {
"day": resource.date_day,
"month": resource.date_month,
"year": resource.date_year
},
}
materials is an array, so you can add new members there using the push() method.
The end result would look something like this:
if (conditionIsMet) {
MYOBJECT.materials.push(additional);
}
Please note, that the push() method adds element to the end of the array, so if you want your results to reside in the middle of the array (like you did in your example) you'll need to specify additional logic for this.
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.
How do i change a value in a variable ?
var elementorFrontendConfig = {
"environmentMode": {
"edit": false,
"wpPreview": false
},
"is_rtl": false,
"breakpoints": {
"xs": 0,
"sm": 480,
"md": 768,
"lg": 1025,
"xl": 1440,
"xxl": 1600
},
"version": "2.8.5",
"urls": {
"assets": "http:\/\/wptheme.tst\/wp-content\/plugins\/elementor\/assets\/"
},
"settings": {
"page": [],
"general": {
"elementor_global_image_lightbox": "yes"
},
"editorPreferences": []
},
"post": {
"id": 6007,
"title": "Elementor #6007",
"excerpt": ""
},
"user": {
"roles": ["administrator"]
}
};
This does not work
elementorFrontendConfig['settings']['general']['elementor_global_image_lightbox']= 'no';
The JS code is okay and does work. It might not be called, depending on where your <script> tag has been placed in the code.
this works
check the fiddle, may be there is something else wrong with your environment
var elementorFrontendConfig = {"environmentMode":{"edit":false,"wpPreview":false},"is_rtl":false,"breakpoints":{"xs":0,"sm":480,"md":768,"lg":1025,"xl":1440,"xxl":1600},"version":"2.8.5","urls":{"assets":"http:\/\/wptheme.tst\/wp-content\/plugins\/elementor\/assets\/"},"settings":{"page":[],"general":{"elementor_global_image_lightbox":"yes"},"editorPreferences":[]},"post":{"id":6007,"title":"Elementor #6007","excerpt":""},"user":{"roles":["administrator"]}};
elementorFrontendConfig['settings']['general']['elementor_global_image_lightbox']= 'no';
alert(elementorFrontendConfig['settings']['general']['elementor_global_image_lightbox']);
https://jsfiddle.net/pj704k3u/
I need to update the type, key and values in the 'fields' array if the componentId and the wfInstanceId id matches.
This is the document that i need to do so.
{
"_id": {
"$oid": "586b6d756937c22f207dd5af"
},
"wfInstanceId": "0111",
"workflowId": "ash3",
"folderURL": "ash3",
"teamName": "teamName",
"dataStream": [
{
"componentInstanceId": "componentInstanceId1",
"componentId": "componentId1",
"_id": {
"$oid": "586b6d756937c22f207dd5b0"
},
"fields": [
{
"type": "String",
"value": "value1",
"key": "key",
"_id": {
"$oid": "586b6d756937c22f207dd5b1"
}
},
{
"type": "String",
"value": "value2",
"key": "key1",
"_id": {
"$oid": "586b6d756937c22f207dd5b1"
}
}
]
},
{
"componentInstanceId": "componentInstanceId2",
"componentId": "componentId22",
"_id": {
"$oid": "586b6d756937c22f207dd5b0"
},
"fields": [
{
"type": "String",
"value": "value1",
"key": "key",
"_id": {
"$oid": "586b6d756937c22f207dd5b1"
}
},
{
"type": "String",
"value": "value2",
"key": "key2",
"_id": {
"$oid": "586b6d756937c22f207dd5b1"
}
}
]
}
],
"id": "38f356f0-d196-11e6-b0b9-3956ed7f36f0",
"__v": 0
}
I tried this like this, Also i tried $set over $push which is also doesn't work.
Model.findOneAndUpdate( {'wfInstanceId': '0111', 'dataStream.componentId': 'componentId1'}, {$push : { 'dataStream.fields.$.key' : 'sdsdds' }}, { upsert: true }, function (err, data) {
if (err) {
reject(err);
console.log('error occured' + err);
}
console.info("succesfully saved");
resolve (data);
});
As they describe in this DOC it shouldbe working for update method that is also didn't work for me. Any help will be really appreciated to overcome this issue i'm facing.
As you are using $ operator it updates only the first matched array element in each document.
As of now it is not possible in mongoose to directly update all array elements.
You can do this in your case:
db.collection.find({'wfInstanceId': '0111', 'dataStream.componentId': 'componentId1'})
.forEach(function (doc) {
doc.datastream.forEach(function (datastream) {
if (dataStream.componentId === componentId1) {
dataStream.fields.forEach(function(fields){
// you can also write condition for matching condition in field
dataStream.fields.key="";
dataStream.fields.value="";
dataStream.fields.type="";
}
}
});
db.collection.save(doc);
});
It is normal javascript code. I think it's clearer for mongo newbies.