我有一个具有以下结构的对象数组:
var arr = [
{
"value": "abc",
"checked": true
},
{
"value": "xyz",
"checked": false
},
{
"value": "lmn",
"checked": true
}
];
let result = arr.filter(item => item.checked);
console.log(result);Run Code Online (Sandbox Code Playgroud)
我希望输出为:
["abc", "lmn"]
Run Code Online (Sandbox Code Playgroud)
因为那两个value都有checked: true。
我尝试根据检查的值过滤掉:
let result = arr.filter(item => item.checked);
Run Code Online (Sandbox Code Playgroud)
我正在获取具有checked设置为的属性值的对象true。
帮助将不胜感激。
我有一个具有以下格式的对象
var obj = [{
"a": 1
}, {
"b": 2
}, {
"c": 3
}];
Run Code Online (Sandbox Code Playgroud)
只想将此对象数组内的每个对象中的键提取到新数组中
像这样的东西:
["a","b","c"]
已尝试以下方法但不起作用:
var obj = [{
"a": 1
}, {
"b": 2
}, {
"c": 3
}];
Run Code Online (Sandbox Code Playgroud)
一直尝试在模板字符串中添加条件。我做不到。我有两个字符串,即 t1 和 t2,当 t2 未定义或为空时,仅显示 t1,当 t2 存在时,将 t2 与 t1 一起附加在括号内
let t1= "hey";
let t2 = "there";
//need the output something like hey(there) when there t2 is present. when it is null or undefined or empty just show hey
//Have tried the below but it is not working
console.log(`${t2} ${t1} ? ${t1}(${t2}): ${t1}`)
Run Code Online (Sandbox Code Playgroud) 我正在尝试打印某些 API 返回的布尔值和时间戳值。我无法做同样的事情。帮助将不胜感激
列配置对象:
<ReactTable
data={this.state.data}
columns={[
{
Header: 'Name',
accessor: 'name'
},
{
Header: 'Age>18',
accessor: d => d.isAgeAbove18.toString(); // this does not work
},
{
Header: 'TimeStamp',
accessor: d => d.timeVal.toString(); // this does not work
},
]}
/>
Run Code Online (Sandbox Code Playgroud) 我试图使用语义-ui-react提供的Spinner来覆盖react-table提供的默认加载器。但这似乎不起作用。我维护了三个组件;一个是应用程序,一个用于数据网格,另一个用于微调器。有人可以帮我吗?
沙箱:https://codesandbox.io/s/react-table-row-table-g3kd5
应用程序组件
import * as React from "react";
import { render } from "react-dom";
import DataGrid from "./DataGrid";
class App extends React.Component {
constructor(props) {
super(props);
this.state = {
data: [],
columns: [],
loading: true
};
}
componentDidMount() {
this.getData();
this.getColumns();
}
getData = () => {
let data = [
{ firstName: "Jack", status: "Submitted" },
{ firstName: "Simon", status: "Pending" },
{ firstName: "Pete", status: "Approved" }
];
setTimeout(() => {
this.setState({ data, loading: false }); …Run Code Online (Sandbox Code Playgroud) 使用 react router v4 和 Typescript 以编程方式导航时出错:
'Readonly & Readonly<{ children?: ReactNode; 类型上不存在属性“历史” }>
我想在 API 调用成功或失败时重定向到特定路径。但无法做到这一点。
import { BrowserRouter as Router , Switch , Route} from 'react-router-dom';
import * as React from 'react';
class App extends React.Component<{}, {}> {
render()
{
<Router>
<Switch>
<Route exact={true} path='/' component={Login}/>
<Route path='/home' component={Home}/>
<Route path='/test' component={Test}/>
</Switch>
</Router>
}
}
export default App;
Run Code Online (Sandbox Code Playgroud)
import { withRouter } from "react-router-dom";
import * as React from 'react';
class Test extends React.Component<IProps, IState> …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用语义-ui-反应和打字稿来打开一个下拉菜单。我面临的问题是,我无法将更改处理程序附加到它。它抛出以下错误“类型'SyntheticEvent'不可分配给类型'ChangeEvent'”。
//Importing Dropdown
import Dropdown from 'semantic-ui-react/dist/commonjs/modules/Dropdown';
//options array
const options = [
{ key: 1, text: 'Location 1', value: 1 },
{ key: 2, text: 'Location 2', value: 2 },
{ key: 3, text: 'Location 3', value: 3 },
]
//state object
this.state ={
value : ''
}
//Dropwdown change event
dropdownChange = (event: React.ChangeEvent<HTMLSelectElement> ,{value}) =>{
this.setState({ value });
}
//Rendering Drop down
render()
{
return(
<Dropdown
options={opitons}
name="value"
value={this.state.value}
onChange={this.dropdownChange}
/>
)
}
Run Code Online (Sandbox Code Playgroud) 在这种情况下,我只需要获取对象数组中的键。对象结构如下所示。我也尝试了一种方法,但是它似乎不起作用。有人可以帮我这个忙。
var arr = [
{ firstName: "aaaaa", status: 0, visits: 155 },
{ firstName: "aabFaa", status: 0, visits: 155 },
{ firstName: "adaAAaaa", status: 10, visits: 1785 },
{ firstName: "aAaaaa", status: 50, visits: 175 },
{ firstName: "aaaaa", status: 670, visits: 155 },
]
console.log([...new Set(arr.map(item => Object.keys(item)))]); // This does not workRun Code Online (Sandbox Code Playgroud)
我希望输出只是 ['firstName','status','visits']
一直在尝试做以下事情:
我有一系列对象,
var arr = [
{ key: "aabFaa", text: "aabFaa" ,field: "firstName",checked: true},
{ key: "aAaaaa", text: "aAaaaa", field: "firstName", checked: true },
];
Run Code Online (Sandbox Code Playgroud)
想要从中获取“文本”和“字段”并形成一个新的对象数组,如下所示:
result = [ { "field" : "firstName" , value : "aabFaa" , type :"add"},
{ "field" : "firstName" , value : "aAaaaa" , type: "add"}
]
Run Code Online (Sandbox Code Playgroud)
这里的类型是硬编码的,其余部分是从“arr”中获取的
有什么更简单的方法可以做到这一点?
已经尝试过这个:
var arr = [
{ key: "aabFaa", text: "aabFaa" ,field: "firstName",checked: true},
{ key: "aAaaaa", text: "aAaaaa", field: "firstName", checked: true },
];
Run Code Online (Sandbox Code Playgroud)
但这必须写成多行才能获得所需的属性。有更简单的方法吗?
我正在尝试在对象数组中映射一些字段,在这种情况下,这是字段名和排序顺序。
我试图实现服务器端排序功能,每当我单击一个字段时,服务器中的服务器都会采用字段名称和排序类型。我只需要使用排序类型(ASCENDING或DESCENDING)来映射字段名称。
我编写了一个示例,其中维护了一个带有类型的对象的示例数组。在单击该列时,我需要确定其排序顺序
有人可以帮忙吗,只需要用字段名称来实现排序的标签
沙箱:https://codesandbox.io/s/hopeful-wescoff-08x8x
import React from "react";
import ReactDOM from "react-dom";
import { render } from "react-dom";
interface IState {
sorting: any;
}
interface IProps {}
export default class App extends React.Component<IProps, IState> {
constructor(props: any) {
super(props);
this.state = {
sorting: [{ firstName: "" }, { lastName: "" }]
};
}
sortHandler = name => {
const sorting = Object.keys(this.state.sorting).reduce((obj, key) => {
if (key === name) {
obj[key] = this.state.sorting[key] === "ASC" ? …Run Code Online (Sandbox Code Playgroud) 我正在尝试实现一个全局搜索过滤器,用于在整个表中搜索键。我正在附加一个更改处理程序,并在每个输入上触发一个回调,该回调在该数据中搜索该键并正在设置该值。它在我输入字符时被过滤,但我希望当我输入多个时搜索工作在搜索输入中搜索像 string1,string2 这样的值
代码沙盒:https : //codesandbox.io/s/jolly-bhabha-iqcx1
import React from "react";
import ReactDOM from "react-dom";
import { Input } from "semantic-ui-react";
import ReactTable from "react-table";
import "react-table/react-table.css";
import "./styles.css";
export default class App extends React.Component {
constructor(props) {
super(props);
this.state = {
data: [
{ firstName: "aaaaa", status: "Pending", visits: 155 },
{ firstName: "aabFaa", status: "Pending", visits: 155 },
{ firstName: "adaAAaaa", status: "Approved", visits: 1785 },
{ firstName: "aAaaaa", status: "Approved", visits: 175 },
{ firstName: "adaSaaa", …Run Code Online (Sandbox Code Playgroud) 我有以下格式的对象数组
var obj = [{
"a": 1
}, {
"b": 2
}, {
"c": 3
}];
Run Code Online (Sandbox Code Playgroud)
想要从该对象数组内的每个对象中分别提取键和值到一个新数组中
像这样:
[{"key:"a","val:"1"],{"key":"b","val":"2"},{"key": "c","val":"3"]}
尝试了以下方法,但是它不起作用:
var obj = [{
"a": 1
}, {
"b": 2
}, {
"c": 3
}];
Run Code Online (Sandbox Code Playgroud)
假设给定一个字符串,我怎样才能得到字符,不。单个查询中的出现次数和出现索引?
例如:顾问
字符出现次数索引:
C 1 1
O 1 2
N 2 3,9
Run Code Online (Sandbox Code Playgroud) javascript ×9
ecmascript-6 ×8
reactjs ×6
typescript ×5
arrays ×3
react-table ×3
object ×2
setstate ×2
sql ×1
sql-server ×1