我有以下虚拟数据.
set.seed(45)
df <- data.frame(x=rep(1:5, 9), val1=sample(1:100,45),
val2=sample(1:100,45),variable=rep(paste0("category",1:9),each=5))
Run Code Online (Sandbox Code Playgroud)
我想基于x绘制val1和val2(在我的实际数据中是一个日期值序列).我怎样才能做到这一点.我试过ggplot2,mplot,并且一切都没有成功.我也查看了其他类似的帖子,但它们不起作用或满足我的需求.
谢谢.
我有以下代码从父组件使用 props 传入的对象返回一个带有键和值对的列表元素。为什么不起作用?
我收到一条错误消息:TypeError: Cannot read property 'props' of undefined(...) 该错误是因为我的 return 语句中的 this.props.data 调用。数据不通过道具传递不是问题。我查看了反应输出树,我的数据对象确实传递给了孩子。
class Parent extends React.Component {
render(){
return (
<div>
{this.props.transactions.map(function(el,i) {
return <div key={i}>
<div>
{el.category}
</div>
<Child data={el.months}/>
</div>;
})}
</div>
);
}
}
class Child extends React.Component {
render(){
return (
<ul>
{Object.keys(this.props.data).map(function(key,index) {
return <li key={index}>{this.props.data[key]}</li>
})};
</ul>
);
}
}
Run Code Online (Sandbox Code Playgroud)
下面是父组件传入的数据:
[{
"category": "Bills",
"month": {
"feb": 9,
"mar": 169,
"apr": 10,
"may": 867,
"jun": 394,
"jul": 787,
"aug": 1112,
"sep": …Run Code Online (Sandbox Code Playgroud) 我在带有 redux 的 React 应用程序中使用交叉提取。在我的 reducer 中,我使用 cross fetch 的 post 方法将一些数据保存到数据库中。我的调用返回一个响应,其中包含一些我需要在保存后分配给状态的数据。我在解析数据时遇到了麻烦。下面是响应的样子。如何获取分配给 body_init 的数据?
Response
headers
:
Headers {map: {…}}
ok
:
true
status
:
200
statusText
:
"OK"
type
:
"default"
url
:
"http://localhost:3001/api/updateTransactions"
_bodyInit
:
"[{"category":"Dining Out","months":{"1":0,"2":0,"3":0,"4":0,"5":0,"6":0,"7":0,"8":0,"9":0,"10":0,"11":0,"12":0}},{"category":"Auto","months":
Run Code Online (Sandbox Code Playgroud)
: : :
这是完整的电话。我的困惑是从api返回的数据实际上已经格式化为json(api返回:res.json(items))
export function saveTransactions(transUpdate,year) {
return function (dispatch) {
dispatch(requestTransactionsSave(transUpdate))
return fetch('http://localhost:3001/api/updateTransactions',
{
method: 'post',
body: JSON.stringify(transUpdate),
headers:{'Content-Type': 'application/json'}
})
.then(
response => response,
error => console.log('An error occurred.', error)
)
.then(res =>
dispatch(completeTransactionsSave(res))
)
Run Code Online (Sandbox Code Playgroud)
} }
调用调度以获取 React 组件的初始数据的最佳方法是什么。我的理解是 ComponentWillMount 在渲染之前被调用。所以理论上,如果我在 ComponentWillMount 上调用 dispatch,当我点击渲染然后 ComponentDidMount 时,我应该在组件的道具中拥有我的数据,对吧?我没有看到。
我看到 render 被调用了两次,并且在初始化组件时第一次调用时,我无法访问 props 中的数据。似乎在第二次渲染之前实际上并没有调用 dispatch 。我基本上希望了解在最初设置组件时调用调度的最佳方式。我基本上是在尝试执行以下操作,其中我使用容器组件从调度中获取数据,然后将其作为道具传递给子组件。但我也想在 ContainerComponent 中初始化一些状态变量,然后将它们作为 props 传递给 ChildComponent。问题是我想要初始化的状态变量取决于从调度返回的数据,理想情况下我会在 ComponentWillMount 或 ComponentDidMount 中进行初始化。
import React from 'react';
import axios from 'axios';
import { connect } from 'react-redux';
import ChildComponent from './ChildComponent.js';
import { getTransactionsAll } from '../actions/actions.js';
class ContainerComponent extends React.Component {
constructor() {
super();
this.state = {
acctList:[],
acctChecked:[],
categoryList:[]
}
}
componentWillMount() {
console.log("componentWillMount entered");
this.props.get_data();
console.log(this.props.searchProps.transactions_all);//this is undefined meaning the dispatch has not assigned …Run Code Online (Sandbox Code Playgroud) 不确定这是否会被视为重复,但我已经四处搜索并实现了与我在网上找到的类似查询,但似乎无法让我的嵌套引用起作用。我只是测试以了解用于填充嵌套引用和嵌套文档的猫鼬语法,如下所述:猫鼬嵌套模式与嵌套模型
但是,我一定错过了一些东西,因为它似乎可以工作,但返回一个空的嵌套引用数组。我知道我的查询应该返回嵌套引用的两个结果。
数据:
结果收集:
{
"_id" : ObjectId("5a4dcbe4ab9a793d888c9396"),
"event_id" : ObjectId("5a482302a469a068edc004e3"),
"event_name" : "Sample Event",
"score" : "3-2",
"winner" : "player1"
},
{
"_id" : ObjectId("5a59791379cc1c321c1918f0"),
"event_id" : ObjectId("5a482302a469a068edc004e3"),
"event_name" : "Sample Event",
"score" : "2-1",
"winner" : "player2"
}
Run Code Online (Sandbox Code Playgroud)
活动合集:
{
"_id" : ObjectId("5a482302a469a068edc004e3"),
"type" : "Tournament",
"name" : "Sample Event"
}
Run Code Online (Sandbox Code Playgroud)
我的代码如下:
var mongoose = require("mongoose");
mongoose.connect("MongoDB://localhost/devDB");
var ResultSchema = mongoose.Schema({
_id: mongoose.Schema.Types.ObjectId,
event_id: {type: mongoose.Schema.Types.ObjectId, ref: "EventModel"},
event_name: String,
score: String,
winner: String
}); …Run Code Online (Sandbox Code Playgroud) 我已通读了有关.Net Core 2.1中不同的设置和访问配置方式的文档,以及有关建议使用的选项模式(https://docs.microsoft.com/zh-cn/aspnet/core/fundamentals /configuration/options?view=aspnetcore-2.1)。但是,我似乎无法获得想要的工作:
我已经完成以下工作:
AppSettings:
{
"ConnectionStrings": {
"DefaultConnStr": "Server=(localdb)\\MSSQLLocalDB;Database=_CHANGE_ME;Trusted_Connection=True;MultipleActiveResultSets=true;Integrated Security=true",
"AW2012ConnStr": "Server=localhost;Database=AW2012;Trusted_Connection=True;MultipleActiveResultSets=true;Integrated Security=true"
}
}
Run Code Online (Sandbox Code Playgroud)
MyConfig:
public class MyConfig
{
public string AWConnStr { get; }
public string DefaultConnStr { get; }
}
Run Code Online (Sandbox Code Playgroud)
启动:
public class Startup
{
public IConfiguration _config { get; set; }
public Startup(IHostingEnvironment env)
{
var builder = new ConfigurationBuilder()
.SetBasePath(env.ContentRootPath)
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true);
_config = builder.Build();
}
public void ConfigureServices(IServiceCollection services)
{
services.AddOptions();
//add config to services for dependency …Run Code Online (Sandbox Code Playgroud) 我有个约会.我想将所有日期部分读出到单独的字符串中,并在适用的情况下使用前导0,然后将它们连接起来创建一个日期字符串,它只是没有任何分隔符的数字.
例如:
DateTime dt = DateTime.Now;
string year = dt.Year.ToString();
string month = dt.Month.ToString();
string day = dt.Day.ToString();
string hour = dt.Hour.ToString();
string minutes = dt.Minute.ToString();
string seconds = dt.Second.ToString();
string finalDt = string.Concat(year, month, day, hour, minutes, seconds);
Run Code Online (Sandbox Code Playgroud)
如果是1月,我想月是01,如果是第3天则是03,并且同样以小时,年,秒.有没有办法实现这一点,而无需检查每个日期部分的计数,并用前导0填充它?
如果有更好的方法来完成我想要做的整体,那么我想建议.
我对以下脚本的结果感到困惑,我不明白为什么会这样:
enddate = '01-02-2020'; //euro format dd-mm-yyyy
datesplit = enddate.split("-");
console.log("datesplit: ", datesplit); //[ '01', '02', '2020' ]
console.log(datesplit[2]); // 2020
console.log(datesplit[1]); // 02
console.log(datesplit[0]); // 01
enddate1 = new Date(datesplit[2],datesplit[1],datesplit[0]);
console.log("enddate 1", enddate1); //output: 2020-03-01T05:00:00.000Z , but I'm expecting 2020-02-01T00:00:00.000Z
Run Code Online (Sandbox Code Playgroud)
最后一个控制台日志输出是我无法理解的。我很感激解释为什么结果是这样的。
当我尝试使用 Object.keys() 读取其键时,有一个对象评估为未定义。但是,当我执行 Object.keys().length 时,我得到 1。
如果有除未定义或空之外的任何键,如何测试对象是否存在。
我已尝试以下操作,但仍然出现错误。我本质上只是检查第一个元素以查看它是否为空或未定义。我猜想可能有更好的方法来运行这种检查。
我在谷歌上搜索并看到了处理特定属性搜索的案例,但这不是我的对象的设置方式。我实际上有一个带有嵌套对象的对象。Player1并且Player2可以是任何值。
var data =
{
player1:
{ team: team1,
score: 6 },
player2:
{ team: team2,
score: 4}
}
Run Code Online (Sandbox Code Playgroud)
这是我的对象的外观,undefined当我尝试循环并将其显示在表格中时,它会显示出来。
{ undefined:
{ team: undefined,
score: '0'} }
Run Code Online (Sandbox Code Playgroud)
正如您所看到的,进行长度检查将返回 1。所以我猜我必须检查第一个元素data[0]?该检查有效,但这是处理此问题的最佳方法吗?
if (Object.keys(data[0]) === null){
do something
}
Run Code Online (Sandbox Code Playgroud) 我有存储为数字或 varchar 值的数据。
我正在执行以下操作,以便如果值是数字,我只想显示它是什么。如果值是数字,我想对其应用计算。我怎样才能做到这一点?
目前,我收到错误:
将数据类型 nvarchar 转换为数字时出错
这是我的选择语句:
select
case
when IsNumeric(value) = 1
then cast(value as decimal(6, 2)) / coalesce(divider)
else value
end as [value]
Run Code Online (Sandbox Code Playgroud) javascript ×3
reactjs ×3
.net-core ×1
asp.net-core ×1
c# ×1
date ×1
datetime ×1
fetch ×1
ggplot2 ×1
mongoose ×1
node.js ×1
object ×1
plot ×1
r ×1
react-redux ×1
sql-server ×1
t-sql ×1