I had an issue earlier and received some help with it earlier but now i'm having an issue updating/adding a link to earlier issue.
I have a slightly different setup at this point because i couldn't figure out how to update my data.
At this point. I need to get the data from my Array A (filepath) into my list of objects as another property in my sub object with a key of "-i".
I feel like i should be able to iterate through the array and add one of these values to my object but i've tried using the reduce and loop features but haven't been able to get my desired output, if there's a easier way from starting with my last post issue i'd be happy to go back to that state.
//Desired ouput
{
process0000x0000: {-i:"D:\\Code\\UnitTest\\Tests\\Run_01_TEMP\\0000x0000.png, tr: 16, tc: 16, ofr: 16, ofc: 16, outfile: 'D:\\Code\\Process\\1' },
process0000x0001: {-i:"D:\\Code\\UnitTest\\Tests\\Run_01_TEMP\\0000x0001.png", tr: 16, tc: 16, ofr: 16, ofc: 16, outfile: 'D:\\Code\\Process\\1' },
process0000x0002: {-i:"D:\\Code\\UnitTest\\Tests\\Run_01_TEMP\\0000x0002.png", tr: 16, tc: 16, ofr: 16, ofc: 16, outfile: 'D:\\Code\\Process\\1' }
}
//Array A
"outputParameters": [
{
"name": "0000x0000",
"filepath": "D:\\Code\\UnitTest\\Tests\\Run_01_TEMP\\0000x0000.png"
},
{
"name": "0000x0001",
"filepath": "D:\\Code\\UnitTest\\Tests\\Run_01_TEMP\\0000x0001.png"
},
{
"name": "0000x0002",
"filepath": "D:\\Code\\UnitTest\\Tests\\Run_01_TEMP\\0000x0002.png"
}]
// current output
{
process0000x0000: { tr: 16, tc: 16, ofr: 16, ofc: 16, outfile: 'D:\\Code\\Process\\1' },
process0000x0001: { tr: 16, tc: 16, ofr: 16, ofc: 16, outfile: 'D:\\Code\\Process\\1' },
process0000x0002: { tr: 16, tc: 16, ofr: 16, ofc: 16, outfile: 'D:\\Code\\Process\\1' }
}
UPDATE:
so i added the below but i'm still receiving the last "filepath" for each entry in my consoleparamscompiled data. I tried your way and the way i have shown below but with the same results.
//results
},
process13x21: {
'-i': 'D:\\Code\\UnitTest\\ConsoleApp\\1\\13x23.png',
'-tr': 16,
'-tc': 16,
'-ofr': 16,
'-ofc': 16,
'-outfile': '"D:\\Code\\UnitTest\\ConsoleApp\\Process\\1"'
},
process13x22: {
'-i': 'D:\\Code\\UnitTest\\ConsoleApp\\1\\13x23.png',
'-tr': 16,
'-tc': 16,
'-ofr': 16,
'-ofc': 16,
'-outfile': '"D:\\Code\\UnitTest\\ConsoleApp\\Process\\1"'
},
process13x23: {
'-i': 'D:\\Code\\UnitTest\\ConsoleApp\\1\\13x23.png',
'-tr': 16,
'-tc': 16,
'-ofr': 16,
'-ofc': 16,
'-outfile': '"D:\\Code\\UnitTest\\ConsoleApp\\Process\\1"'
}
}
// ADDED CODE
// loop through each data we want to add and add a property.
` consoleOutputParamsOBJ.forEach((obj) => {
var processname = dynamicTaskNameBaseOBJ + obj.name;
console.log(processname);
//taskparamscompiled.processname['-i'] = obj.filepath;
taskparamscompiled[processname]['-i'] = obj.filepath;
// console.log(dynamicTaskNameBaseOBJ + obj.name);
});
console.log(taskparamscompiled);`
I'm not 100% sure I understand your question, but here goes.
You can access an objects property by a string by using [] instead of .
E.g.
let x = {name: "chris"};
x.name = 'Sam';
x['name'] = 'Ben';
The above will both change the name property
Here is a code example with the data you provided.
let data = [{
"name": "0000x0000",
"filepath": "D:\\Code\\UnitTest\\Tests\\Run_01_TEMP\\0000x0000.png"
}, {
"name": "0000x0001",
"filepath": "D:\\Code\\UnitTest\\Tests\\Run_01_TEMP\\0000x0001.png"
}, {
"name": "0000x0002",
"filepath": "D:\\Code\\UnitTest\\Tests\\Run_01_TEMP\\0000x0002.png"
}];
let result = {
process0000x0000: {
tr: 16,
tc: 16,
ofr: 16,
ofc: 16,
outfile: 'D:\\Code\\Process\\1'
},
process0000x0001: {
tr: 16,
tc: 16,
ofr: 16,
ofc: 16,
outfile: 'D:\\Code\\Process\\1'
},
process0000x0002: {
tr: 16,
tc: 16,
ofr: 16,
ofc: 16,
outfile: 'D:\\Code\\Process\\1'
}
}
// loop through each data we want to add and add a property.
data.forEach(obj => {
result[`process${obj.name}`]["-i"] = obj.filepath
})
console.log(result)
And the output is :
{
process0000x0000: {
-i: "D:\Code\UnitTest\Tests\Run_01_TEMP\0000x0000.png",
ofc: 16,
ofr: 16,
outfile: "D:\Code\Process\1",
tc: 16,
tr: 16
},
process0000x0001: {
-i: "D:\Code\UnitTest\Tests\Run_01_TEMP\0000x0001.png",
ofc: 16,
ofr: 16,
outfile: "D:\Code\Process\1",
tc: 16,
tr: 16
},
process0000x0002: {
-i: "D:\Code\UnitTest\Tests\Run_01_TEMP\0000x0002.png",
ofc: 16,
ofr: 16,
outfile: "D:\Code\Process\1",
tc: 16,
tr: 16
}
}
Related
I'm writing a NODE.JS code that presents the user with some data about his account inside a game.
It sends a GET request to the game API, and the API returns an array with the amount of "points" the player has with every "champion" in the game. The problem is, it doesn't return the "champion" names, only their IDs:
[
{
championId: 19,
championLevel: 7,
championPoints: 116531,
},
{
championId: 24,
championLevel: 6,
championPoints: 67710,
},
{
championId: 131,
championLevel: 6,
championPoints: 67233,
}
]
I have a list with all the IDs and their related champion names, as below. I want to know if there's a way to replace all the champion IDs with their according champion name, so I can print the array with the champion names instead of the IDs, to make it easier to understand the info. I only included 3 champions here to make it simpler, but the real array has 150 champions, so I need a way to "mass edit" it.
championId: 19 equals to champion name Warwick
championId: 24 equals to champion name Jax
championId: 131 equals to champion name Diana
What i would like to print is something like:
[
{
championName: Warwick,
championLevel: 7,
championPoints: 116531,
},
{
championName: Jax,
championLevel: 6,
championPoints: 67710,
},
{
championName: Diana,
championLevel: 6,
championPoints: 67233,
}
]
The two useful array functions are map and find; map to transform the scores array, and find to do the important part of the transformation, which is looking up the name in the names array....
let scores = [{
championId: 19,
championLevel: 7,
championPoints: 116531,
},
{
championId: 24,
championLevel: 6,
championPoints: 67710,
},
{
championId: 131,
championLevel: 6,
championPoints: 67233,
}
];
let names = [{
championId: 19,
name: 'Warwick'
},
{
championId: 24,
name: 'Jax'
},
{
championId: 131,
name: 'Diana'
}
];
let scoresWithNames = scores.map(s => {
// find the matching championId in the names array
let name = names.find(n => n.championId === s.championId).name;
return { ...s, name };
});
console.log(scoresWithNames);
What you want to use is Array.prototype.map MDN. It transforms all the elements of an array using a callback function and returns the transformed array:
const champions = [
{
championId: 19,
championLevel: 7,
championPoints: 116531,
},
{
championId: 24,
championLevel: 6,
championPoints: 67710,
},
{
championId: 131,
championLevel: 6,
championPoints: 67233,
}
];
function getName(championId) {
// your logic to get the name of the champion
}
const newChampions = champions.map((c) => ({
championName: getName(c.championId),
championLevel: c.championLevel,
championPoints: c.championPoints
}));
I am trying to invoke function invokeEndpoint and parse response. I am getting data as undefined
I tried to parse this JSON but I can't figure out why it's giving me undefined
sagemakerruntime.invokeEndpoint(params2, function (err, result) {
if (err) {
console.log("INVOKE ENDPOINT ERROR!!!" + err);
} else {
console.log(JSON.stringify(result));
}
});
JSON printed:
{
"ContentType": "text/csv; charset=utf-8",
"InvokedProductionVariant": "variant-name-1",
"Body": {
"type": "Buffer",
"data": [
123,
34,
54,
50,
102,
55,
48,
51,
53,
102,
45,
99,
102,
52,
52,
45,
52,
53,
50,
50,
99,
100,
49,
48,
50,
49,
51,
97,
51,
52,
52,
34,
58,
32,
34,
81
]
}
}
Tried:
console.log(JSON.stringify(result["Body"].data));
Keeps giving me undefine.. unsure why. Tried doing result["Body"].data.toString('utf8'); doesn't work either. Anyone know what I'm doing wrong?
According to doc: SageMakerRuntime.html, Body is buffer type.
Try this:
sagemakerruntime.invokeEndpoint(params2, function (err, result) {
if (err) {
console.log("INVOKE ENDPOINT ERROR!!!" + err);
} else {
console.log(JSON.stringify(result.Body.toString('utf8')));
}
});
Hiii, some body know how i can write Uint16Array in kepserver i got some error:
ConstantStatusCode {
_value: 2147483648,
_description: 'The value is bad but no specific reason is known.',
_name: 'Bad' } ]
i'm try this:
var valor = new Uint16Array([ 2, 23, 23, 12, 24, 3, 25, 3, 26, 3, 27, 3, 28, 1, 43690, 1, 1261, 0, 0, 0, 0, 0, 0, 0, 65535, 11 ])
nodeToWrite[0] = {
nodeId: resolveNodeId("ns=2;s=" + endereco[0].ADDRESS),
attributeId: opcua.AttributeIds.Value,
value: /new DataValue(/{ value: {/ Variant /
dataType: 5,
arrayType: 1,
value: valor,
}
}
}
const {AttributeIds, OPCUAClient, DataType, VariantArrayType} = require("node-opcua");
const endpointUrl = "opc.tcp://localhost:48010";
(async () => {
const client = OPCUAClient.create({ endpoint_must_exist: false});
await client.withSessionAsync(endpointUrl, async (session) => {
const arrayOfvalues = new Uint16Array([ 2, 23, 23, 12, 24, 3, 25, 3, 26, 3, 27, 3, 28, 1, 43690, 1, 1261, 0, 0, 0, 0, 0, 0, 0, 65535, 11 ]);
const nodeToWrite = {
nodeId: "ns=2;s=Demo.Static.Arrays.UInt16",
attributeId: AttributeIds.Value,
value: {
value: {
dataType: DataType.UInt16,
arrayType: VariantArrayType.Array,
value: arrayOfvalues,
}
}
}
const statusCode = await session.write(nodeToWrite);
console.log("write statusCode = ",statusCode.toString());
});
})();
As demonstrated above, writing a Array of Uint16 is ok when addressing node ns=2;s=Demo.Static.Arrays.UInt16 of UAServerCPP from Unified Automation.
I would contact Kepware for support.
I am detecting beacons and getting JSON data from which I can get Mac Address of Beacon but I need information about battery level.
Currently I am using node js and using Noble module for searching beacons.
var https = require('https'),
myip = require('quick-local-ip'),
noble = require('noble'),
os = require('os'),
amqp = require('amqp');
noble.on('stateChange', function (state) {
if (state === 'poweredOn') {
console.log("start scanning");
noble.startScanning([], true);
} else {
noble.removeAllListener();
noble.stopScanning();
console.log("stop scanning, is Bluetooth on?");
}
noble.on('discover', function (peripheral) {console.log("peripheral - "+peripheral);}
Output -
{
"id": "ac233f244b34",
"address": "ac:23:3f:24:4b:34",
"addressType": "public",
"connectable": true,
"advertisement": {
"manufacturerData": {
"type": "Buffer",
"data": [76, 0, 2, 21, 226, 197, 109, 181, 223, 251, 72, 210, 176, 96, 208, 245, 167, 16, 150, 224, 0, 1, 0, 1, 129]
},
"serviceData": [{
"uuid": "feaa",
"data": {
"type": "Buffer",
"data": [32, 0, 12, 120, 24, 0, 0, 0, 251, 140, 1, 223, 146, 120]
}
}],
"serviceUuids": ["feaa"],
"solicitationServiceUuids": [],
"serviceSolicitationUuids": []
},
"rssi": -48,
"state": "disconnected"
}
How can I Get to know about Battery level from this data.
I'm trying this simple MapReduce operation:
function map() {
var gameDay = Math.floor((this.matchCreation - 1427846400000) / 86400000) + 1; // day of april 2015 when the game was played
this.teams.forEach (function (team){
**team.bans.forEach(function (ban){** // says bans is undefined
var value ={
banned : 1,
firstBanned: ( ((ban.pickTurn == 1) || (ban.pickTurn == 2))? 1 : 0 )
}
emit({championId: ban.championId,
day: Number(gameDay)}, value);
emit({championId: ban.championId,
day: "all"}, value);
});
});
}
function reduce(key, values) {
var a = values[0];
for(var i = 1 ; i<values.length ; i++){
var b = values[i]; // will merge 'b' into 'a'
a.banned += (b.banned? b.banned : 0);
a.firstBanned += (b.firstBanned? b.firstBanned : 0);
for (var attrname in b){
if(attrname != "banned" && attrname != "firstBanned")
a[attrname] = b[attrname];
}
}
return a;
}
matchesCollection.mapReduce(map, reduce, {
out: { reduce: "mapReduceResults" }
}, function (err, data){
if(err)
return callback (err);
callback (null, "OK");
});
It used to work before, but just when I tried to deploy the app after testing for a while, it seems to fail in this line: team.bans.forEach(function (ban){, says team.bans is undefined, although every one of the documents has a "teams" array and a "bans" array inside of each object in it, I even double checked it by querying the database and there is no document in which those fields dont exist.
So weird. The reduce function is a bit more complex but it seems to work alright, yet the map one (unlike Reduce, its supposed to be called just once per original document, right?) throws this unexplainable error. Could anyone give me some insight?
Sample input:
{
"_id": {
"$oid": "5531a63f2a3f135c11ed14a8"
},
"matchId": 1778704162,
"region": "NA",
"platformId": "NA1",
"matchMode": "CLASSIC",
"matchType": "MATCHED_GAME",
"matchCreation": 1427864425511,
"matchDuration": 1431,
"queueType": "URF_5x5",
"mapId": 11,
"season": "SEASON2015",
"matchVersion": "5.6.0.194",
"participants": [
{
"teamId": 100,
"spell1Id": 12,
"spell2Id": 4,
"championId": 81,
"highestAchievedSeasonTier": "SILVER",
"timeline": [],
"masteries": [],
"stats": {
"winner": false,
"champLevel": 19,
"item0": 1037,
"item1": 3078,
"item2": 3117,
"item3": 3035,
"item4": 3072,
"item5": 1038,
"item6": 3340,
"kills": 7,
"doubleKills": 1,
"tripleKills": 0,
"quadraKills": 0,
"pentaKills": 0,
"unrealKills": 0,
"largestKillingSpree": 3,
"deaths": 15,
"assists": 9,
"totalDamageDealt": 103191,
"totalDamageDealtToChampions": 22148,
"totalDamageTaken": 32924,
"largestCriticalStrike": 669,
"totalHeal": 2263,
"minionsKilled": 97,
"neutralMinionsKilled": 1,
"neutralMinionsKilledTeamJungle": 1,
"neutralMinionsKilledEnemyJungle": 0,
"goldEarned": 13923,
"goldSpent": 13273,
"combatPlayerScore": 0,
"objectivePlayerScore": 0,
"totalPlayerScore": 0,
"totalScoreRank": 0,
"magicDamageDealtToChampions": 6082,
"physicalDamageDealtToChampions": 15803,
"trueDamageDealtToChampions": 263,
"visionWardsBoughtInGame": 0,
"sightWardsBoughtInGame": 0,
"magicDamageDealt": 45997,
"physicalDamageDealt": 56651,
"trueDamageDealt": 543,
"magicDamageTaken": 25249,
"physicalDamageTaken": 7490,
"trueDamageTaken": 184,
"firstBloodKill": false,
"firstBloodAssist": false,
"firstTowerKill": false,
"firstTowerAssist": false,
"firstInhibitorKill": false,
"firstInhibitorAssist": false,
"inhibitorKills": 0,
"towerKills": 4,
"wardsPlaced": 2,
"wardsKilled": 0,
"largestMultiKill": 2,
"killingSprees": 1,
"totalUnitsHealed": 1,
"totalTimeCrowdControlDealt": 98
},
"participantId": 1,
"runes": []
},
... (9 more like that)
],
"participantIdentities": [],
"teams": [
{
"teamId": 100,
"winner": false,
"firstBlood": true,
"firstTower": false,
"firstInhibitor": true,
"firstBaron": false,
"firstDragon": true,
"towerKills": 6,
"inhibitorKills": 2,
"baronKills": 0,
"dragonKills": 3,
"vilemawKills": 0,
"dominionVictoryScore": 0,
"bans": [
{
"championId": 120,
"pickTurn": 1
},
{
"championId": 37,
"pickTurn": 3
},
{
"championId": 13,
"pickTurn": 5
}
]
},
{
"teamId": 200,
"winner": true,
"firstBlood": false,
"firstTower": true,
"firstInhibitor": false,
"firstBaron": false,
"firstDragon": false,
"towerKills": 11,
"inhibitorKills": 4,
"baronKills": 0,
"dragonKills": 0,
"vilemawKills": 0,
"dominionVictoryScore": 0,
"bans": [
{
"championId": 28,
"pickTurn": 2
},
{
"championId": 38,
"pickTurn": 4
},
{
"championId": 63,
"pickTurn": 6
}
]
}
]
}
Expected output:
{
_id: { championId: Number, day: Number }
value: { banned: Number, firstBanned: Number }
}
After that, its supposed to merge with the results of a previous MapReduce operation, copying all the fields of documents with the same key (in the reduce function), but thats irrelevant now since the error happens before...