我有json数组来自http web api,我想过滤这个数组:
[
{
"bulan": "4",
"tahun": "2017",
"id_program": 4,
"id_segmen": 1,
"segmen": "ppu",
"jumlah": 3953,
"target": 900000
},
{
"bulan": "4",
"tahun": "2017",
"id_program": 3,
"id_segmen": 2,
"segmen": "bpu",
"jumlah": 45,
"target": 500000
},
{
"bulan": "4",
"tahun": "2017",
"id_program": 3,
"id_segmen": 1,
"segmen": "ppu",
"jumlah": 8752,
"target": 900000
}]
我需要过滤或选择get all id_segmen = 1。 这是我的期望:
[
{
"bulan": "4",
"tahun": "2017",
"id_program": 4,
"id_segmen": 1,
"segmen": "ppu",
"jumlah": 3953,
"target": 900000
}]
我目前的http.get如下:
this.http.get<JuResponse>(environment.url + '/cps/' + year + '/' + month + '/' + kanwil + '/' + cabang).
subscribe(data => {
let filteredValues = data.filter((data) => value.id_segmen == 1);
console.log(filteredValues);
});
答案 0 :(得分:1)
检查以下代码,items是来自WebAPI的JSON,fiteredItem是一个具有id_segmen = 1的对象数组
this.filteredItem= this.items.filter((item) => {
return (item.id_segmen ===1);
})
答案 1 :(得分:0)
您可以使用Array.filter
let filteredArray = this.myArray.filter(item => item.id_segment===1)
答案 2 :(得分:0)
只需使用原生javascript filter()方法,就像这样:
let filteredValues = result.filter((value) => value.id_segmen == 1);
这样,
filteredValues 将仅包含id_segmen == 1的值。
答案 3 :(得分:0)
您可以使用
for (var i = 0; i < arrayData.length; i++) {
if (arrayData[i].id_segmen == 1) {
result.push(arrayData[i]);
}
}
试试这个
var arrayData= [
{
"bulan": "4",
"tahun": "2017",
"id_program": 4,
"id_segmen": 1,
"segmen": "ppu",
"jumlah": 3953,
"target": 900000
},
{
"bulan": "4",
"tahun": "2017",
"id_program": 3,
"id_segmen": 2,
"segmen": "bpu",
"jumlah": 45,
"target": 500000
},
{
"bulan": "4",
"tahun": "2017",
"id_program": 3,
"id_segmen": 1,
"segmen": "ppu",
"jumlah": 8752,
"target": 900000
}]
var result = [];
for (var i = 0; i < arrayData.length; i++) {
if (arrayData[i].id_segmen == 1) {
result.push(arrayData[i]);
}
}
console.log(result)
答案 4 :(得分:0)
您可以将Javascript过滤器用于此目的
var a = [
{
"bulan": "4",
"tahun": "2017",
"id_program": 4,
"id_segmen": 1,
"segmen": "ppu",
"jumlah": 3953,
"target": 900000
},
{
"bulan": "4",
"tahun": "2017",
"id_program": 3,
"id_segmen": 2,
"segmen": "bpu",
"jumlah": 45,
"target": 500000
},
{
"bulan": "4",
"tahun": "2017",
"id_program": 3,
"id_segmen": 1,
"segmen": "ppu",
"jumlah": 8752,
"target": 900000
}];
var c = a.filter(function(val){
return val.id_segmen == 1 ;
});
alert(JSON.stringify(c))