我需要浏览每个文本(父母和孩子)
示例js代码:
data: [
{ text: "Item 1" },
{ text: "Item 2", items: [
{ text: "SubItem 2.1" },
{ text: "SubItem 2.2" }
] },
{ text: "Item 3" }
]
angular.forEach(data, function (value, key)
{
console.log(value.text);
});`
此代码仅返回父文本。
答案 0 :(得分:1)
它没有用,因为您没有循环使用子项。请参阅以下代码:
angular.module('app', [])
.controller('ctrl', function() {
var printItems = function(items) {
angular.forEach(items, function(value, key) {
console.log(value.text);
if (value.items) {
printItems(value.items);
}
});
};
var data = [{
text: "Item 1"
}, {
text: "Item 2",
items: [{
text: "SubItem 2.1"
}, {
text: "SubItem 2.2"
}]
}, {
text: "Item 3"
}];
printItems(data);
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="app" ng-controller="ctrl"></div>
它将逻辑提取到一个单独的函数,并递归调用它来遍历子项。
答案 1 :(得分:1)
您应该使用 baseurl <- "http://zoeken.kvk.nl/Address.ashx?site=handelsregister&partialfields=&q=010"
pages3 <- list()
for(i in 1:99999){
if(i < 10){
mydata <- RJSONIO::fromJSON(paste0(baseurl,"00000",i), flatten=TRUE)
}
if(i < 100 & i >= 10){
mydata <- RJSONIO::fromJSON(paste0(baseurl,"0000",i), flatten=TRUE)
}
if(i < 1000 & i >= 100){
mydata <- RJSONIO::fromJSON(paste0(baseurl,"000",i), flatten=TRUE)
}
if(i < 10000 & i >= 1000){
mydata <- RJSONIO::fromJSON(paste0(baseurl,"00",i), flatten=TRUE)
}
if(i < 100000 & i >= 10000){
mydata <- RJSONIO::fromJSON(paste0(baseurl,"0",i), flatten=TRUE)
}
if(i < 1000000 & i >= 100000){
mydata <- RJSONIO::fromJSON(paste0(baseurl,i), flatten=TRUE)
}
mydata <- RJSONIO::fromJSON(paste0(baseurl,i), flatten=TRUE)
pages3[[i]] <- mydata$resultatenHR
options(timeout = 4000000)
if(i %% 100 == 0){Sys.sleep(5)}
if(i %% 1000 == 0){
final_df<-do.call(rbind,pages3)
final<- Reduce(function(x,y) merge(x, y, all=TRUE), final_df)
mytime <- format(Sys.time(), "%b_%d_%H_%M_%S_%Y")
myfile <- file.path(R(), paste0(mytime, "_", i, ".csv"))
write.csv2(final, file = myfile, sep = "", row.names = FALSE, col.names = FALSE,
quote = FALSE, append = FALSE)
}
}
}
}
检查每个angular.isDefined
对象中存在的items
属性,然后遍历该属性以获取data
数组中的text
值:
items
&#13;
var myApp = angular.module('myApp', []);
function MyCtrl($scope) {
var data= [
{ text: "Item 1" },
{ text: "Item 2", items: [
{ text: "SubItem 2.1" },
{ text: "SubItem 2.2" }
] },
{ text: "Item 3" }
];
angular.forEach(data, function (value, key)
{
console.log(value.text);
if(angular.isDefined(value.items)){
var itemsArray = value.items;
angular.forEach(itemsArray, function (item, key){
console.log(item.text);
});
}
});
}
&#13;