I have a json object that looks like this and I have read this into a variable called myData
{
{
issue : 'A1',
pdate : '2021-05-21'
type : 'small'
},
{
issue : 'A2',
pdate : '2021-05-21'
type : 'med'
},
{
issue : 'A4',
pdate : '2021-05-21'
type : 'large'
},
{
issue : 'A22',
pdate : '2021-06-21'
type : 'small'
},
{
issue : 'A222',
pdate : '2021-06-21'
type : 'small'
},
{
issue : 'A422',
pdate : '2021-06-21'
type : 'small'
}
}
I want to get how many items of each time exist on a given day.
My current code is something like this
var dict = {};
myData.map((elem) => {
var xkey = elem.pdate;
dict[xkey] = xkey in dict ? dict[xkey] + 1 : 1;
});
this given me a dict like this
2021-05-21 : 3,
2021-06-21 : 2
Now I want to convert this into a dictionary or a 2D array so I can know how many diff type of items are present on a given day.
{
'2021-05-21' : {'small':1,'med':1,'large':1},
'2021-06-21' : {'small' : 2}
}
Please can someone help me write this. I have tried diff combinations of if conditions to make this work but I am unable to get this. Also this doesn't have to be a dictionary, either an array or a dict, as long as I am able to iterate over it later.
Hope this works:
const test = [{
issue : 'A1',
pdate : '2021-05-21',
type : 'small'
},
{
issue : 'A2',
pdate : '2021-05-21',
type : 'med'
},
{
issue : 'A4',
pdate : '2021-05-21',
type : 'large'
},
{
issue : 'A22',
pdate : '2021-06-21',
type : 'small'
},
{
issue : 'A222',
pdate : '2021-06-21',
type : 'small'
},
{
issue : 'A422',
pdate : '2021-06-21',
type : 'small'
}]
const output = {};
test.forEach((obj) => {
if (!output[obj.pdate]) {
output[obj.pdate] = {};
}
if (!output[obj.pdate][obj.type]) {
output[obj.pdate][obj.type] = 0;
}
output[obj.pdate][obj.type] += 1;
});
console.log(output);
Related
I am making query for my orders list, here is the data structure and my database rules:
I use a query like this to find out whether the latest price hit the tp or sl price like this:
function GetBuyList(symbol, currentPrice) {
//Less than or equal to, for buying search tp lesser than current price
var buy_tp = db.ref(`orders/active_orders/${symbol}`).orderByChild("tp").startAt(`buy_tp_${0}`).endAt(`buy_tp_${currentPrice}`)
//More than or equal to, for buying search sl higher than current price
var buy_sl = db.ref(`orders/active_orders/${symbol}`).orderByChild("sl").startAt(`buy_sl_${currentPrice}`).endAt(`buy_sl_${100000000}`)
buy_tp.once("value", function (snapshot) {
// do some stuff once
if (snapshot.val() !== null) {
ProcessOrders(snapshot.val(), 'tpHit', currentPrice)
}
});
buy_sl.once("value", function (snapshot) {
// do some stuff once
if (snapshot.val() !== null) {
ProcessOrders(snapshot.val(), 'slHit', currentPrice)
}
});
}
For price that is in lower value like 1.211, it working fine, but when the price goes larger, the buy_sl query is not working, but the buy_tp query is still working fine. Example, when I query for the price like 34886 for the data below the buy_sl is not working:
Edit:
Hi Frank, herein the json exported:
{
"active_orders" : {
"BTCUSD" : {
"-Masii03kq9LvuLfWOyG" : {
"close_type" : "None",
"lot_size" : 1,
"order_price" : 34888.17,
"sl" : "buy_sl_34887",
"symbol" : "BTCUSD",
"tp" : "buy_tp_34889",
"ts" : 1622301925456,
"type" : "buy",
"uid" : "6XaKYgXCsuMNg1d5bWYHg6ej5sd2"
}
},
"EURUSD" : {
"-MasVPCtD4sdPCcdF9S9" : {
"close_type" : "None",
"lot_size" : 1,
"order_price" : 1.211,
"sl" : "buy_sl_1.210",
"symbol" : "EURUSD",
"tp" : "buy_tp_1.23",
"ts" : 1622298174339,
"type" : "buy",
"uid" : "6XaKYgXCsuMNg1d5bWYHg6ej5sd2"
}
},
"USDJPY" : {
"-MasWoRREHQhvOR6iQ8G" : {
"close_type" : "None",
"lot_size" : 1,
"order_price" : 109.861,
"sl" : "buy_sl_107.0",
"symbol" : "USDJPY",
"tp" : "buy_tp_110",
"ts" : 1622298543910,
"type" : "buy",
"uid" : "6XaKYgXCsuMNg1d5bWYHg6ej5sd2"
}
}
}
}
Example, when I perform the function GetBuyList("EURUSD", 1.3) or GetBuyList("EURUSD", 1.1), the result returned as:
{
'-MasVPCtD4sdPCcdF9S9': {
close_type: 'None',
lot_size: 1,
order_price: 1.211,
sl: 'buy_sl_1.210',
symbol: 'EURUSD',
tp: 'buy_tp_1.23',
ts: 1622298174339,
type: 'buy',
uid: '6XaKYgXCsuMNg1d5bWYHg6ej5sd2'
}
}
When I perform the function like this, GetBuyList("BTCUSD", 34890), it would return:
{
'-Masii03kq9LvuLfWOyG': {
close_type: 'None',
lot_size: 1,
order_price: 34888.17,
sl: 'buy_sl_34887',
symbol: 'BTCUSD',
tp: 'buy_tp_34889',
ts: 1622301925456,
type: 'buy',
uid: '6XaKYgXCsuMNg1d5bWYHg6ej5sd2'
}
}
But when I run this, GetBuyList("BTCUSD", 34886), nothing is return.
sl and tp are both strings and because they are, they won't be parsed as numbers and instead are subject to lexiographic sorting.
One of the most common examples of this happening is if you look at a file list in a folder:
0.jpg
1.jpg
10.jpg
11.jpg
12.jpg
2.jpg
3.jpg
4.jpg
5.jpg
6.jpg
7.jpg
8.jpg
9.jpg
If you can't switch from using strings, you need to pad the number with your expected maximum number:
000.jpg
001.jpg
002.jpg
003.jpg
004.jpg
005.jpg
006.jpg
007.jpg
008.jpg
009.jpg
010.jpg
011.jpg
012.jpg
const formatWithPadding = (inp, digits) => {
let n = Number(inp), nStr = `${Math.abs(n)}`, sign = n<0;
return (sign ? '+' : '-') + (
nStr.length > digits
? nStr
: `${"0".repeat((digits || 1) - 1)}${nStr}`.slice(-digits)
)
};
const tpVal = 1.210;
const [integerPart, fractionalPart] = String(tpVal).split(".");
const tp = `buy_tp_${formatWithPadding(integerPart, 6)}.${fractionalPart || 0}`;
// tp is "buy_tp_+000001.210"
To find objects with array of ids we can do this:
var idsArray = ['id1', 'id2', 'id3'];
SomeObject.find({'chatroomId': { $in: idsArray}}).exec();
.then(function(someObjects) {
console.log(someObjects);
})
Is it possible to limit result for each member in array?
Not for whole result like this:
SomeObject.find({'chatroomId': { $in: idsArray}}).limit(3)
but for each element. Maybe something like this:
SomeObject.find({'chatroomId': { $in: idsArray.forEach.limit(3)}});
Considering that you have a document structure like :
{
_id : ObjectId("591931855d0aad80c996eab2)",
chatName : 'SchoolChat',
chatrootId : ['id1', 'id2','id3','id4','id5']
}
You can use following query to get what you need :
db.collection_name.aggregate([
{ $unwind : '$chatrootId' },
{ $limit : 3},
{ $group : {
_id : '$_id',
chatName : { $first : '$chatName' },
chatroomId : { $push : '$chatrootId' },
}
}
]);
It will give you :
{
"_id" : ObjectId("591931855d0aad80c996eab2"),
"chatName" : "SchoolChat",
"chatroomId" : [ 'id1', 'id2', 'id3']
}
I am new to angular js, and I was trying to get unique values using ng-repeat. I went through many sites for the example , one of them is enter link description here
which I got from stackoverflow only.
var app = angular.module('app', []);
app.filter('unique', function() {
return function(collection, keyname) {
var output = [],
keys = [];
angular.forEach(collection, function(item) {
var key = item[keyname];
if(keys.indexOf(key) === -1) {
keys.push(key);
output.push(item);
}
});
return output;
};
});
app.controller('MyCtrl', function ($scope) {
$scope.items = [
{ id : 1,column : "col1", comment : "col1-label1",
text1 : "col1-label1-text1",
checked: false,
},
{
id : 2,
column : "col1",
comment : "col1-label2",
text1 : "col1-label2-text1",
checked: false,
},
{
id : 3,
column : "col2",
comment : "col2-label1",
text1 : "col2-label1-text1",
checked: false,
},
{
id : 4,
column : "col2",
comment : "col2-label2",
text1 : "col2-label2-text1",
checked: false,
},
{
id : 5,
column : "col3",
comment : "col3-label1",
text1 : "col3-label1-text1",
checked: false,
},
{
id : 6,
column : "col3",
comment : "col3-label2",
text1 : "col3-label2-text1",
checked: false,
},
{
id : 7,
column : "col4",
comment : "col4-label1",
text1 : "col4-label1-text1",
checked: false,
},
{
id : 8,
column : "col4",
comment : "col4-label2",
text1 : "col4-label2-text1",
checked: false,
}
];
});
But I am having problem to understand at one point that I have spent hours on but still couldn't understand.
**angular.forEach(collection, function(item) {
var key = item[keyname];**
In forEach method in the function we pass value , index and then the object itself that we are iterating over, In the above example we are only passing the value as "item" , then in the next line we are using as object/array. I am really confused, what is it? what are they trrying to do
I have this schema with mongoose
schema = new Schema({
id: {
type: String,
},
embedded: [embeddedSchema]
});
embeddedSchema = new Schema({
value: {
type: String,
},
});
This can produce something like :
{
"_id" : ObjectId("5454f4f1073cc3b529320f79"),
"embedded" : [
{
"value" : 123,
} , {
"value" : 123,
},
{
"value" : 123423,
}
]
}
/* 1 */
{
"_id" : ObjectId("5454f508910ef3b82970f11d"),
"embedded" : [
{
"value" : 1,
} , {
"value" : 2,
},
{
"value" : 9999999,
}]
}
I would like to sort the schema collection by the biggest value of embedded doc.
Which query can produce this kind of result ?
Thanks you!
When you sort descending on an array element field like value, MongoDB uses the maximum value of that field among all elements in the array.
So in this case it would be:
MyModel.find().sort('-embedded.value').exec(callback);
I am very new to Mongo so I am most likely missing something very obvious, but I have not found anything on the internet to tell me what it is. I am trying to run a mongodb query from a JavaScript file but I am having trouble.
Mongo seems to be ignoring the projection part of the query, everything else is coming through fine though.
criteria = ' { "powersave_enabled" : false, "tx_rate" : { $lt : 26000 }, "rx_rate" : { $lt : 26000 }, "btyes-r" : { $ne: 0 } } ';
projection = ' {"_id":0, "hostname" : 1, "rssi" : 1, "mac" : 1, "ap_mac" : 1, "noise" : 1} ';
command = criteria + ', ' + projection;
accessPoints = db.cache_sta.find(command);
while (accessPoints.hasNext()){
printjson( accessPoints.next() );
}
I have printed out the command and tried running it myself in mongo and it seems to be working right, but something in the JS is messed up.
Thanks in advance!
Instead of concatenate the criteria and the projection pass them as objects like this:
criteria = { "powersave_enabled" : false, "tx_rate" : { $lt : 26000 }, "rx_rate" : { $lt : 26000 }, "btyes-r" : { $ne: 0 } };
projection = {"_id":0, "hostname" : 1, "rssi" : 1, "mac" : 1, "ap_mac" : 1, "noise" : 1};
accessPoints = db.cache_sta.find(criteria, projection);
while (accessPoints.hasNext()){
printjson( accessPoints.next() );
}