Replacing values in an object - arrays

I have an object that looks like this:
{id: 1, cells: ["Ashar", 68345, 14, 66666, "Retail", 1, ""]}
And I have an array of objects like so:
[
{id: 1, cells: ["Ashar", 68345, 14, 46100, "Retail", 1, ""]},
{id: 2, cells: ["Ashar", 300881, 14, 37000, "Retail", 3, 3]},
{id: 3, cells: ["Rob", 94448, 17, 11395, "Retail", 1, ""]},
{id: 4, cells: ["Shahab", 19023870, 219, 12500, "Retail", 1, ""]},
{id: 5, cells: ["David", 29008000, 229, 12500, "Retail", 5, 26]}
]
What I am trying to do is take the id of the first object and use it to search through the array to find the matching id and then replace the value of cells with the value from the first object.
Any help with figuring this out would be much appreciated.
Thanks for your time.

var temp = {id: 1, cells: ["Ashar", 68345, 14, 66666, "Retail", 1, ""]};
var arr=[
{id: 1, cells: ["Ashar", 68345, 14, 46100, "Retail", 1, ""]},
{id: 2, cells: ["Ashar", 300881, 14, 37000, "Retail", 3, 3]},
{id: 3, cells: ["Rob", 94448, 17, 11395, "Retail", 1, ""]},
{id: 4, cells: ["Shahab", 19023870, 219, 12500, "Retail", 1, ""]},
{id: 5, cells: ["David", 29008000, 229, 12500, "Retail", 5, 26]}
];
arr.forEach(function(value,key){
if(value.id == temp.id) {
value.cells=temp.cells;
}
});

Related

How can I swap two bits starting from MSB?

I'm looking for a way to swap 2 bits at a given position but counting starts from MSB(most significant bit) to LSB (least significant bit).
Lets say i have position p1 = 0 and p2 = 2 and my number is 1000 = 8 . The result should be
0010 .
I tried this piece of code but it swaps the bits starting from LSB to MSB . How do I "reverse" the process?
unsigned int bit1 = (num >> p1) & 1;
unsigned int bit2 = (num >> p2) & 1;
unsigned int x = (bit1 ^ bit2);
x = (x << p1) | (x << p2);
result = num ^ x;
By your example, I will assume you mean to start numbering bits from the MSB of the number value. That is, given:
00010100
↑
msb == most significant bit in the number
In that case, you need a way to count the bit index of the MSB. There are actually processor instructions you can use to do that, and both MSVC and GCC (and Clang) provide special functions to access that functionality...
...but you don’t need that. Instead, just write yourself a function:
int index_of_msb( unsigned long value )
{
...
}
Remember that you can shift a number down by one bit at a time. As long as the number is not zero, you have at least one set bit left.
1 0 1 0 0 0
→ 1 0 1 0 0 (1 shift)
→ 1 0 1 0 (2 shifts)
→ 1 0 1 (3 shifts)
→ 1 0 (4 shifts)
→ 1 (5 shifts)
→ 0
Five shifts → bit 5 is MSB.
Now you can convert your bit positions to the standard shift offsets.
p1 = p_msb - p1;
p2 = p_msb - p2;
What remains to do is use your standard bit operators to swap the two bit values.
Four bits is small enough that you can use a 256-byte table:
unsigned char SwapBits(unsigned char number, int p0, int p1)
{
static const unsigned char Table[16][4][4] = {
{ { 0, 0, 0, 0}, { 0, 0, 0, 0}, { 0, 0, 0, 0}, { 0, 0, 0, 0} },
{ { 1, 0, 0, 0}, { 0, 0, 0, 0}, { 0, 0, 0, 0}, { 0, 0, 0, 0} },
{ { 2, 1, 0, 0}, { 1, 2, 2, 2}, { 0, 2, 2, 2}, { 0, 2, 2, 2} },
{ { 3, 3, 1, 1}, { 3, 3, 2, 2}, { 1, 2, 3, 3}, { 1, 2, 3, 3} },
{ { 4, 2, 1, 0}, { 2, 4, 4, 4}, { 1, 4, 4, 4}, { 0, 4, 4, 4} },
{ { 5, 3, 5, 1}, { 3, 5, 6, 5}, { 5, 6, 5, 4}, { 1, 5, 4, 5} },
{ { 6, 6, 3, 2}, { 6, 6, 5, 4}, { 3, 5, 6, 6}, { 2, 4, 6, 6} },
{ { 7, 7, 7, 3}, { 7, 7, 7, 5}, { 7, 7, 7, 6}, { 3, 5, 6, 7} },
{ { 8, 4, 2, 1}, { 4, 8, 8, 8}, { 2, 8, 8, 8}, { 1, 8, 8, 8} },
{ { 9, 5, 3, 9}, { 5, 9, 9, 12}, { 3, 9, 9, 10}, { 9, 12, 10, 9} },
{ {10, 6, 10, 3}, { 6, 10, 12, 10}, {10, 12, 10, 9}, { 3, 10, 9, 10} },
{ {11, 7, 11, 11}, { 7, 11, 13, 14}, {11, 13, 11, 11}, {11, 14, 11, 11} },
{ {12, 12, 6, 5}, {12, 12, 10, 9}, { 6, 10, 12, 12}, { 5, 9, 12, 12} },
{ {13, 13, 7, 13}, {13, 13, 11, 13}, { 7, 11, 13, 14}, {13, 13, 14, 13} },
{ {14, 14, 14, 7}, {14, 14, 14, 11}, {14, 14, 14, 13}, { 7, 11, 13, 14} },
{ {15, 15, 15, 15}, {15, 15, 15, 15}, {15, 15, 15, 15}, {15, 15, 15, 15} },
};
return Table[number][p0][p1];
}

