Insert in middle of a Object in nodeJS - node.js

I have a object and I need insert a object in the middle.
I tried a push and sort, but dosenĀ“t work...
this is my code:
let myObject = [
{
'item1' : 'string1',
'item2' : 'string2',
'item3' : 'string3',
'item4' : 'string4',
'item5' : 'string5',
'item6' : 'string6',
'item7' : 'string7',
'item8' : 'string8',
'item10' : 'string10',
'item11' : 'string11',
'item12' : 'string12',
'item14' : 'string14',
'item15' : 'string15'
},
{
'item16' : 'string16',
'item17' : 'string17',
'item18' : 'string18',
'item19' : 'string19',
'item20' : 'string20',
'item21' : 'string21',
'item22' : 'string22',
'item23' : 'string23',
'item25' : 'string25',
'item26' : 'string26',
'item27' : 'string27',
'item29' : 'string29',
'item30' : 'string30'
}
]
myObject[0].item9 = 'string9'
myObject[0].item13 = 'string13'
console.log(myObject[0])
//OUTPUT
// {
// item1: 'string1',
// item2: 'string2',
// item3: 'string3',
// item4: 'string4',
// item5: 'string5',
// item6: 'string6',
// item7: 'string7',
// item8: 'string8',
// item10: 'string10',
// item11: 'string11',
// item12: 'string12',
// item14: 'string14',
// item15: 'string15',
// item9: 'string9',
// item13: 'string13'
// }
myObject = Object.fromEntries(Object.entries(myObject[0]).sort())
console.log(myObject)
//OUTPUT
// {
// item1: 'string1',
// item10: 'string10',
// item11: 'string11',
// item12: 'string12',
// item13: 'string13',
// item14: 'string14',
// item15: 'string15',
// item2: 'string2',
// item3: 'string3',
// item4: 'string4',
// item5: 'string5',
// item6: 'string6',
// item7: 'string7',
// item8: 'string8',
// item9: 'string9'
// }
How can I do that OUTPUT:
{
item1: 'string1',
item2: 'string2',
item3: 'string3',
item4: 'string4',
item5: 'string5',
item6: 'string6',
item7: 'string7',
item8: 'string8',
item9: 'string9',
item10: 'string10',
item11: 'string11',
item12: 'string12',
item13: 'string13',
item14: 'string14',
item15: 'string15'
}

That would be a solution that came to my head but i'm sure that there is a more efficient and sexy solution to this problem. The problem is that you would need to convert it back to an object from the array. Should work, I didn't came to test it just wrote it quick in my notepad :).
let myObject = [
{
'item1' : 'string1',
'item2' : 'string2',
'item3' : 'string3',
'item4' : 'string4',
'item5' : 'string5',
'item6' : 'string6',
'item7' : 'string7',
'item8' : 'string8',
'item10' : 'string10',
'item11' : 'string11',
'item12' : 'string12',
'item14' : 'string14',
'item15' : 'string15'
},
{
'item16' : 'string16',
'item17' : 'string17',
'item18' : 'string18',
'item19' : 'string19',
'item20' : 'string20',
'item21' : 'string21',
'item22' : 'string22',
'item23' : 'string23',
'item25' : 'string25',
'item26' : 'string26',
'item27' : 'string27',
'item29' : 'string29',
'item30' : 'string30'
}
]
var result = Object.keys(myObject[0]).map(e => ({item: e, string: myObject[0][e]}))
result.splice(8, 0, {item: 'item9', word: 'string9'});
result.splice(12, 0, {item: 'item13', word: 'string13'});
//result would be an array of objects with params 'item' and 'array'
console.log(result)

Related

Recursive function type based on object keys

