I can't use .insert to add object into array with Dart - arrays

I'm trying to insert an object into array of objects. The idea is just add the content of the object if the object already exist but I don't understand why I can't use the .insert
I have tried using the .insert and adding the object into the array.
void main() {
var arr = [
{
"shelf1": [
{
"Entry": "something",
"Status": "",
"Account": "",
"Address": "",
"Result": null
}
]
},
{
"shelf2": [
{
"Entry": "something",
"Status": "",
"Account": "",
"Address": "",
"Result": null
}
]
},
{
"shelf3": [
{
"Entry": "something",
"Status": "",
"Account": "",
"Address": "",
"Result": null
}
]
}
];
var object = {
"shelf1": {
"Entry": "something2",
"Status": "",
"Account": "",
"Address": "",
"Result": null
}
};
arr.insert(0, object);
print(arr);
}

The arr has a type of List<Map<String, Map<String, List<Map<String, String>>>>> and the object you declared has a type of Map<String, Map<String, String>>. This is because using var, the type is automatically inferred based on the assigned value. Since you're trying to add an object of an incorrect type an error is thrown.
So your new object should be made like the following to be inserted normally:
var object = {
"shelf1": [{ // note that I made this inside a List []
"Entry": "something2",
"Status": "",
"Account": "",
"Shelf": "shelf",
"Address": "",
"Result": null
}]
};

Related

Adding elements within Object TypeScript

Hello everyone i have dynamically expanding object which consists of a 2 x dimensional array. I
try to add in field label:{ } new key:value {"hg":"Hg"} object.
object JSON:
[
{
"callbackQueryData": "tNLQy3VcX",
"method": {
"value": "",
"property": "",
"linkUrl": "",
"inside": null,
"type": "NEXT_PAGE",
"nextId": "Cll8xZbVo6",
"validation": null,
"calendar": null,
"condition": null,
"api": null,
"pagination": null
},
"label": {
"en": "New button"
}
},
{
"callbackQueryData": "qntufUVhz",
"label": {
"en": "New button"
},
"method": {
"value": "",
"property": "",
"linkUrl": "",
"inside": null,
"type": "NEXT_PAGE",
"nextId": "",
"validation": null,
"calendar": null,
"condition": null,
"api": null,
"pagination": null
}
}
],
[
{
"callbackQueryData": "cx46ECYG9",
"label": {
"en": "New button"
},
"method": {
"value": "",
"property": "",
"linkUrl": "",
"inside": null,
"type": "NEXT_PAGE",
"nextId": "",
"validation": null,
"calendar": null,
"condition": null,
"api": null,
"pagination": null
}
}
],
[
{
"callbackQueryData": "uHp5yd3Li",
"label": {
"en": "New button"
},
"method": {
"value": "",
"property": "",
"linkUrl": "",
"inside": null,
"type": "NEXT_PAGE",
"nextId": "",
"validation": null,
"calendar": null,
"condition": null,
"api": null,
"pagination": null
}
}
],
[]
]
I try cast it to simple array and via forEach() addressing everyone elements button:any and to add object which i need. But Spread syntax(...) can't find argument forEach().Or I'm doing it completely wrong.
let arrayButton:IBotButton[][] = ([] as IBotButton[][]).concat(...page.callbacks)//create simple array
arrayButton.forEach((button:any)=>{
...button,
label: { ...button.label, [languageSelect]: buttonText }
})
Assume your arrayButton looks like this:
const arrayButton: IBotButton[][] = [
[
{
label: { "en": "new Button-1" }
// ...
},
{
label: { "en": "new Button-2" }
// ...
},
//...
],
[
//...
]
];
And have these variables:
const languageSelect: string = "jp";
const buttonText: string = "new Button-3";
Then you could modify the label by following code:
// arrayButton is 2d array
arrayButton.forEach((array) => {
// array is 1d array
array.forEach((button, buttonIdx) => {
array[buttonIdx] = {
...button,
label: {
...button.label,
[languageSelect]: buttonText,
},
};
});
});

Insert parameters into JSON Array in Logic Apps