How to fetch data from event's resource and display on week view in React-Big-Calendar

I am trying to add custom data for month, week and day view. I created custom event file where I separate date and date time for week's event.
I am able to display data if I export event file like this
export default [
{
progress: '80',
title: 'streak minus one',
end: new Date(2022, 2, 21, 10, 8, 0),
start: new Date(2022, 2, 21, 10, 0, 0),
resource: {
month: {
status: true,
progress: 80
}
}
},
{
progress: '80',
title: 'streak minus one',
end: new Date(2022, 2, 21, 10, 19, 0),
start: new Date(2022, 2, 21, 10, 9, 0),
resource: {
month: {
status: true,
progress: 80
}
}
}
]
But not like this
export default [
{
title: 'streak minus one',
end: new Date(2022, 2, 21),
start: new Date(2022, 2, 21),
resource: {
month: {
progress: 70
},
week: [
{
endDateTime: new Date(2022, 2, 21, 10, 8, 0),
startDateTime: new Date(2022, 2, 21, 10, 0, 0)
},
{
endDateTime: new Date(2022, 2, 21, 10, 19, 0),
startDateTime: new Date(2022, 2, 21, 10, 9, 0)
},
{
endDateTime: new Date(2022, 2, 21, 10, 26, 0),
startDateTime: new Date(2022, 2, 21, 10, 19, 0)
},
{
endDateTime: new Date(2022, 2, 21, 10, 33, 0),
startDateTime: new Date(2022, 2, 21, 10, 26, 0)
}
]
}
},
{
title: 'second event',
end: new Date(2022, 2, 23),
start: new Date(2022, 2, 23),
resource: {
month: {
status: true,
progress: 45
},
week: [
{
endDateTime: new Date(2022, 2, 22, 10, 15, 0),
startDateTime: new Date(2022, 2, 22, 10, 0, 0)
}
]
}
}
]
How to display events by fetching it from resource on Week View in React-big-calendar
React-Big-Calendar expects the events individual 'event' data structures to be consistent, and uses the same data 'accessors' (startAccessor, endAccessor, titleAccessor) in all views. You can use onRangeChange to reload your events, but it would still expect the data structures to be the same.

