I have list of jobs that needs to be perform based on the data and device.
This is my sample data
device = [ device0, device1, device2 ]
device0 = {
group: [MATCH1, MATCH2]
}
device1 = {
group: [MATCH1, MATCH2, MATCH3]
}
device2 = {
group: [MATCH2, MATCH3]
}
data = {
"destination": "destination",
"text": "Test send message",
"hlr": "MATCH2"
}
How can i fix my code so that my data can match and perform task to device. I'm sorry this is my first question. I'm still learning programming and I'm not fluent in english.
This is my sample code :
const jobs = []
jobs.push(data)
if (jobs.length > 0) {
let task= jobs.shift()
device.push(device.splice(0, 1)[0]); // swap device position
dataLoop: for (var i = 0; i < device.length; i++) {
itemLoop: for (var j = 0; j < device[0].group.length; j++) { // cycle through until find match
if (device[0].group[j].toString() === data.hlr.toString()) {
console.log(device[0].group[j] + ':' + data.hlr.toString()); // output should be MATCH2 : MATCH2
break dataLoop;
}
}
device.push(device.splice(0, 1)[0]);
}
device[0].sendData({ // using first device to perform task
receiver: data.destination,
text: data.text,
request_status: true
}, function(err, ref) {
setTimeout(function() {
if (err) { console.log(err) }
console.log(ref)
}, 5000)
});
}
Theres a typo:
itemLoop: for (var j = 0; j < device[i].group.length; j++) {
must be
itemLoop: for (var j = 0; j < device[0].group.length; j++) { //
Related
I'm trying to perform MapReduce on this kind of data set:
{
"_id": "599861ce7ce78cd973746906",
"name": "Macias Rosario",
"col": [
{
"date": "15/03/2016",
"name": "MAGNEATO",
"amount": 313.86
},
{
"date": "08/08/2016",
"name": "FORTEAN",
"amount": 151.06
},
{
"date": "05/11/2014",
"name": "ECRATIC",
"amount": 291.68
}
]
}
Goal is to sum up all amount for name Macias Rosario. Currently I did with my code to group all by subelements this.col.name on this way:
mapper = Code("""
function() {
for (var index = 0; index < this.col.length; ++index) {
var col = this.col[index];
emit(col.name, col.amount );
}
}
""")
reducer = Code("""
function(key, values) {
var total = 0;
for( var i = 0; i < values.length; ++i){
total += values[i];
}
return value.price;
}
""")
result = collection.map_reduce(mapper, reducer, "myresult")
Has anyone have some idea how to reference, or group by this.name and not this.col.name because I don't know anymore and I'm driving nuts?
PS don't suggest me to use aggregate, did that way, want to try on this way also :)
Kind regards,
I hope the following code helps (works in pymongo as well)
This is my map function:
var mapFunction = function() {
for (var idx = 0; idx < this.col.length; idx++) {
var key = this.name;
var value = { amount : this.col[idx].amount };
emit(key, value);
}
};
And the following is my reduce function:
var reduceFunction = function(key, amountVl) {
reduceVal = { amount : 0 };
for (var idx = 0; idx < amountVl.length; idx++) {
reduceVal.amount += amountVl[idx].amount;
}
return reduceVal;
}
With your sample data it produces:
{ "_id" : "Macias Rosario", "value" : { "amount" : 756.6 } }
I keep getting EsLint errors for having forEach in my loop. How can I fix the following code:
for (var i = 0; i < this.m_V.length; ++i) {
var o = this.m_V[i];
var node = o.m_Data.GetCurr(this.m_C);
if (node && node.m_datae.length) {
for (var j = 0; j < node.m_datae.length; ++j)
var dataelement = node.m_datae[j].m_dataat
AreaId.forEach(function(area) {
dataelement.forEach(function(value) {
if (area === value.m_V) {
if (vo.m_p) {
var a = v.m_p.map;
....
}
}
}
}, this);
}, this);
}
}
}
How can I move the forEach out of the code? I tried to move the forEach out but then it didn't know what my variables node and 'o' were.
This nested loop is running really slow in my app so I'm trying to change it to use regular for-loops instead of foreach. I'm a little confused on the find() part. Can someone help me convert this nested loop so that it doesn't use any foreach loops? thanks.
var filters = [];
if (selectionTagFilters.length > 0) {
for (var i = 0; i < selectionTagFilters.length; i++) {
filterTree.forEach(function find(tag) {
if (tag.tagCategoryId == selectionTagFilters[i].tag.tagCategoryId) {
tag.tags.forEach(function find(tag) {
if (tag.tagId == selectionTagFilters[i].tag.tagId) {
filters.push({ tagHeader: tag.tagHeader, tagId: tag.tagId, tagCategoryId: tag.tagCategoryId });
}
});
}
});
}
}
var filters = [],
selectionTagFilterItem = null,
filterTreeItem = null,
tagItem = null;
for(var i = 0, ii = selectionTagFilters.length; i < ii; i++) {
selectionTagFilterItem = selectionTagFilters[i];
for(var fti, ftii = filterTree.length; fti < ftii; fti++) {
filterTreeItem = filterTree[fti];
if (filterTreeItem.tagCategoryId != selectionTagFilterItem.tag.tagCategoryId) continue;
for(var ti = 0, tii = filterTreeItem.tags.length; ti < tii; ti++) {
tagItem = filterTreeItem.tags[ti];
if (tagItem.tagId != selectionTagFilterItem.tag.tagId) continue;
filters.push({ tagHeader: tagItem.tagHeader, tagId: tagItem.tagId, tagCategoryId: tagItem.tagCategoryId });
}
}
}
Could you please tell me how to make from this 4 functions just 1. Cause they all do one thing, with just one parameter changing all the time.
Also, I need to take the value of each time the function is running and put it into a new variable so I can after calculate it.
function getValue(age)
{
for (var i = 0; i < document.getElementsByName('age').length; i++)
{
if (document.getElementsByName('age')[i].checked)
{
return document.getElementsByName('age')[i].value;
}
}
}
function getBmiValue()
{
for (var i = 0; i < document.getElementsByName('bmi').length; i++)
{
if (document.getElementsByName('bmi')[i].checked)
{
return document.getElementsByName('bmi')[i].value;
}
}
}
function getFamValue()
{
for (var i = 0; i < document.getElementsByName('fam').length; i++)
{
if (document.getElementsByName('fam')[i].checked)
{
return document.getElementsByName('fam')[i].value;
}
}
}
function getDietValue()
{
for (var i = 0; i < document.getElementsByName('diet').length; i++)
{
if (document.getElementsByName('diet')[i].checked)
{
return document.getElementsByName('diet')[i].value;
}
}
}
function getValueByElementName(element_name)
{
for (var i = 0; i < document.getElementsByName(element_name).length; i++)
{
if (document.getElementsByName(element_name)[i].checked)
{
return document.getElementsByName(element_name)[i].value;
}
}
}
Or a little bit optimization:
function getValueByElementName(element_name)
{
var elements = document.getElementsByName(element_name);
for (var i = 0; i < elements.length; i++)
{
if (elements[i].checked)
return elements[i].value;
}
}
function getValue(tagname)
{
for (var i = 0; i < document.getElementsByName(tagname).length; i++)
{
if (document.getElementsByName(tagname)[i].checked)
{
return document.getElementsByName(tagname)[i].value;
}
}
}
Pass your element name as a variable in this function and call it.
Here is an example of what you can do:
function getValue(key) {
// get this once, not on each loop iteration
var elem = document.getElementsByName(key);
// cache the length too, so not to calculate it on each loop iteration
for (var i = 0, len = elem.length; i < len; i++) {
if (elem[i].checked) {
return elem[i].value;
// this will exit the function when it finds the FIRST one.
// Is that what you want?
}
}
}
// you can call above function like this:
getValue('age');
getValue('bmi');
getValue('fam');
getValue('diet');
I have a generated JSON file that I would like to transform. Is there an easy way to transform the "id"/"value" form of this JSON to a proper key/value JSON object, without using any frameworks?
These lines:
"value":"chrome",
"id":"foo"
would convert to:
"foo": "chrome"
Input JSON:
{"row":[
{
"column":[
{
"value":"chrome",
"id":"foo"
},
{
"value":0,
"id":"bar"
},
{
"value":"baz1",
"id":"baz"
},
{
"value":0,
"id":"legacy"
}
]
},
{
"column":[
{
"value":"firefox",
"id":"foo"
},
{
"value":0,
"id":"bar"
},
{
"value":"baz2",
"id":"baz"
},
{
"value":0,
"id":"legacy"
}
]
}
]
}
Desired JSON:
{"row":[
{
"foo":"chrome",
"bar":0,
"baz":"baz1",
"legacy":0
},
{
"foo":"firefox",
"bar":0,
"baz":"baz2",
"legacy":0
}
]
}
Here is the solution:
var result = {"row": []}
for (var i = 0; i < input["row"].length; i++) {
var object = {};
var column = input["row"][i]["column"];
for (var j = 0; j < column.length; j++) {
object[column[j]["id"]] = column[j]["value"];
}
result.row.push(object);
}
console.log(result);
input variable refers to your initial JSON object.
var input = {"row":[
{
"column":[
...
here is my function, i was typing it out allready before ioseb posted his answer so i figured it post it as well, it's linted and everything
function transform(data) {
"use strict";
var x, i, row, column, colNumb, out = {rows : []}, rownum = data.row.length;
for (x = 0; x < rownum; x += 1) {
row = {};
column = data.row[x].column;
colNumb = column.length;
for (i = 0; i < colNumb; i += 1) {
row[column[i].id] = column[i].value;
}
out.rows.push(row);
}
return out;
}