我有两个数组。我想在 originalArr 中找到 currentArr 位置的索引。 这两个数组都在运行时更新。
let currentArr= [[450, 30, "down"],[480, 60, "right"]]
let originalArr = [[510, 60, "right"],[480, 60, "right"],[450, 60, "down"],[450, 30, "down"], [450, 0, "right"]]
有人可以帮我吗?
答案 0 :(得分:3)
您可以使用函数p1 <- '<img src="http://www.dailyexcelsior.com/wp-content/uploads/2019/01/house.jpg" width=150 height=100>'
p2 <- '<img src="http://www.pd.co.th/uploads/content/2017/10/o_1brg6i1m25is1hnng571876544a.jpg" width=150 height=100>'
p3 <- '<img src="https://www.harronhomes.com/wp-content/uploads/2015/02/Birkwith-330x192-24-June-EDIT1.jpg" width=150 height=100>'
p4 <- '<img src="https://m.persimmonhomes.com/images/the-rockcliff_133414.jpg" width=150 height=100>'
p5 <- '<img src="https://www.ryanhomes.com/rh-community-gallery-NewAspectRatio/d8b0c394-b123-4d9d-957b-1d24e21d319f/db/d8b0c394-b123-4d9d-957b-1d24e21d319f.jpg" width=150 height=100>'
p6 <- '<img src="https://www.iconichouses.org/foto/houses/duldeck.jpg" width=150 height=100>'
dtable1 <- datatable(
t(data.frame(
"Pic" = p1,
Size = 1500,
Age = 5,
Bathrooms = 2,
row.names = "p1")),
escape = FALSE,
height = 300,
options = list(
dom = 't',
pageLength = 20)
)
dtable2 <- datatable(
t(data.frame(
Pic = c(p2,p3,p4,p5,p6),
Size = c(1500,1200,1400,1600,1300),
Age = c(5,15,10,7,12),
Bathrooms = c(1.5,2,2,1.5,2),
row.names = c("p2","p3","p4","p5","p6"))),
escape = FALSE,
options = list(
dom = 't',
pageLength = 20)
)
# save tables as html
saveWidget(dtable1, "dtable1.html")
saveWidget(dtable2, "dtable2.html", selfcontained = FALSE)
# read the html files in R lists
library(xml2)
list1 <- as_list(read_html("dtable1.html"))
list2 <- as_list(read_html("dtable2.html"))
div2 <- list2$html$body$div[1:3]
scripts2 <- list2$html$body[4:5]
# # change default style "width:960px;height:500px;"
# # that doesn't work, actually the dimensions are set by JavaScript
# # => use the width/height options of datatable()
# attr(list1$html$body$div$div, "style") <- attr(div2$div, "style") <- "width: 100%;"
# "stack" the two datatables
list12 <- list1
list12$html$body$div <- c(list12$html$body$div, div2)
list12$html$body <- c(list12$html$body, scripts2)
# set the attributes because they are lost
attr(list12$html$body$div, "id") <- "htmlwidget_container"
attr(list12$html$body, "style") <- attr(list1$html$body, "style")
# create new html file
html12 <- as_xml_document(list12)
write_html(html12, "dtables.html", options = "as_html")
# snapshot
webshot("dtables.html", "dtablesSnapshot.pdf")
和函数map
查找匹配项。
此替代方法使用函数findIndex
检查长度和每个索引值。
我假设索引应该在同一位置
every
let currentArr= [[450, 30, "down"],[480, 60, "right"]]
let originalArr = [[510, 60, "right"],[480, 60, "right"],[450, 60, "down"],[450, 30, "down"], [450, 0, "right"]];
let indexes = currentArr.map(a => originalArr.findIndex(ia => ia.length === a.length && ia.every((e, i) => e === a[i])));
console.log(indexes);
答案 1 :(得分:0)
由于内部数组将始终处于相同顺序,因此您可以使用JSON.stringify
比较数组的字符串化版本:
let currentArr= [[450, 30, "down"],[480, 60, "right"]]
let originalArr = [[510, 60, "right"],[480, 60, "right"],[450, 60, "down"],[450, 30, "down"], [450, 0, "right"]];
let indexes = currentArr.map(c =>
originalArr.findIndex(o => JSON.stringify(o) === JSON.stringify(c)));
console.log(indexes);