Im trying to create a recursive function type that takes the outer most keys in an object type as an argument and returns a new function that then takes the next outer most keys of the object from the given key recursively until no more keys are available.
If the key doesn't extend /${string} the argument must be of an object type containing the key ans and property:
type Argument<T> = T extends `/${string}` ? T : { [key in T]: string | number }
There will only be one key that wont extend /${string} per. level.
Example
const values = {
'/aaa': {
'/lorem': {
foo: 'hello world',
'/boo': 12345,
},
},
bbb: {
'/ipsum': {
'/dolor': 'lorem ipsum',
amet: 567890,
},
},
'/ccc': ...
};
foo<typeof values>('/aaa')('/lorem')({ foo: '...' }); // should return type of string
foo<typeof values>('/aaa')('/lorem')('/boo'); // should return type of number
foo<typeof values>('/aaa')('/ipsum'); // should fail
foo<typeof values>({ bbb: '...' })('/ipsum')({ amet: '...' }); // should return type of number
foo<typeof values>({ bbb: '...' })('/ipsum')('/dolor'); // should return type of string
foo<typeof values>({ bbb: '...' })('/lorem'); // should fail
My current code
I have a type that almost does the job., but doesn't work with the non /${string} extensions :(
type Foo<T extends object> = <K extends keyof T>(args: K) => T[K] extends object ? Foo<T[K]> : T[K];
const values = {
'/aaa': {
'/lorem': {
foo: 'hello world',
'/boo': 12345,
},
},
bbb: {
'/ipsum': {
'/dolor': 'lorem ipsum',
amet: 567890,
},
},
};
const foo = {} as Foo<typeof values>
foo('/aaa')('/lorem')('/boo') // works :D
foo('/aaa')('/lorem')({ foo: '...' }) // fails :(
I tried to handle the args type within the function - But it can't return the next keys :(
type Foo<T extends object> = <K extends keyof T>(
args: K extends `/${string}` ? K : { [key in K]: string | number }
) => T[K] extends object ? Foo<T[K]> : T[K];
const values = {
'/aaa': {
'/lorem': {
foo: 'hello world',
'/boo': 12345,
},
},
bbb: {
'/ipsum': {
'/dolor': 'lorem ipsum',
amet: 567890,
},
},
};
const foo = {} as Foo<typeof values>
foo('/aaa')('/lorem')('/boo') // works :D
const a = foo({ bbb: '...' })('/ipsum') // fails :(
I don't even know if want I'm trying is possible - but if you have any suggestions you would save my day :)
Thanks for your time.
Hmm, problem seems to be with {[key in K]: string | number}, apparently typescript does not know if T[K] is an object after that.
If you change it to {[key: string]: K} it works (but {[key: string|number]: K} does not). This seems like an issue with Typescript (I am using version 4.7.4.)
Best I can do is using a second parameter instead of an object:
type Foo<T extends object> = <K extends keyof T>(
arg: K,
val?: K extends `/${string}` ? undefined : string|number
) => T[K] extends object ? Foo<T[K]> : T[K];
const foo = {} as Foo<typeof values>
const boo = foo('/aaa')('/lorem')('/boo') // TS: boo is number
const ipsum = foo('bbb', 12)('/ipsum') // TS: ipsum is Foo<{'/dolor': string, amet: number}>

Error : REQUIRED_FIELD_MISSING, missing required field: [objectId]: [objectId] on below code

