How to modify an object - javascript

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.

Related

cognito user attributes to plain object

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)
}

Traversing Object to return value

I have a value that I am searching for. I would like to match the term I am searching for to the value of the key and return the value for the key of name associated with the same object.
Here is a sample object:
{
"test1": {
"functions": {
"function1": {
"inputs": [
{
"key": "key1",
"name": "name1"
},
{
"key": "key2",
"name": "name3"
},
{
"key": "key3",
"name": "name3"
}
]
},
"function2": {
"inputs": [
{
"key": "key4",
"name": "name4"
},
{
"key": "key5",
"name": "name5"
},
{
"key": "key6",
"name": "name6"
}
]
}
}
}
}
Let's say I want to find the name of an input with a key of key4. How would I achieve this in javascript?
You only have to iterate the individual objects inside of the "inputs" array and check, whether the key property is "key4"
var inputs = []; // you would have to get the individual lists
inputs.forEach(function (input) {
if (input['key'] === 'key4') {
// do something with input['name'];
}
});
optimally you would make a function, that returns the input['name']

Extract an element value in json using jsonpath

Following is my json data
{
"primary": [
{
"secondary": {
"name": {
"fullname": "fullname1"
},
"alias": "alias1"
},
"reference": "reference1"
},
{
"secondary": {
"name": {
"fullname": "fullname2"
},
"alias": "alias2"
},
"reference": "reference2"
}
]
}
Now I want to extract "alias" value based on condition based fullname value from this json data using jsonpath.
I am using following expression but failed to parse result
$.primary[*].secondary[?(#.name.fullname == "fullname1")].alias
Your JSON is malformed, if it really looks like you posted - you won't be able to use JSON Extractor, you will have to switch to Regular Expression Extractor.
Valid JSON would be something like:
{
"primary": [
{
"secondary": {
"name": {
"fullname": "fullname1"
},
"alias": "alias1"
},
"reference": "reference1"
},
{
"secondary": {
"name": {
"fullname": "fullname2"
},
"alias": "alias2"
},
"reference": "reference2"
},
{
"type": "online"
}
]
}
And if it is correct your JsonPath expression works fine as it evidenced by JsonPath Tester mode of the View Results Tree listener:

Re-arrage JSON values from existing values

The JSON provided is kind of unstructured and doesn't meet many of my
requirements. I have tried this many ways but does take a very long time
when I provide 100,000 records
Implemented Code
for (var f in stack.data) {
var field = new Object();
for (var o in stack.data[f]['field_values']) {
field[stack.data[f]['field_values'][o]['field_name']] = stack.data[f]['field_values'][o]['value'];
}
stack.data[f]['field_values'] = field;
}
console.log(JSON.stringify(stack, null, 2));
Input JSON:
var stack = {
"data": [{
"id": 950888888073,
"name": "www.stackoverflow.com",
"field_values": [{
"field_name": "Newsletter?",
"value": true
},
{
"field_name": "Parent",
"value": 950888661
},
{
"field_name": "Birthday",
"value": "2018-04-29"
},
{
"field_name": "Related matter",
"value": 1055396205
},
{
"field_name": "Referral",
"value": "Don Ho"
},
{
"field_name": "Spouse",
"value": "Wo Fat"
}
]
}]
}
Expected Output:
{
"data": [
{
"id": 950888888073,
"name": "www.stackoverflow.com",
"field_values": {
"Newsletter?": true,
"Parent": "Gigi Hallow",
"Birthday": "2018-04-29",
"Related": "2012-00121-Sass",
"Referral": "Don Ho",
"Spouse": "Wo Fat"
}
Sometimes "field_values can be empty. Need to check them as well
{
"id": 950821118875,
"name": "www.google.com",
"field_values": [],
}
This is mostly re-arranging the values. Here values becomes keys. There should actually be one liner to handle this, but i am run out of options.
Hope the question is clear
It would probably help to declare a variable to hold the array element, rather than doing 4 levels of indexing every time through the loop. You can also use destructuring to extract the properties of the object.
And use {} rather than new Object.
Even if this doesn't improve performance, it makes the code easier to read.
var stack = {
"data": [{
"id": 950888888073,
"name": "www.stackoverflow.com",
"field_values": [{
"field_name": "Newsletter?",
"value": true
},
{
"field_name": "Parent",
"value": 950888661
},
{
"field_name": "Birthday",
"value": "2018-04-29"
},
{
"field_name": "Related matter",
"value": 1055396205
},
{
"field_name": "Referral",
"value": "Don Ho"
},
{
"field_name": "Spouse",
"value": "Wo Fat"
}
]
}]
}
for (var f in stack.data) {
const field = {};
const fobj = stack.data[f];
for (var o in fobj.field_values) {
const {field_name, value} = fobj.field_values[o];
field[field_name] = value;
}
fobj.field_values = field;
}
console.log(JSON.stringify(stack, null, 2));

How to update value X?

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.

Categories

Resources