Can't access Array in UseEffect return statement

I am fetching data in React using axios inside a useEffect hook. When I console log the data from inside the useEffect hook everything is fine, but when I attempt to access it in the return statement, it returns Cannot read property 'Con' of undefined.
Here is my code:
import React, { useState, useEffect } from "react";
import axios from 'axios'
import './App.css';
export default function App() {
const [data, setData] = useState([]);
useEffect(() => {
axios.get('/api/comRes', {headers:{"Access-Control-Allow-Origin": "http://localhost:3000/"}})
.then(function (data) {
setData(data.data);
console.log(data.data.Con[0])
})}, []);
return (
<div className="grid-container">
<div className="menu"></div>
<div className="dashboard">{data.data.Con[0]}</div>
</div>
);
this is the console.log of of the whole datset:
{_id: "5e85c1241c9d440000e730c7", Con: Array(30), Lab: Array(30), LibDem: Array(30), Other: Array(30), …}
_id: "5e85c1241c9d440000e730c7"
Con: (30) [47, 50, 44, 33, 35, 41, 43, 54, 63, 34, 42, 59, 49, 48, 52, 39, 39, 45, 32, 39, 49, 43, 37, 47, 50, 46, 57, 45, 57, 56]
Lab: (30) [31, 28, 34, 47, 42, 33, 35, 23, 19, 44, 34, 21, 28, 29, 34, 36, 39, 31, 11, 29, 33, 45, 52, 41, 30, 38, 28, 29, 25, 22]
LibDem: (30) [9, 9, 10, 9, 11, 11, 8, 8, 9, 10, 9, 9, 14, 9, 4, 8, 8, 10, 6, 5, 10, 8, 5, 7, 11, 7, 9, 14, 11, 14]
Other: (30) [0, 1, 0, 1, 1, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 1, 0, 1, 0, 0, 1, 1, 1, 1, 0]
Brexit: (30) [3, 3, 2, 0, 3, 2, 4, 3, 1, 2, 3, 2, 1, 2, 4, 4, 2, 4, 1, 4, 3, 3, 2, 2, 5, 3, 2, 4, 1, 3]
Green: (30) [4, 4, 4, 6, 3, 5, 5, 5, 2, 4, 5, 3, 4, 4, 3, 4, 4, 5, 3, 1, 4, 2, 3, 3, 4, 5, 4, 8, 4, 5]
SNP: (30) [4, 5, 4, 4, 4, 8, 3, 6, 3, 4, 5, 4, 3, 4, 4, 6, 6, 3, 47, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
Plaid: (30) [1, 1, 1, 0, 1, 1, 1, 2, 1, 1, 1, 2, 0, 2, 0, 1, 2, 1, 0, 22, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
Date: "20/02/2020"
__proto__: Object
This is the eror report when I try to access {Data.Con[0]} from the return statement:
TypeError: Cannot read property '0' of undefined
First time when your component renders, the data might not be there yet.
My suggestion would be to set the data to null by default, so change this line
const [data, setData] = useState([]);
to
const [data, setData] = useState(null);
Then, before trying to rendering something, check it is there
<div className="dashboard">{data ? data.Con[0] : null }</div>
If you will still have issues, you might want to post the axios's response data structure.
Update #1
import React, { useState, useEffect } from "react";
import axios from 'axios'
import './App.css';
export default function App() {
const [data, setData] = useState();
useEffect(() => {
axios.get('/api/comRes', { headers:
{ "Access-Control-Allow-Origin": "http://localhost:3000/" }
}).then(function (data) {
setData(data.data);
})
}, []);
return (
<div className="grid-container">
<div className="menu"></div>
<div className="dashboard">{data ? data.Con[0] : null}</div>
</div>
);
}
It should just be {data.Con[0]}

Working with Charts in motion [Animating chart]

I have created a areaspline chart using highcharts library and making it animated with play/pause button transition between weeks of data
Ref. https://www.highcharts.com/blog/tutorials/176-charts-in-motion/
jsfiddle Ref. https://jsfiddle.net/larsac07/wkev75nL/?utm_source=website&utm_medium=embed&utm_campaign=wkev75nL
in the above example, we are animating the chart week wise
dataSet used :
dataSequence = [
{
name: 'Week 1',
data: [1, 2, 2, 1, 1, 2, 2]
}, {
name: 'Week 2',
data: [6, 12, 2, 3, 3, 2, 2]
}, {
name: 'Week 3',
data: [4, 5, 6, 5, 5, 4, 9]
}, {
name: 'Week 4',
data: [5, 5, 6, 6, 5, 6, 6]
}, {
name: 'Week 5',
data: [6, 7, 7, 6, 6, 6, 7]
}, {
name: 'Week 6',
data: [8, 9, 9, 8, 8, 8, 9]
}, {
name: 'Week 7',
data: [9, 10, 4, 10, 9, 9, 9]
}, {
name: 'Week 8',
data: [1, 10, 10, 10, 10, 11, 11]
}, {
name: 'Week 9',
data: [11, 11, 12, 12, 12, 11, 11]
}
]
I don't want change chart
on play button click I want animate the data points 11 data point for week1 to same data point but different value on y axis for week2
xAxis = ["week1", "Week2", ..... ],
yAxis = [[1,2,3,4,5,6,7,8,9,10,11], [3,5,7,8,2,1,5,7,6,1,10], ....]
on the play button it would transit between week1 then will go to week 2 and so till last week number available.
Trying to have something like this Ref. https://aatishb.com/covidtrends/
this chart is plotted using this dataset for series
Highcharts.chart("container", {
chart: {
type: "areaspline"
},
tooltip: {
shared: true,
valueSuffix: " units"
},
xAxis: {
categories: [
"Week 1",
"Week 2",
"Week 3",
"Week 4",
"Week 5",
"Week 6",
"Week 7"
]
},
yAxis: {
title: {
text: "Index"
}
},
legend: {
layout: "horizontal",
align: "right",
verticalAlign: "top",
x: 50,
y: 50,
floating: true,
borderWidth: 1,
backgroundColor:
(Highcharts.theme && Highcharts.theme.legendBackgroundColor) ||
"#FFFFFF"
},
plotOptions: {
areaspline: {
fillOpacity: 0.5
}
},
credits: {
enabled: false
},
series: [
{
name: "By week",
data: dataSequence[value].data.slice()
},
{
type: "spline",
name: "Topic 1",
data: [3, 2, 1, 3, 4, 7, 8]
},
{
type: "spline",
name: "Topic 2",
data: [1, 5, 1, 3, 4, 7, 8]
},
{
type: "spline",
name: "Topic 3",
data: [3, 7, 1, 3, 4, 7, 8]
},
{
type: "spline",
name: "Topic 4",
data: [5, 1, 1, 3, 4, 7, 8]
},
{
type: "spline",
name: "Topic 5",
data: [7, 3, 1, 3, 4, 7, 8]
},
{
type: "spline",
name: "Topic 6",
data: [9, 2, 1, 3, 4, 7, 8]
},
{
type: "spline",
name: "Topic 7",
data: [11, 8, 1, 3, 4, 7, 8]
},
{
type: "spline",
name: "Topic 8",
data: [13, 11, 1, 3, 4, 7, 8]
},
{
type: "spline",
name: "Topic 9",
data: [15, 7, 1, 3, 4, 7, 8]
},
{
type: "spline",
name: "Topic 10",
data: [7, 5, 1, 3, 4, 7, 8]
}
],
title: {
text: ""
},
subtitle: {
text: "Efficiency Index of Topics"
}
});
this my update function in react
update(increment) {
var input = $("#play-range")[0];
var output = $("#play-output")[0];
if (increment) {
input.value = parseInt(input.value) + increment;
}
output.innerHTML = this.state.dataSequence[input.value].name;
this.setState({
value: input.value
});
if (input.value >= input.max) {
// Auto-pause
this.pause();
this.setState(
{
value: 0
},
() => {
output.innerHTML = this.state.dataSequence[0].name;
}
);
}
}
the whole chart is plotted at once, I need something that it should transit
first, it plots all data points for week1 then week 2 after that week 3 when I click on the play button
You need to start with an empty data and use addPoint method in the update function:
function update(increment) {
var input = $('#play-range')[0],
output = $('#play-output')[0],
increment;
chart.series[0].addPoint(dataSequence[input.value].data[actualPointIndex]);
actualPointIndex += increment;
if (actualPointIndex === 6) {
actualPointIndex = 0;
input.value = parseInt(input.value) + increment;
}
output.innerHTML = dataSequence[input.value].name; // Output value
if (input.value >= input.max) { // Auto-pause
pause($('#play-pause-button')[0]);
}
}
Live demo: https://jsfiddle.net/BlackLabel/stpxyfca/
API Reference: https://api.highcharts.com/class-reference/Highcharts.Series#addPoint

Mongo Sort Documents by value in array

I have a list of Students. Each one has an array of points they've earned by category in an array named lightTrail. I want the top 5 people with the most behavior points which is shown at index 1 in the lightTrail array. If it is a tie the second sort field is their total points which is shown at index 0.
Here is what I tried:
db.students.aggregate([{
$project: {
fname: 1,
surname: 1,
lightTrails: 1,
'_id': 0
}
},
{
$sort: {
"lightTrails.1": -1,
"lightTrails.0": -1
}
}, {
$limit: 5
}
], {
allowDiskUse: true
})
This is the result I wanted:
{
"fname": "Jim",
surname: "Jones",
lightTrails: [200, 70, 30, 30, 15, 15, 40]
}, {
"fname": "Sean",
surname: "Marx",
lightTrails: [180, 50, 50, 20, 20, 15, 25]
}, {
"fname": "Todd",
surname: "Lull",
lightTrails: [150, 40, 60, 15, 15, 10, 10]
}, {
"fname": "Flynn",
surname: "Moore",
lightTrails: [100, 40, 25, 15, 5, 10, 5]
}, {
"fname": "Al",
surname: "Ryan",
lightTrails: [80, 20, 20, 10, 10, 10, 10]
}
The result I got was just 5 students no order:
{
"fname": "Flynn",
surname: "Moore",
lightTrails: [100, 40, 25, 15, 5, 10, 5]
}, {
"fname": "Jim",
surname: "Jones",
lightTrails: [200, 70, 30, 30, 15, 15, 40]
}, {
"fname": "Al",
surname: "Ryan",
lightTrails: [80, 20, 20, 10, 10, 10, 10]
}, {
"fname": "Todd",
surname: "Lull",
lightTrails: [150, 40, 60, 15, 15, 10, 10]
}, {
"fname": "Sean",
surname: "Marx",
lightTrails: [180, 50, 50, 20, 20, 15, 25]
}
I found 2 similar questions but one was for an array of documents which didn't work for me and the other wasn't really asking the same question at all.
You can achieve this by find query. No need to use aggregation.
Query:
db.students.find({},{fname:1,surname:1,lightTrails:1,_id:0}).sort({"lightTrails.0":-1,"lightTrails.1":-1}).limit(5)
Result:
{ "fname" : "Jim", "surname" : "Jones", "lightTrails" : [ 200, 70, 30, 30, 15, 15, 40 ] }
{ "fname" : "Sean", "surname" : "Marx", "lightTrails" : [ 180, 50, 50, 20, 20, 15, 25 ] }
{ "fname" : "Todd", "surname" : "Lull", "lightTrails" : [ 150, 40, 60, 15, 15, 10, 10 ] }
{ "fname" : "Flynn", "surname" : "Moore", "lightTrails" : [ 100, 40, 25, 15, 5, 10, 5 ] }
{ "fname" : "Al", "surname" : "Ryan", "lightTrails" : [ 80, 20, 20, 10, 10, 10, 10 ] }

Resources