这是我试图循环的json:


 var json = {“series”:[
 {
 “ID”:1448822
 “名”: “白金汉”,
 “选择”:真,
 “数据”:[
 [
 19.23
 19.23
 ]
 ]
 },
 {
 “ID”:1448823
 “名”: “剑桥”,
 “选择”:假,
 “数据”:[
 [
 38.46
 61.54
 ]
 ]
 }
 }

&#xA;&#xA; 我一直在努力研究如何使用以下代码中的值为true选择名称来获取数组位置:< / p>&#xA;&#xA;
for(var i = 0; i&lt; json.length; i ++){&#xA; var geoAreaSelected = serData [i] .selected;&#xA; if(geoAreaSelected === true){&#xA; this.series [I]。数据[I] .setState( '选择');&#XA; }&#XA; };&#XA; 代码>
&#XA;
答案 0 :(得分:1)
您必须迭代json.series
而不是json
for (var i = 0; i < json.series.length; i++) {
var geoAreaSelected = json.series[i].selected;
if (geoAreaSelected === true) {
json.series[i].data[i].setState('select');
console.log(i); // <--- you can use the variable i to access ith object in the array
}
};
答案 1 :(得分:0)
修改强>
[ 19.23, 19.23 ]
控制台日志@interface ModalViewController (){
NSMutableArray *arrayToReturn;
}
@property (strong, nonatomic) IBOutlet UITextField *bookTextField;
@property (strong, nonatomic) IBOutlet UITextField *pageTextField;
@property (strong, nonatomic) IBOutlet customAddBookButton *doneButton;
@end
@implementation ModalViewController
#pragma mark- textField Delegate
-(BOOL)textFieldShouldReturn:(UITextField *)textField{
NSLog(@"textFieldShouldReturn");
if (textField == self.bookTextField) {
[self.pageTextField becomeFirstResponder];
}else if (textField == self.pageTextField){
if ([self.bookTextField.text isEqual:@""]) {
[self.bookTextField becomeFirstResponder];
}else {
[self.pageTextField resignFirstResponder];
}
}
[arrayToReturn insertObject:self.bookTextField.text atIndex:0];
[arrayToReturn insertObject:self.pageTextField.text atIndex:1];
return YES;
}
#pragma mark- Done button action
- (IBAction)Done:(id)sender {
self.bookMetaReturnedfromModalView (arrayToReturn);
NSLog(@"array is %@", arrayToReturn);
[self dismissViewControllerAnimated:YES completion:nil];
}
#pragma mark- viewDidLoad
- (void)viewDidLoad {
[super viewDidLoad];
[self.bookTextField becomeFirstResponder];
arrayToReturn = [[NSMutableArray alloc]init];
}
@end