Code :
Public static void IncentiveCompAutoSubmitApproval(List tickets)
{
Approval.ProcessSubmitRequest req1 = new Approval.ProcessSubmitRequest();
for(Case cs : tickets)
{
req1.setComments('Submitting request for approval.');
req1.setObjectId(cs.Id);
req1.setSubmitterId(UserInfo.getUserId());
req1.setProcessDefinitionNameOrId('Incentive_Compensation_Tier_1_Approvals');
}
Approval.ProcessResult result = Approval.process(req1);

Aerospike - add new key/value pair in nested object

In Aerospike, how can I add a new key/value pair in nested object stored in bins of type map?
For ex,
I have a bins of type map against which I need to store below key/value pairs.
{
"a" : "apple",
"b" : "ball",
"c" : { "d" : "dog", "e" : "elephant" },
"f" : { "g" : { "h" : "horse" } },
"i" : { "j" : "jackal", "k" : { "l" : "lion", "m" : "monkey" } }
}
Now, I want to update an existing nested object against key "k" to add one more key value pair like below.
"k" : { "l" : "lion", "m" : "monkey", "n" : "nest" }
Final result should be like below.
{
"a" : "apple",
"b" : "ball",
"c" : { "d" : "dog", "e" : "elephant" },
"f" : { "g" : { "h" : "horse" } },
"i" : { "j" : "jackal", "k" : { "l" : "lion", "m" : "monkey", "n" : "nest" } }
}
Any suggestions on how to achieve this?
It's a NodeJS (10.6.0) application & I'm using NodeJS aerospike client (3.6.1) to interact with Aerospike (4.3.0.7).
Updates on nested CDT maps and lists are possible now, using Aerospike server version 4.6 or later and Aerospike Node.js client version 3.12 or later. That update introduced the withContext() function on list and map operations that lets you specify the context in which the list/map operation is to be executed. You can find more information in the documentation for the new CdtContext class.
Here is how you would perform the update given in your example:
const Aerospike = require('aerospike')
const maps = Aerospike.maps
Aerospike.connect().then(async (client) => {
const key = new Aerospike.Key('test', 'test', 'test')
const map = {
"a" : "apple",
"b" : "ball",
"c" : { "d" : "dog", "e" : "elephant" },
"f" : { "g" : { "h" : "horse" } },
"i" : { "j" : "jackal", "k" : { "l" : "lion", "m" : "monkey" } }
}
console.log('BEFORE:', map)
await client.put(key, map)
await client.operate(key, [
maps.put('i', 'n', 'nest')
.withContext((ctx) => ctx.addMapKey('k'))
])
const record = await client.get(key)
console.log('AFTER:', record.bins)
client.close()
}).catch((error) => {
console.error(error)
if (error.client) error.client.close()
})
Here is what you would get, when you run the example:
$ node nested-map-update-example.js
BEFORE: {
a: 'apple',
b: 'ball',
c: { d: 'dog', e: 'elephant' },
f: { g: { h: 'horse' } },
i: { j: 'jackal', k: { l: 'lion', m: 'monkey' } }
}
AFTER: {
a: 'apple',
b: 'ball',
c: { d: 'dog', e: 'elephant' },
f: { g: { h: 'horse' } },
i: { j: 'jackal', k: { l: 'lion', m: 'monkey', n: 'nest' } }
}
You will have to update the full value for the key "i".

Return object with dynamic keys in AQL

Can I return something like:
{
"c/12313" = 1,
"c/24223" = 2,
"c/43423" = 3,
...
}
from an AQL query? The idea is something like (this non-working code):
for c in my_collection
return { c._id : c.sortOrder }
where sortOrder is some property on my documents.
Yes, it is possible to have dynamic attribute names:
LET key = "foo"
LET value = "bar"
RETURN { [ key ]: value } // { "foo": "bar" }
An expression to compute the attribute key has to be wrapped in [ square brackets ], like in JavaScript.
This doesn't return quite the desired result however:
FOR c IN my_collection
RETURN { [ c._id ]: c.sortOrder }
[
{ "c/12313": 1 },
{ "c/24223": 2 },
{ "c/43423": 3 },
...
]
To not return separate objects for every key, MERGE() and a subquery are required:
RETURN MERGE(
FOR c IN my_collection
RETURN { [ c._id ]: c.sortOrder }
)
[
{
"c/12313": 1,
"c/24223": 2,
"c/43423": 3,
...
}
]

How can I pass object properties to onSyndicationSuccess event while using SMF.Net.WebClient dynamically

I'm trying to create a central function for dynamic web requests.
function makeWebRequest(remoteURL, requestString, callBackFunction) {
var myWebRequest = new SMF.Net.WebClient({
url : remoteURL,
httpMethod : "POST",
requestString : requestString,
requestHeaders : [
"Content-Type: application/x-www-form-urlencoded"],
onSyndicationSuccess : callBackFunction,
onServerError : function (e) {
alert(e);
}
});
myWebRequest.run(false);
}
While calling makeWebRequest, passing a callBackFunction to it like;
var remoteURL = "http://parse.com/12/test";
var requestString = "category=news&type=world";
function callBackFunction(e) {
responseText = this.responseText;
if (responseText != null) {
parsedJSON = JSON.parse(responseText);
}
}
makeWebRequest(remoteURL,requestString,callBackFunction);
Application raises an error at line
responseText = this.responseText;
How can I pass myWebRequest itself to a function like that?
I used your codeLines. I just add a textButton to Page1, and it works fine both for Android and iOS .
In Global.js;
function makeWebRequest(remoteURL, requestString, callBackFunction) {
var myWebRequest = new SMF.Net.WebClient({
url : remoteURL,
httpMethod : "POST",
requestString : requestString,
requestHeaders : [
"Content-Type: application/x-www-form-urlencoded"],
onSyndicationSuccess : callBackFunction,
onServerError : function (e) {
alert(e);
}
});
myWebRequest.run(false);
}
var remoteURL = "http://parse.com/12/test";
var requestString = "category=news&type=world";
function callBackFunction(e) {
var responseText = this.responseText;
alert(responseText);
if (responseText != null) {
parsedJSON = JSON.parse(responseText);
}
}
function Global_Events_OnStart(e) {
changeLang(Device.language, true);
include("BC.js"); //included for future BC support. Removing is not advised.
// Comment following block for navigationbar/actionbar sample. Read the JS code file for usage.
// Also there is a part of code block in Page1, which should be copied to every page for HeaderBar usage
load("HeaderBar.js");
header = new HeaderBar();
// Uncomment following block for menu sample. Read the JS code file for usage.
/*
load("Menu.js");
/**/
}
function Global_Events_OnError(e) {
switch (e.type) {
case "Server Error":
case "Size Overflow":
alert(lang.networkError);
break;
default:
SES.Analytics.eventLog("error", JSON.stringify(e));
//change the following code for desired generic error messsage
alert({
title : lang.applicationError,
message : e.message + "\n\n*" + e.sourceURL + "\n*" + e.line + "\n*" + e.stack
});
break;
}
}
In Page1.js;
function Page1_Self_OnKeyPress(e) {
if (e.keyCode === 4) {
Application.exit();
}
}
function Page1_Self_OnShow() {
//Comment following block for removing navigationbar/actionbar sample
//Copy this code block to every page onShow
header.init(this);
header.setTitle("Page1");
header.setRightItem("RItem");
header.setLeftItem();
/**/
}
function Page1_TextButton1_OnPressed(e){
makeWebRequest(remoteURL,requestString,callBackFunction);
}
it works fine. Check your makeWebRequest function, must be on Global.js. Also define "responseText" variable with "var".

Resources