我有这样的JSON,它存储在变量中。
data=[
[
{
"fdate":"01-01-2018",
"pdate":"31-12-2018",
"range":"01-01-2018 - 31-12-2018",
"nlist":[ ],
"nplist":[ ]
}
],
[
{
"fdate":"01-01-2018",
"pdate":"31-12-2018",
"range":"01-01-2018 - 31-12-2018",
"nlist":[ ],
"nplist":[ ]
},
{
"efdate":"21-01-2018",
"expdate":"31-12-2018",
"range":"21-01-2018 - 31-12-2018",
"nlist":[ ],
"nplist":[ ]
}
]
]
如何从打印机文件中获取范围,请提供建议。
我试图像这样检索,但它无法正常工作
for(let i=0; i< this.data.length; i++){
this.other += this.data[i].range;
}
Console.log(this.other);
如果有更好的方法,请建议或告诉我
答案 0 :(得分:1)
你可以这样做:
for(let i=0; i < this.data.length;i++){
for(let j=0; j < this.data[i].length;j++){
this.other += this.data[i][j].range;
console.log(this.data[i][j].range); // What you want to do here...
}
}
以下是工作示例:https://stackblitz.com/edit/angular-8j5hkk
希望这能帮到你!!!!
答案 1 :(得分:1)
var data=[
[
{
"fdate":"01-01-2018",
"pdate":"31-12-2018",
"range":"01-01-2018 - 31-12-2018",
"nlist":[ ],
"nplist":[ ]
}
],
[
{
"fdate":"01-01-2018",
"pdate":"31-12-2018",
"range":"01-01-2018 - 31-12-2018",
"nlist":[ ],
"nplist":[ ]
},
{
"efdate":"21-01-2018",
"expdate":"31-12-2018",
"range":"21-01-2018 - 31-12-2018",
"nlist":[ ],
"nplist":[ ]
}
]
]
var calc = calculateRange(data);
console.log(calc)
function calculateRange(_data){
var r = "";
for(var i=0;i<_data.length;i++){
var isArray = Array.isArray(_data[i]);
if(isArray){
r+=calculateRange(_data[i]);
}else{
var x = _data[i].range;
r+= typeof x != "undefined"? x:"";
}
}
return r;
}
&#13;
您可以使用逻辑循环,示例在上面。
答案 2 :(得分:0)
this.data=[
[
{
"fdate":"01-01-2018",
"pdate":"31-12-2018",
"range":"01-01-2018 - 31-12-2018",
"nlist":[ ],
"nplist":[ ]
}
],
[
{
"fdate":"01-01-2018",
"pdate":"31-12-2018",
"range":"01-01-2018 - 31-12-2018",
"nlist":[ ],
"nplist":[ ]
},
{
"efdate":"21-01-2018",
"expdate":"31-12-2018",
"range":"21-01-2018 - 31-12-2018",
"nlist":[ ],
"nplist":[ ]
}
]
]
for(let i=0; i< this.data.length; i++){
for(let t = 0; t < this.data[i].length; t++) {
console.log(this.data[i][t].range);
}
}
在html页面中,您可以像这样检索
<div *ngFor="let i of data">
<p *ngFor="let t of i">{{t.range}}</p>
</div>
或在你的ts中,你可以这样做
for(let i=0; i< this.data.length; i++){
for(let t = 0; t < this.data[i].length; t++) {
console.log(this.data[i][t].range);
}}