Using jq to change each variable in the array seperatly - jq

I have an array like this:
[
{"price": 10},
{"price": 20},
{"price": 30}
]
I want to go over each price item and set it to be negative.
If i do something like this:
.[].price = .[].price * -1
I'll get:
[
{
"price": -10
},
{
"price": -10
},
{
"price": -10
}
]
[
{
"price": -20
},
{
"price": -20
},
{
"price": -20
}
]
[
{
"price": -30
},
{
"price": -30
},
{
"price": -30
}
]
How do i do it properly so at the end it will look like this?
[
{"price": -10},
{"price": -20},
{"price": -30}
]

If the output is to be an array, then consider:
map( .price |= -1 * . )
If the price on output must be negative even if the input price is already negative, then you could replace the expression in parens by:
if .price > 0 then .price |= -1 * . else . end

How about
.[] as $x | { price: ($x.price * -1) }
Here is a working example:
https://jqplay.org/s/VeGHuouLRY

Related

`jq` filter to selectively replace parent field with child

I have many JSON objects where I would like to transform some objects to reduce redundancy and limit some depth.
For example, given:
{
"foo": [
{
"foobarList": {
"foobar": 2
}
},
{
"raboofList": {
"raboof": [3, 5, 7]
}
}
],
"bazList": {
"baz": 11
},
"foobar": {
"barbazList": {
"barbaz": [13, 17, 19]
},
"foobazList": {
"foobaz": {
"barfooList": {
"barfoo": [23, 29]
}
}
}
}
}
... I'd like to "hoist" the values of all the fields that end in "List", e.g., "foobarList".
So, the above would be transformed to:
{
"foo": [
{
"foobar": 2
},
{
"raboof": [3, 5, 7]
}
],
"baz": 11,
"foobar": {
"barbaz": [13, 17, 19],
"foobaz": {
"barfoo": [23, 29]
}
}
}
I've tried various filters, but so far I can only identify the "List$" fields with:
jq '.. |iterables |to_entries[] |select((has("key")) and (.key|type == "string") and (.key|test("List$")) )'
How can this transformation be accomplished with jq?
Here's a jqplay.org to help getting started.
The following produces the answer you want, but may not meet your expectations in every respect:
def hoist:
with_entries(if (.key | endswith("List"))
and (.value | type == "object")
and (.value | keys_unsorted | length==1)
then (.value | keys_unsorted[0]) as $k
| .key = $k
| .value |= .[$k]
else . end);
walk(if type == "object" then hoist else . end)
If the input is "hoistable" structurally, you could also operate on its stream representation:
[fromstream(tostream | del(.[0][] | select(endswith("List")?)))] | add
Demo

How to get the cheapest N results?

By default, Sabre does not return the cheapest results. It returns some sort of different price categories. How can I get the cheapest N (20, 50, 100, ...) prices?
This is my code:
"TPA_Extensions": {
"IntelliSellTransaction": {
"RequestType": {
"Name": "20ITINS"
}
}
},

trying to remove item from vector and this error came out: C2676 binary '=='

for (Node &i : myNodes){
if (CheckCollisionPointCircle(Vector2{ (float)(GetMouseX()),(float)(GetMouseY()) }, Vector2{ (float)(i.X),(float)(i.Y) }, i.radient)) {
auto index = std::find(myNodes.begin(), myNodes.end(), i);
myNodes.erase(index);
}
}
trying to remove a Node from vector of nodes if the CheckCollisionPointCircle() returns true
for (auto iter = myNodes.begin(); iter != myNodes.end();) {
if (CheckCollisionPointCircle(Vector2{ (float)(GetMouseX()),(float)(GetMouseY()) }, Vector2{ (float)(iter->X),(float)(iter->Y) }, iter->radient)) {
iter = myNodes.erase(iter);
}
else {
++iter;
}
}

Chart.js - Define custom y-axis scale

I am working with Chart.js on Asp.Net and I have a bar chart. My dataset has not very close numbers so I can not determine any fixed stepSize for this dataset.
My dataset like that;
[105000,200000,310000,0.0002]
So, y-axis range seems like that [0 - 100000 - 200000 - 300000 ...] but I want to show it like that [ 0 - 0.005 - 100000 - 200000 - 300000 ...].
My chart options :
options = {
scales: {
yAxes: [{
ticks: {
beginAtZero: true,
responsive: true,
mainAspectRatio: false
}
}]
}
}
I tried to add to options "suggestedMin: 0.005" but y-axis values didn't change.
How can I define custom scale for y-axis?
Try to use the callback property:
options = {
scales: {
yAxes: [{
ticks: {
beginAtZero: true,
responsive: true,
mainAspectRatio: false,
callback: (v) => !!~[0, 0.005, 100000, 200000, 300000].indexOf(v) ? v : '',
min: 0,
max: 300000,
step: 0.005
}
}]
}
}

What is is the correct way to update vertices and send them to GPU?

I'm trying to draw a line strip with points from user clicks on a QOpenGLWidget. If I put vertices manually and do not update them, it works as expected, but if I update my vector of Vertex, it only draws a line from center to the right center of the widget, regardless of vertices location. This is my updateVertices method (called when a user clicks on the widget):
void CurveGLWidget::updateVertices()
{
m_vao.bind();
m_vbo.bind();
m_vbo.allocate(vertices.size() * sizeof(Vertex));
m_vbo.write(0, &vertices.begin(), vertices.size() * sizeof(Vertex));
m_program.enableAttributeArray("position");
m_program.setAttributeBuffer("position", GL_FLOAT, 0, 2, sizeof(Vertex));
m_program.enableAttributeArray("color");
m_program.setAttributeBuffer("color", GL_FLOAT, sizeof(glm::vec2), 4, sizeof(Vertex));
m_vao.release();
m_vbo.release();
}
at this point m_vao and m_vbo are already created.
This is my paintGL method:
void CurveGLWidget::paintGL()
{
if (vertices.size() < 2) {
return;
}
updateVertices();
glClearColor(m_clearColor.r, m_clearColor.g, m_clearColor.b, m_clearColor.a);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
m_program.bind();
m_vao.bind();
glDrawArrays(GL_LINE_STRIP, 0, vertices.size());
m_vao.release();
m_program.release();
}
this is a sample of user clicks:
V X Y
Vertex[ 0 ] = [ -0.715, 0.48 ]
Vertex[ 1 ] = [ -0.5175, 0.08 ]
Vertex[ 2 ] = [ -0.285, 0.426667 ]
Vertex[ 3 ] = [ -0.2, -0.153333 ]
Vertex[ 4 ] = [ -0.02, 0.64 ]
Vertex[ 5 ] = [ 0.1425, 0.05 ]
Vertex[ 6 ] = [ 0.2875, 0.696667 ]
Vertex[ 7 ] = [ 0.41, 0.03 ]
Vertex[ 8 ] = [ -0.485, -0.396667 ]
Vertex[ 9 ] = [ -0.015, -0.37 ]
Vertex[ 10 ] = [ 0.14, 0.316667 ]
Can anyone tell me what is wrong with this code? How can I update my vertices on demand?
The full CurveGLWidget source code can be found here.
I figured it out. Instead of &vertices.begin() I must use &vertices.front().

Resources