[英]Passing key-values to another array with different length
嗨,我有兩個數組,里面有對象。 我正在嘗試將title屬性的哈希值傳遞給也具有標題鍵-值對但不具有哈希值(這是一個空字符串)的另一個數組的對象。
訣竅是數組未排序,並且它們具有不同的長度。
這是我的代碼:
var res = [ [{ title: "time goes by", searchhash: "" }, { title: "gta vice city", searchhash: "" }, { title: "miami beach", searchhash: "" } ], [{ title: "miami beach", search_hash: "12456" }, { title: "time goes by", search_hash: "98765" } ] ] for (var i = 0; i < res[0].length; i++) { if (res[0][i].searchhash === "") { titlePass(res[0][i].title) res[0][i].searchhash = function hashPass(p) { console.log(p); i += 1 } } } function titlePass(t) { //Here I'm getting the titles (t) res[1].forEach((obj) => { if (t === obj.title) { hashPass(obj["search_hash"]) } }) }
但是我得到的不是hashPass函數。 我知道我可以訪問forPass循環中的hashPass函數的那些值,但是我想讓這些值在for循環內傳遞,以將其分配給searchhash屬性...
你是這個意思嗎
var res = [ [{ title: "time goes by", searchhash: "" }, { title: "gta vice city", searchhash: "" }, { title: "miami beach", searchhash: "" } ], [{ title: "miami beach", search_hash: "12456" }, { title: "time goes by", search_hash: "98765" } ] ] for (var i = 0; i < res[0].length; i++) { var res0 = res[0][i]; if (res0.searchhash === "") { var title = res0.title; for (var j=0;j<res[1].length;j++) { var res1 = res[1][j]; if (res1.title == title) res0.searchhash=res1.search_hash; } } } console.log(res)
嗨,嘗試下面的代碼。
var res = [
[{
title: "time goes by",
searchhash: ""
},
{
title: "gta vice city",
searchhash: ""
},
{
title: "miami beach",
searchhash: ""
}
],
[{
title: "miami beach",
search_hash: "12456"
},
{
title: "time goes by",
search_hash: "98765"
}
]
]
for (var i = 0; i < res[0].length; i++) {
if (res[0][i].searchhash === "") {
titlePass(res[0][i]);
}
}
function titlePass(o) {
res[1].forEach((obj) => {
if (o.title === obj.title) {
o["searchhash"] = obj["search_hash"];
}
})
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.