我有以下功能:
function isBigEnough(element, index, array) {
return (element >= 10);
}
var filtered = [12, 5, 8, 130, 44].filter(isBigEnough);
// filtered is [12, 130, 44]
Run Code Online (Sandbox Code Playgroud)
如何返回大于(或等于)除10?之外的数字的值?例如,array.filter(isBigEnough(15))会给我44, 130
arr.filter(callback[, thisArg])
Run Code Online (Sandbox Code Playgroud)
https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Array/filter
因此,这是我应该理解的文档中的示例。
Arr =一些数组,很棒。filter =数组具有的一种方法,很棒。回调=一些功能,基本上。大。那是[, thisArg]什么
查看一些关于stackoverflow的示例,结果显示:
objects.filter(function (obj) {
return obj.someIntProp <= 1000
});
Run Code Online (Sandbox Code Playgroud)
我了解这是如何工作的,我不需要使用它的帮助。我只想了解[, thisArg]应该如何告诉我该函数采用数组中的每个对象。我觉得我似乎错过了大多数人都可以在Coding 101中学习的显而易见的东西。
这里我有folderObjs数组,这个数组的控制台在下面的代码中,我放置了搜索输入字段,我想通过文件夹名称和文件夹大小以角度搜索这个数组,这怎么可能?
HTML
<mat-form-field>
<input matInput (keyup)="applyFilter($event.target.value)" placeholder="Search">
</mat-form-field>
<div *ngFor="let folder of folderObjs">
<span>{{folder.folderName}}</span>
<span>{{folder.folderSize}}</span>
</div>
Run Code Online (Sandbox Code Playgroud)
TS
folderObjs : Folder[] = [];
applyFilter(filterValue) {
console.log(this.folderObjs);
// 0: {folderid: 781, folderName: "pelu folder",folderActivity: "true", …}
1: {folderid: 782, folderName: "biju folder", folderActivity: "true", …}
filter: "d"
length: 2
this.folderObjs.filter = filterValue.trim().toLowerCase();
}
Run Code Online (Sandbox Code Playgroud) 如何在javascript中收集对象数组,例如我在这种情况下有这个数组:
var abc = [{"name": "udin", "checked": true}, {"name": "kabayan": "checked": true}, {"name": "hebring"}];
Run Code Online (Sandbox Code Playgroud)
如何得到这样的结果:
abc = [{"name": "udin", "checked": true}, {"name": "kabayan": "checked": true}];
Run Code Online (Sandbox Code Playgroud)
我只想显示"checked"== true的元素
我正在尝试在数组内的对象内循环数组内的对象,我真的很困惑!
这是我的arr:
var arr = [
{
title: "Harry Potter",
categories: [
{
id: 39,
name: "Popular Books"
},
{
id: 3,
name: "Kids"
},
{
id: 79,
name: "All Books"
}
]
},
{
title: "Pride and Prejudice",
categories: [
{
id: 36,
name: "Classic Books"
},
{
id: 3,
name: "Woman"
},
{
id: 79,
name: "All Books"
}
]
}
]
Run Code Online (Sandbox Code Playgroud)
如何获取只有类别名称“儿童”的图书的标题?
现在我能想到的只有:
var find = function(arr){
for(var i=0; i<arr.length; i++){
for(var j=0; j<arr[i].categories; j++){
console.log(arr[i].categories[j].name)
} …Run Code Online (Sandbox Code Playgroud) 我有一个data包含以下内容的对象:
{
"content": {
"id": "someID",
"type": "unit",
"method": "xyz",
"blocks": [{
"key": "blue",
"data": [
"Array"
]
}, {
"key": "red",
"data": [
"Array"
]
}, {
"key": "yellow",
"data": [
"Array"
]
}, {
"key": "black",
"data": [
"Array"
]
}],
"notes": "abc"
}
}
Run Code Online (Sandbox Code Playgroud)
我想删除block那个有密钥yellow,通过循环blocks,其余的数据应保持原样.所以预期的最终结果是
{
"content": {
"id": "someID",
"type": "unit",
"method": "xyz",
"blocks": [{
"key": "blue",
"data": [
"Array"
]
}, {
"key": "red",
"data": [
"Array" …Run Code Online (Sandbox Code Playgroud) 我有一个这样的数组:
const rawdata = [
{
top_feature_col: "s9",
top_feature_row: 1,
data: [{ cluster: 0, value: 151508, opportunity_perc: 69 }]
},
{
top_feature_col: "s9",
top_feature_row: 2,
data: [{ cluster: 0, value: 127518, opportunity_perc: 70 }]
},
{
top_feature_col: "s9",
top_feature_row: 2,
data: [{ cluster: 1, value: 12668, opportunity_perc: 40 }]
}
];Run Code Online (Sandbox Code Playgroud)
我想过滤哪里,opportunity_perc >= 50但我不知道该怎么做。
结果应该是:
const result = [
{
top_feature_col: "s9",
top_feature_row: 1,
data: [{ cluster: 0, value: 151508, opportunity_perc: 69 }]
},
{
top_feature_col: "s9",
top_feature_row: …Run Code Online (Sandbox Code Playgroud)我有一个数据数组,我需要将这些数据过滤到 React js 中的变量中。
这是我的数组
[{idappcontent: "Com_01", idcontenttype: "0", idclient: "5"},
{idappcontent: "Com_02", idcontenttype: "0", idclient: "5"},
{idappcontent: "Com_03", idcontenttype: "0", idclient: "5"},
{idappcontent: "Review1_Ques_01", idcontenttype: "0", idclient: "5"},
{idappcontent: "Review1_Ques_02", idcontenttype: "0", idclient: "5"},
{idappcontent: "Sign_01", idcontenttype: "0", idclient: "5"},
{idappcontent: "Sign_02", idcontenttype: "0", idclient: "5"},
{idappcontent: "Thank_01", idcontenttype: "0", idclient: "5"},
{idappcontent: "Thank_02", idcontenttype: "0", idclient: "5"}
]
Run Code Online (Sandbox Code Playgroud)
我需要获取包含“idappcontent ==”Sign_“”的数据。如何过滤与“Sign_”字符串匹配的数组数据?
是否有内置的Javascript函数或库可以执行以下操作:
const data = [
{ name: 'name1', type: 'type1' },
{ name: 'name2', type: 'type2' },
{ name: 'name3', type: 'type3' },
{ name: 'name4', type: 'type2' },
];
Run Code Online (Sandbox Code Playgroud)
要搜索以下内容并返回所有对象,其中 type = 'type2'
类似data.findIndex((i) => i.type === 'type2')但返回所有匹配项而不是第一个索引?
谢谢
export class ResultComponent {
students: AdmissionFormData[]
constructor(private adStudent: AdmissionFormService) {
adStudent.adFormGet().subscribe(
x => this.students = x
)
}
onSubmit(value) {
}
}
Run Code Online (Sandbox Code Playgroud)
在学生数组中,我有数据。而onSubmit是函数,value是参数“roll”
HTML 文件:
<div class="form">
<div class="col-md-5 offset-md-3">
<div class="card">
<div class="card-header text-center">
<h3 id="form_name" >Search Result</h3>
</div>
<div class="card-block">
<form (ngSubmit)="onSubmit(f.value)" #f="ngForm" >
<div class="form-group">
<label for="">Roll</label>
<input
type="number"
ngModel
name="reg"
#reg="ngModel"
[min]="99999"
placeholder="Ex: 224697"
class="form-control">
<p class="text-danger" *ngIf="!reg.valid && reg.touched">Roll Should have at least 6 letter</p>
</div>
<input
type="submit"
value="Search"
class="btn btn-block btn-outline-success">
</form>
</div>
</div> …Run Code Online (Sandbox Code Playgroud) 我有一个对象数组,例如:
{
"value": [
{
"attachment_text": "Support: hello there! I need some help syncing records",
"ticket_text": "Ticket 1537 from ronburgundy@news.com"
},
{
"attachment_text": "Copied from original request - 401 unathorized",
"ticket_text": "Ticket 1459 from klaus.kinsky@kinsky.com"
},
{
"attachment_text": "Contact request form received",
"ticket_text": "Ticket 1173 from sophia@copolla.com"
},
{
"attachment_text": "Hello, we need to troubleshoot some problems",
"ticket_text": "Ticket 1591 from pat@pending.com"
},
{
"attachment_text": "Contact request form received",
"ticket_text": "Ticket 1483 from bugs@bunny.com"
},
{
"attachment_text": "Contact request form …Run Code Online (Sandbox Code Playgroud) 我的基础数据如下......
[{
month: 'Jan',
cat: A,
val: 20
},{
month: 'Jan',
cat: B,
val: 5
},{
month: 'Jan',
cat: C,
val: 10
},{
month: 'Feb',
cat: A,
val: 30
},{
month: 'Feb',
cat: B,
val: 10
},{
month: 'Feb',
cat: C,
val: 20
}]
Run Code Online (Sandbox Code Playgroud)
我需要使用javascript将其转换为以下内容..
[{
month: 'Jan',
A: 20,
B: 5,
C: 10
}, {
month: 'Feb',
A: 30,
B: 10,
C: 20
}]
Run Code Online (Sandbox Code Playgroud)
如果基础数据只有cat和val ..
我本来可以试过像..
data[e.cat] = e.val;
Run Code Online (Sandbox Code Playgroud)
但按月分组给了我..任何帮助都是真诚的感谢..
javascript ×9
arrays ×8
angular ×2
ecmascript-6 ×2
object ×2
angularjs ×1
filter ×1
loops ×1
reactjs ×1
regex ×1
selection ×1
sorting ×1
typescript ×1