我有一个对象数组。如何从 1 开始向它们添加 id 键。
[
{
color: "red",
value: "#f00"
},
{
color: "green",
value: "#0f0"
},
{
color: "blue",
value: "#00f"
},
{
color: "cyan",
value: "#0ff"
},
{
color: "magenta",
value: "#f0f"
},
{
color: "yellow",
value: "#ff0"
},
{
color: "black",
value: "#000"
}
]
Run Code Online (Sandbox Code Playgroud)
所以,它会像
[
{
color: "red",
value: "#f00",
id: 1
},
{
color: "green",
value: "#0f0",
id: 2
},
{
color: "blue",
value: "#00f",
id: 3
},
{
color: "cyan",
value: "#0ff", …Run Code Online (Sandbox Code Playgroud) 我最近升级到 Yarn2 并将以下内容添加到我的.gitignore
.yarn/*
!.yarn/cache
!.yarn/releases
!.yarn/plugins
!.yarn/sdks
!.yarn/versions
Run Code Online (Sandbox Code Playgroud)
我仍然使用nodeLinker: node-modules,但是当我推送代码时,.yarn/cache每当升级软件包时,我都会推送带有新文件的文件夹。
该文件夹应该推送到 Git 还是只推送到我的机器中?请指教。
我在 React 中有一个 dataService 函数来执行我的 API 获取。我尝试转换为 async/await 块,但似乎遇到了障碍。
使用承诺:
const dataService = (url, options, dataToPost) => {
return (dispatch, getState) => {
const { requestAction, successAction, failureAction } = options.actions;
if (options.shouldRequest(getState())) {
dispatch(requestAction());
const promise = axios.get(url, { withCredentials: true });
return promise
.then(response => {
if (response.status === 200) {
return dispatch(successAction(response, dispatch));
}
return Promise.reject(response);
})
.catch(error => {
if (error.response.status === 302) {
window.location = '/view';
}
dispatch(openErrorDialog());
return dispatch(failureAction(error));
});
}
return Promise.reject(new Error('FETCHING')); …Run Code Online (Sandbox Code Playgroud) 我怎样才能编写一个比较两个字符串的函数,并返回一个包含两个字符串中出现的字符的对象,以及它们出现的次数?
这是一些示例输入和输出:
"Leopard", "Lion" // { "L": 2, "o": 2 }
"Hello", "World" // { "l": 3, "o": 2 }
"Flower", "Power" // { "o": 2, "w": 2, "e": 2, "r": 2 }
"phone", "Memphis" // { "p": 2, "h": 2, "e": 2 }
Run Code Online (Sandbox Code Playgroud) 我有一个数组,我需要将这个数组映射到它的子数组中。
var data = [{
name: 'Cars',
content: 'BMW',
value: 2000
},
{
name: 'Cars',
content: 'Fiat',
value: 542
},
{
name: 'Cars',
content: 'Mercedes',
value: 745
},
{
name: 'Cars',
content: 'Toyota',
value: 965
},
{
name: 'Cars',
content: 'Honda',
value: 754
}, {
name: 'Cars',
content: 'VW',
value: 123
},
{
name: 'Cars',
content: 'Ford',
value: 200
},
{
name: 'Fruits',
content: 'Apple',
value: 500
}, {
name: 'Fruits',
content: 'Orange',
value: 769
}, {
name: 'Fruits',
content: …Run Code Online (Sandbox Code Playgroud) 我有一个带有两个键的枚举。我想根据枚举的键过滤数据并返回一个对象。
enum ArchiveStatus {
Current = 0,
Archive = 1,
}
const data = [{name:'A', Archive_Status: 0}, {name: 'B', Archive_Status: 0}, {name: 'C', Archive_Status: 1}, {name: 'D', Archive_Status: 1}];
const filteredData = {
Current: data.filter(item => item.Archive_Status === ArchiveStatus.Current),
Archive: data.filter(item => item.Archive_Status === ArchiveStatus.Archive)
}
console.log(filteredData)
Run Code Online (Sandbox Code Playgroud)
预期结果:
{ "Current": [ { "name": "A", "Archive_Status": 0 }, { "name": "B", "Archive_Status": 0 } ], "Archive": [ { "name": "C", "Archive_Status": 1 }, { "name": "D", "Archive_Status": 1 } ] } …Run Code Online (Sandbox Code Playgroud) 我有一个嵌套的对象数组。我该如何使对象数组也从嵌套属性中获取值?如果onClick属性为空,则意味着存在一个称为子级的属性,并且父元素不应成为新列表的一部分。我需要遍历children数组并获取值。请在下面查看预期的输出。
const headers = [{
title: 'Arun',
id: 'arunId',
onClick: 'onClickArun'
},
{
title: "George",
id: 'georgeId',
onClick: '',
children: [{
title: 'David',
id: 'davidId',
onClick: 'onClickDavid'
},
{
title: 'Patrick',
id: 'patrickId',
onClick: 'onClickPatrick'
}
]
},
{
title: 'Mark',
id: 'markId',
onClick: 'onClickMark'
}
];
console.log(headers.map(item => {
return {
title: item.title,
onClick: item.onClick
}
}))Run Code Online (Sandbox Code Playgroud)
预期产量:
[{
title: 'Arun',
onClick: 'onClickArun'
},
{
title: 'David',
onClick: 'onClickDavid'
},
{
title: 'Patrick',
onClick: 'onClickPatrick'
},
{
title: 'Mark', …Run Code Online (Sandbox Code Playgroud) 我有一个字符串,其中包含单词,后跟冒号。我需要用对象中的值替换该字符串中的冒号。我能够提取出冒号单词,但不确定在字符串中替换它的最佳方法。
这就是我所拥有的:
const string = 'This is :state :buttonName by :name';
const buttonName = 'button link';
const data = {
state: 'Alabama',
name: 'Arun'
}
const res = string.match(/:[a-zA-Z]+/g).map(i => i.replace(':', ''))
console.log(res)
// This is Alabama button link by ArunRun Code Online (Sandbox Code Playgroud)
最终结果应该是
This is Alabama button link by Arun
Run Code Online (Sandbox Code Playgroud)
请指教。
我正在使用材质 V5。如何设置所选列表项的样式?我想要 5px 的 borderLeft。
像这样的事情:
const theme = createTheme({
palette: {
primary: {
// light: will be calculated from palette.primary.main,
main: colors.primary
// dark: will be calculated from palette.primary.main,
}
},
components: {
MuiListItem: {
styleOverrides: {
root: {
"&$selected": {
borderLeft: `5px solid ${colors.primary}`
}
}
}
}
}
});
Run Code Online (Sandbox Code Playgroud)
这是我的代码和框:
import * as React from 'react';
import Box from '@material-ui/core/Box';
import List from '@material-ui/core/List';
import ListItemButton from '@material-ui/core/ListItemButton';
import ListItemIcon from '@material-ui/core/ListItemIcon';
import ListItemText from '@material-ui/core/ListItemText'; …Run Code Online (Sandbox Code Playgroud) 嗨,我有一个案例,我在输入表单时禁用提交按钮,只在输入框有一些文本时启用它.
<div id="app-id-input-container" ng-form="appIdInput">
<div class="input-group">
<input id="app-id-input" name="appIdInput" ng-click="clearAppIdInput()" class="form-control" type="text" ng-model="appId" pattern="^[AaIi][PpXxNn][0-9]{6}$" maxlength="8" />
<span class="input-group-btn">
<button id="addAppId" class="btn btn-success" ng-click="preferences.appIdInput.$valid && addAppId()" type="button"> <span class="glyphicon glyphicon-plus"></span></button>
</span>
</div>
<span class="validation-alert" ng-show="appIdError">{{appIdError}}</span>
</div>
Run Code Online (Sandbox Code Playgroud)
当用户在输入框内单击时,我清除该字段.
$scope.clearAppIdInput = function() {
$scope.appId = "";
};
Run Code Online (Sandbox Code Playgroud)
即使范围为空,也不会禁用该按钮.
这是我禁用按钮的方式.
$(document).ready(function(){
$('#addAppId').prop('disabled',true);
$('#app-id-input').keyup(function(){
$('#addAppId').prop('disabled', this.value == "" ? true : false);
});
});
Run Code Online (Sandbox Code Playgroud)
现在,我可以通过单击键盘上的"退格键"再次禁用该按钮?
当我通过点击清除输入字段时,如何再次禁用该按钮?
我有一个 API 每 2.5 秒调用一次。最初,响应对象内的数据为空,因为数据库仍在通过事务对其进行更新。但是在随后的第 3 次或第 4 次尝试中,我得到了数据。我正在为此编写一个可重用的函数,但是我没有定义。我的目标是继续调用 API,直到我获得路径中的值并关闭连接。请指教。
PS:下面的 API URL 没有任何延迟,但我的私有 API 有。
const getData = (url, path) => {
const interval = setInterval(async () => {
const result = await axios.get(url);
if (_.has(result.data, path) && result.data[path]) {
return result[path]
}
}, 2500)
return clearInterval(interval)
}
getData('https://api.oceandrivers.com/static/resources.json', 'swaggerVersion')Run Code Online (Sandbox Code Playgroud)
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.15/lodash.js"></script>Run Code Online (Sandbox Code Playgroud)
请指教。
我有一个 value 的字符串{} "[1, 2, 3, 4]"。我想将其转换为列表。
这是我尝试过的:
char[] delimiterChars = { '[', ',', ']' };
string text = "[1, 2, 3, 4]";
text = text.Replace("[", "");
text = text.Replace("]", "");
List<int> numbers = new List<int>( Array.ConvertAll(text.Split(delimiterChars ), int.Parse) );
System.Console.WriteLine($"{numbers}");
Run Code Online (Sandbox Code Playgroud)
我收到一个错误:
System.Reflection.TargetInvocationException:调用的目标已抛出异常。---> System.FormatException: 输入字符串的格式不正确。
我的预期输出是[1, 2, 3, 4].
请指教。
javascript ×8
arrays ×4
async-await ×2
angularjs ×1
c# ×1
emotion ×1
enums ×1
lodash ×1
material-ui ×1
object ×1
promise ×1
reactjs ×1
regex ×1
setinterval ×1
sorting ×1
string ×1
typescript ×1
yarnpkg-v2 ×1