使用相同的“规则”检查多个变量(Checking multiple variables with the same “rules”)
我必须构建一个对象数组。 我可以“长手”,但我希望找到一种方法来遍历一些变量,并检查每个变量,将它们“推”到数组中的正确位置。
我有这个:
//this is the starting array...I'm going to update these objects operationTime = [ {"isActive":false,"timeFrom":null,"timeTill":null},//Monday which is operationTime[0] {"isActive":false,"timeFrom":null,"timeTill":null}, {"isActive":false,"timeFrom":null,"timeTill":null}, {"isActive":false,"timeFrom":null,"timeTill":null}, {"isActive":false,"timeFrom":null,"timeTill":null}, {"isActive":false,"timeFrom":null,"timeTill":null}, {"isActive":false,"timeFrom":null,"timeTill":null} ]; //I get the below via an API call var monHours = placeHours.mon_open_close; var tueHours = placeHours.tue_open_close; var wedHours = placeHours.wed_open_close; var thuHours = placeHours.thu_open_close; var friHours = placeHours.fri_open_close; var satHours = placeHours.sat_open_close; var sunHours = placeHours.sun_open_close; var sunHours = placeHours.sun_open_close; //here's where I'm stuck. if (monHours.length>0){ var arr = monHours[0].split("-"); operationTime[0].isActive= true; operationTime[0].timeFrom= arr[0]; operationTime[0].timeTill= arr[1]; } else { operationTime[0].isActive= false; }
我的
if/else
在上面使用星期一的例子中完美地工作,但我不想在一周中的七天内写这个让它不必要的复杂。 我怎么能把这个压缩成一个单独的“函数”来测试每个变量并将它推入数组对象的正确位置?I have to construct an array of objects. I can do it "long hand," but I'm hoping to find a way to iterate through some variables and check each at "push" them into the right spot in the array.
I have this:
//this is the starting array...I'm going to update these objects operationTime = [ {"isActive":false,"timeFrom":null,"timeTill":null},//Monday which is operationTime[0] {"isActive":false,"timeFrom":null,"timeTill":null}, {"isActive":false,"timeFrom":null,"timeTill":null}, {"isActive":false,"timeFrom":null,"timeTill":null}, {"isActive":false,"timeFrom":null,"timeTill":null}, {"isActive":false,"timeFrom":null,"timeTill":null}, {"isActive":false,"timeFrom":null,"timeTill":null} ]; //I get the below via an API call var monHours = placeHours.mon_open_close; var tueHours = placeHours.tue_open_close; var wedHours = placeHours.wed_open_close; var thuHours = placeHours.thu_open_close; var friHours = placeHours.fri_open_close; var satHours = placeHours.sat_open_close; var sunHours = placeHours.sun_open_close; var sunHours = placeHours.sun_open_close; //here's where I'm stuck. if (monHours.length>0){ var arr = monHours[0].split("-"); operationTime[0].isActive= true; operationTime[0].timeFrom= arr[0]; operationTime[0].timeTill= arr[1]; } else { operationTime[0].isActive= false; }
My
if/else
works perfectly in the above example using Monday, but I don't want to write this for seven days of the week making it unnecessarily complicated. How could I condense this into a single "function" that'll test each variable and push it into the array object in the correct position?
原文:https://stackoverflow.com/questions/45449805
最满意答案
使用
data.table
连接:replacement = data.table(a = char, new_a = num, key = "a") dt.fixed = replacement[dt][, a := ifelse(is.na(new_a), a, new_a)][, new_a := NULL]
Use
data.table
joins:replacement = data.table(a = char, new_a = num, key = "a") dt.fixed = replacement[dt][, a := ifelse(is.na(new_a), a, new_a)][, new_a := NULL]
相关问答
更多-
快速识别符合data.table标准的观测值的方法(Quick way to identify observations that meets certain criteria in data.table)[2022-03-30]
它可能......而且很有趣......但不是那么美丽,也不可读: CJ(A1=A$age,A2=A$age)[,.(A1, A2,d=abs(A1-A2)) ][d>=0.5 & d<=1 ][, .(d, dmin=min(d),A2), by="A1" ][d==dmin ][A, .(A1, A2,V2 ... -
data.table避免回收(data.table avoid recycling)[2023-06-25]
可以使用超出范围的索引: library("data.table") x <- c(1,2,3,4) y <- c(8,9) n <- max(length(x), length(y)) dt <- data.table(x = x[1:n], y = y[1:n]) # > dt # x y # 1: 1 8 # 2: 2 9 # 3: 3 NA # 4: 4 NA 或者您可以通过执行来扩展y (如评论中推荐的@Roland): length(y) <- length(x) <- max ... -
使用data.table连接: replacement = data.table(a = char, new_a = num, key = "a") dt.fixed = replacement[dt][, a := ifelse(is.na(new_a), a, new_a)][, new_a := NULL] Use data.table joins: replacement = data.table(a = char, new_a = num, key = "a") dt.fixed = rep ...
-
尝试这个: DT[,letterA:=Reduce("|",lapply(.SD,grepl,pattern= "^A" )), .SDcols=c("V1","V2")] # V1 V2 V3 V4 letterA #1: A D G A TRUE #2: B E H A FALSE #3: C F I A FALSE Try this: DT[,letterA:=Reduce("|",lapply(.SD,grepl,pattern= ...
-
data.table:使用矩阵更新data.table中的多个列(data.table: Update multiple columns in a data.table with a matrix)[2023-08-20]
您需要将RHS转换为list ,并且简单的方法是使用as.data.table : DT[, 2:ncol(DT) := as.data.table(mat[,2:ncol(DT)])] 在这里没有必要,因为LHS被推断为自动表示列号。 You need to convert the RHS to a list, and an easy way to do that is to use as.data.table: DT[, 2:ncol(DT) := as.data.table(mat[,2:ncol( ... -
这个怎么样? 使用data.table的滚动连接: DT = data.table(pre = pre.numbers, current.number = pre.numbers+0.5, key="current.number") setkey(the.table, current.number) ans = DT[the.table, roll=Inf, rollends=FALSE] 既然你在处理整数,我刚刚加了0.5(0到1之间的任何数字都应该没问题),以便从pre.numbers ...
-
你可以使用rle() : dat[, index2:=rep(seq(length(unique(year))), rle(year)$lengths), by=id] 我希望有所帮助。 You can use rle(): dat[, index2:=rep(seq(length(unique(year))), rle(year)$lengths), by=id] I hope that helps.
-
data.table:通过观察在过去24小时内,每个组的子集(data.table: subset by observations in last 24 hours, per group)[2022-01-06]
简单的解决方案为每个组创建一个截止时间(假设时间已经转换): dt[, cutoff_time := max(time) - 24*60*60, by = group] dt[time > cutoff_time] 编辑: “GForce优化最大值”的评论让我很好奇,所以为了比较速度,我创建了一些更大的假数据。 请注意, integer在max和>=都很好地起作用: require(data.table) require(microbenchmark) N = 100000 N_g = 100 al ... -
在R中,如何返回观察组,而不是观察数量?(In R, how to return the observation groups, not the number of observations?)[2022-11-14]
几个选项包括: R> unique(x) [1] 1 2 3 4 5 6 7 8 9 10 11 12 Levels: 1 2 3 4 5 6 7 8 9 10 11 12 R> levels(x) [1] "1" "2" "3" "4" "5" "6" "7" "8" "9" "10" "11" "12" R> as.numeric(levels(x)) [1] 1 2 3 4 5 6 7 8 9 10 11 12 如果您想要分组,则level ... -
这是一种将mapply()与setdiff()和union()结合使用的方法。 在将id收集到列表中之后,首先我们排除当前的id,然后将union()从两边列出: setDT(indata)[,msib:=.(list(id)), by = "mid"][ ,msibs := mapply(setdiff, msib, id)][ ,fsib := .(list(id)), by = "fid"][ ,fsibs := mapply(setdiff, fsib, id)][ ,sibs ...