I am creating a Logic app to gather members from one platform using an API call and posting them to another platform using POST method. At the end of the entire process, I get a JSON array with the data that I need. However, I need to add in a parameters into the array at the beginning. How would I go about doing so?
Currently, my array looks like this
[
{
"company": "",
"email": "",
"firstName": "",
"lastName": "",
"nickname": "",
"prefix": "",
"sourceId": "",
"title": "",
"workPhone": ""
},
{
"company": "",
"email": "",
"firstName": "",
"lastName": "",
"nickname": "",
"prefix": "",
"sourceId": "",
"title": "",
"workPhone": ""
}
]
I need for the body of my HTTP request to look like this:
**{"data":**
[
**"dataRecord":** {
"company": "",
"email": "",
"firstName": "",
"lastName": "",
"nickname": "",
"prefix": "",
"sourceId": "",
"title": "",
"workPhone": ""
},
{
"company": "",
"email": "",
"firstName": "",
"lastName": "",
"nickname": "",
"prefix": "",
"sourceId": "",
"title": "",
"workPhone": ""
}
}
My current flow looks like this:
Scheduled Trigger
List item
Authenticate platform (to)
Authentication platform(from)
Get Data
Compose data
Parse Json
Initialize Array Variable
For Each:
(1)Compose - Map Parsed JSON data to Destination Fields
(2)Append to array variable
compose expression: string(variables('variable'))
Compose string to Json: json(string(outputs('Compose_2')))
HTTP POST
Edit:
Adding screenshot of where I need the data to be in the output, along with what my app looks like
After receiving the json array try using Parse Json action of Data Operations Connector to get the parameters and then you can form a json using Compose Connector. Here is the screenshot of my logic app for your reference.
Here is the output:
Here is the schema in the compose connector
{
"data": [
{
"dataRecord": {
"company": "#{items('For_each')['company']}",
"email": "#{items('For_each')['email']}",
"firstName": "#{items('For_each')['firstName']}",
"lastName": "#{items('For_each')['lastName']}",
"nickname": "#{items('For_each')['nickname']}",
"prefix": "#{items('For_each')['prefix']}",
"sourceId": "#{items('For_each')['sourceId']}",
"title": "#{items('For_each')['title']}",
"workPhone": "#{items('For_each')['workPhone']}"
}
}
]
}
Below is the code view of my logic app
{
"definition": {
"$schema": "https://schema.management.azure.com/providers/Microsoft.Logic/schemas/2016-06-01/workflowdefinition.json#",
"actions": {
"For_each": {
"actions": {
"Compose": {
"inputs": {
"data": [
{
"dataRecord": {
"company": "#{items('For_each')['company']}",
"email": "#{items('For_each')['email']}",
"firstName": "#{items('For_each')['firstName']}",
"lastName": "#{items('For_each')['lastName']}",
"nickname": "#{items('For_each')['nickname']}",
"prefix": "#{items('For_each')['prefix']}",
"sourceId": "#{items('For_each')['sourceId']}",
"title": "#{items('For_each')['title']}",
"workPhone": "#{items('For_each')['workPhone']}"
}
}
]
},
"runAfter": {},
"type": "Compose"
}
},
"foreach": "#body('Parse_JSON')",
"runAfter": {
"Parse_JSON": [
"Succeeded"
]
},
"type": "Foreach"
},
"Parse_JSON": {
"inputs": {
"content": "#triggerBody()",
"schema": {
"items": {
"properties": {
"company": {
"type": "string"
},
"email": {
"type": "string"
},
"firstName": {
"type": "string"
},
"lastName": {
"type": "string"
},
"nickname": {
"type": "string"
},
"prefix": {
"type": "string"
},
"sourceId": {
"type": "string"
},
"title": {
"type": "string"
},
"workPhone": {
"type": "string"
}
},
"required": [
"company",
"email",
"firstName",
"lastName",
"nickname",
"prefix",
"sourceId",
"title",
"workPhone"
],
"type": "object"
},
"type": "array"
}
},
"runAfter": {},
"type": "ParseJson"
}
},
"contentVersion": "1.0.0.0",
"outputs": {},
"parameters": {},
"triggers": {
"manual": {
"inputs": {
"schema": {}
},
"kind": "Http",
"type": "Request"
}
}
},
"parameters": {}
}

Format an object to split the header of the data

I have a Typescript project where I want to change the format of a JSON object. The object I receive is an array of objects with the keys and the values, what I want is to separate the keys in the first array and then the data in each array.
The object I currently have is the following:
[
{
"id": "1",
"ser": null,
"IP": null,
"host": "",
"type": "Web",
"auth": ""
},
{
"id": "2",
"ser": null,
"IP": "191.174.230.02",
"host": "",
"type": "Proxy",
"auth": ""
}
]
This is what I want to achieve:
[
"id",
"ser",
"IP",
"host",
"type",
"auth"
],
[
"1",
null,
null,
"",
"Web",
""
],
[
"2",
null,
"191.174.230.02",
"",
"Proxy",
""
]
This is the object that I want to get by adding the values property to it:`
{
"values": [
[
"id",
"ser",
"IP",
"host",
"type",
"auth"
],
[
"1",
null,
null,
"",
"Web",
""
],
[
"2",
null,
"191.174.230.02",
"",
"Proxy",
""
]
]
}
use Object.keys(data[0]) to get the keys of the first element
iterate all elements with Object.values(element) to get each element data
push key and element value to array
Here is the code.
var data = [
{
id: "1",
ser: null,
IP: null,
host: "",
type: "Web",
auth: "",
},
{
id: "2",
ser: null,
IP: "191.174.230.02",
host: "",
type: "Proxy",
auth: "",
},
];
var arr = [Object.keys(data[0])];
for (let element of data) {
arr.push(Object.values(element));
}
var obj = { values: arr };

$unwind multiple arrays present in the embedded documents and show each array as single document as output in mongoDB

Refer below code. In this, field scenario is a embedded document which has has arrays and I want to showcase each array as a single document in the output. Note that each array contains embedded document in it so it would be helpful to get the code which extracts fields from those too. I'm not using java to query. Would be using external BI application which would be integrated in. Think I should also mention that i'm using NoSQLBooster for MongoDB application to create these queries.
{
"_id": {
"$oid": ""
},
"organisationId": "",
"bcpId": "",
"bcpName": "",
"bcpDescription": "",
"biaEntity": {},
"version": "0.01",
"status": "PENDING",
"primaryBridgeNumber": "1",
"alternateBridgeNumber": "2",
"scenario": [{
"_id": {
"$oid": "5e3ab709367d2c5f5826c6fd"
},
"scenario": "",
"strategies": [{
"mdmStrategy": {},
"strategy": {
"pSIStrategyDetails": {
"scenarioName": "",
"strategyName": "",
"rto": "",
"sustainablePeriod": {
},
"description": "1",
"primaryContact": {
},
"secondaryContact": {
},
"recoverySite": {
}
},
"pSICriticalStaff": {},
"specialRequirement": [{
}, {
}, {
}, {
}, {
}]
},
"createdOn": {},
"updatedOn": {}
}, {
"mdmStrategy": {},
"strategy": {
"pSIStrategyDetails": {},
"pSICriticalStaff": {},
"specialRequirement": [{
},
{
},
{
},
{
},
{
}]
},
"createdOn": {},
"updatedOn": }
}],
"description": "",
"status": "Active",
"createdOn": {},
"updatedOn": {}
}],
"updatedOn": {},
"createdOn": {},
"business_owner_id": {},
"bc_coordinator_id": {},
"backup_business_owner_id": {},
"backup_business_coordinator_id": {},
"sme_id": {},
"_class": "com.bcm.bcp.api.model.BcmBcpEntity"
}
expected output:
{{
"bcpId": "",
"bcpName": "",
"bcpDescription": "",
"version": "0.01",
"status": "PENDING",
"scenario.scenario":"---",
"scenario.strategies.strategy.strategyName":"---",
"scenario.strategies.strategy.rto":"---",
etc...
}{
"bcpId": "",
"bcpName": "",
"bcpDescription": "",
"version": "0.01",
"status": "PENDING",
"scenario.scenario":"---",
"scenario.strategies.strategy.strategyName":"---",
"scenario.strategies.strategy.rto":"---",
etc...
}{
"bcpId": "",
"bcpName": "",
"bcpDescription": "",
"version": "0.01",
"status": "PENDING",
"scenario.scenario":"---",
"scenario.strategies.strategy.strategyName":"---",
"scenario.strategies.strategy.rto":"---",
etc...
}}
"scenario.scenario":"---","scenario.strategies.strategy.strategyName":"---",
"scenario.strategies.strategy.rto":"---",
will be coming from the arrays so the output will be number of elements present in the array
U hope this is what you want:
db.collection.aggregate([
{
$unwind: "$scenario"
},
{
$unwind: "$scenario.strategies"
},
{
$project: {
bcpId: 1,
bcpName: 1,
bcpDescription: 1,
version: 1,
status: 1,
scenario: {
scenario: 1,
strategies: {
strategy: {
pSIStrategyDetails: {
rto: 1,
strategyName: 1
}
}
}
}
}
}
])
Output:
[
{
"_id": ObjectId("5a934e000102030405000000"),
"bcpDescription": "",
"bcpId": "",
"bcpName": "",
"scenario": {
"scenario": "",
"strategies": {
"strategy": {
"pSIStrategyDetails": {
"rto": "",
"strategyName": ""
}
}
}
},
"status": "PENDING",
"version": "0.01"
},
{
"_id": ObjectId("5a934e000102030405000000"),
"bcpDescription": "",
"bcpId": "",
"bcpName": "",
"scenario": {
"scenario": "",
"strategies": {
"strategy": {
"pSIStrategyDetails": {}
}
}
},
"status": "PENDING",
"version": "0.01"
}
]
Explanation: You need to use 2 $unwind operators, as it is like arrays of arrays and a $project operator to display only those fields, which you need.
MongoPlayGroundLink
P.S. - The question is still unclear.

Create new Single Object from an ArrayObject

I want an Object which I can simply change to JSON format for Exporting later on.
For this I have an array of objects like this:
[
{
"name": "Test",
"value": "EN Test"
},
{
"name": "Test2",
"value": "DE test2"
}
]
It has to be exact like this
{
"Test" : "EN Test",
"Test2" : "DE test2",
}
So a Single Object with the Value of all Objects of the Array but only the Values of 'name' and 'value'.
Angular merge/copy/extend and so on is working with 2 Arrays and just merging them without Options like get only value of name and value.
With Lodash
object = _.map(objectArray,'name');
Giving me
[ "Test", "Test2", ]
But with multiple
object = _.map(objectArray,['name','value']);
[ false, false, ]
doing
object = _.map(objectarray,_.partial(_.ary(_.pick,2),_,['name','value']));
output is
[ { "name": "Test", "value": "EN Test" }, { "name": "Test2", "value": "DE test2" } ]
not
{ "Test" : "EN Test", "Test2" : "DE test2", }
and so on
object = _.mapValues(objectArray,'name');
{
"0": "Test",
"1": "Values",
}
this.lang = _.mapValues(this.exportData,_.partial(_.ary(_.pick,2),_,['name','value']));
"0": {
"name": "Test",
"value": "Test"
},
"1": {
"name": "TEst2",
"value": "Test2"
},..
What can I do to achieve is
{
"Test" : "EN Test",
"Test2" : "DE test2",
}
Use native JavaScript Array#reduce method.
var data = [{
"name": "Test",
"value": "EN Test"
}, {
"name": "Test2",
"value": "DE test2"
}];
// iterate over array
var res = data.reduce(function(obj, v) {
// define the object property
obj[v.name] = v.value;
// return the object
return obj;
// define initial value as empty object
// for storing the result
}, {})
console.log(res);
Try this
var a = [
{
"name": "Test",
"value": "EN Test"
},
{
"name": "Test2",
"value": "DE test2"
}
];
var obj = {};
a.forEach(function(node){
obj[node.name] = node.value
});
console.log(obj);

Resources