为了账号安全,请及时绑定邮箱和手机立即绑定

如何将单词从对象打印到javascript中的指定位置

如何将单词从对象打印到javascript中的指定位置

白衣非少年 2021-04-09 18:15:47
我在javascript中有对象,对象的键是我的段落中的单词,如何在段落中打印它.Value(key:value)保持其位置。尝试使用for循环获取对我不起作用var userdata= {          "\"Ten": [            0          ],          "blue": [            1          ],          "links\"": [            2          ],          "have": [            3          ],          "defined": [            4          ],          "web": [            5,            36,            65          ],          "search": [            6,            32,            37,            70,            90,            108,            126          ],          "results": [            7,            33,            38,            71,            82,            99,            119          ],          "for": [            8,            80          ],          "the": [            9,            28,            56,            61,            69,            95,            105          ],          "last": [            10          ],          "fifteen": [            11          ],          "years": [            12          ],          "--": [            13          ],          "snippets": [            14          ],          "of": [            15,            30,            63,            97,            107,            125          ],        };“十个蓝色链接”定义了过去十五年的网络搜索结果-文本片段以及文档标题和URL。在本文中,我们建立了增强搜索结果的概念,将Web搜索结果扩展到包括图像和视频之类的多媒体对象,特定于意图的键值对以及允许用户直接与网页内容进行交互的元素从搜索结果页面。我们显示,用户明确地以及在其搜索行为中观察到时都表达了对增强结果的偏爱。我们还展示了增强结果在帮助用户评估搜索结果的相关性方面的有效性。最后,我们证明了我们可以有效地生成增强的结果,以覆盖搜索结果页面的很大一部分。
查看完整描述

3 回答

?
眼眸繁星

TA贡献1873条经验 获得超9个赞

使用将对象转换为单词/索引对Object.entries()。使用迭代条目Array.reduce()。在reduce中,用迭代索引Array.forEach(),并将每个单词分配给累加器(r)中的索引。用空格将单词数组连接起来。


const userdata = {"\"Ten":[0],"blue":[1],"links\"":[2],"have":[3],"defined":[4],"web":[5,36,65],"search":[6,32,37,70,90,108,126],"results":[7,33,38,71,82,99,119],"for":[8,80],"the":[9,28,56,61,69,95,105],"last":[10],"fifteen":[11],"years":[12],"--":[13],"snippets":[14],"of":[15,30,63,97,107,125],"text":[16],"combined":[17],"with":[18,60],"document":[19],"titles":[20],"and":[21,46,52,85],"URLs.":[22],"In":[23],"this":[24],"paper,":[25],"we":[26,111,114],"establish":[27],"notion":[29],"enhanced":[31,81,98,118],"that":[34,54,75,113],"extend":[35],"to":[39,58,103,120],"include":[40],"multimedia":[41],"objects":[42],"such":[43],"as":[44],"images":[45],"video,":[47],"intent-specific":[48],"key":[49],"value":[50],"pairs,":[51],"elements":[53],"allow":[55],"user":[57],"interact":[59],"contents":[62],"a":[64,78,122],"page":[66],"directly":[67],"from":[68],"page.":[72],"We":[73,92],"show":[74,112],"users":[76,102],"express":[77],"preference":[79],"both":[83],"explicitly,":[84],"when":[86],"observed":[87],"in":[88,100],"their":[89],"behavior.":[91],"also":[93],"demonstrate":[94],"effectiveness":[96],"helping":[101],"assess":[104],"relevance":[106],"results.":[109],"Lastly,":[110],"can":[115],"efficiently":[116],"generate":[117],"cover":[121],"significant":[123],"fraction":[124],"result":[127],"pages.":[128]};


const result = Object.entries(userdata)

  .reduce((r, [word, indexes]) => {

    indexes.forEach(index => r[index] = word);

    

    return r;

  }, [])

  .join(' ');

  

console.log(result);


查看完整回答
反对 回复 2021-04-15
?
繁花不似锦

TA贡献1851条经验 获得超4个赞

您可以遍历该对象,然后

  • 取得金钥名称(word

  • 使用以下位置提供的位置(indexuserdata[word]

  • 在结果数组中定义要使用的索引和单词,例如arrResult[index] = word

然后,使用' '定界符将该数组连接到字符串中

例如:

var userdata = {"\"Ten":[0],"blue":[1],"links\"":[2],"have":[3],"defined":[4],"web":[5,36,65],"search":[6,32,37,70,90,108,126],"results":[7,33,38,71,82,99,119],"for":[8,80],"the":[9,28,56,61,69,95,105],"last":[10],"fifteen":[11],"years":[12],"--":[13],"snippets":[14],"of":[15,30,63,97,107,125],"text":[16],"combined":[17],"with":[18,60],"document":[19],"titles":[20],"and":[21,46,52,85],"URLs.":[22],"In":[23],"this":[24],"paper,":[25],"we":[26,111,114],"establish":[27],"notion":[29],"enhanced":[31,81,98,118],"that":[34,54,75,113],"extend":[35],"to":[39,58,103,120],"include":[40],"multimedia":[41],"objects":[42],"such":[43],"as":[44],"images":[45],"video,":[47],"intent-specific":[48],"key":[49],"value":[50],"pairs,":[51],"elements":[53],"allow":[55],"user":[57],"interact":[59],"contents":[62],"a":[64,78,122],"page":[66],"directly":[67],"from":[68],"page.":[72],"We":[73,92],"show":[74,112],"users":[76,102],"express":[77],"preference":[79],"both":[83],"explicitly,":[84],"when":[86],"observed":[87],"in":[88,100],"their":[89],"behavior.":[91],"also":[93],"demonstrate":[94],"effectiveness":[96],"helping":[101],"assess":[104],"relevance":[106],"results.":[109],"Lastly,":[110],"can":[115],"efficiently":[116],"generate":[117],"cover":[121],"significant":[123],"fraction":[124],"result":[127],"pages.":[128]};


let arrResult = [];


for (let word in userdata)

{

  userdata[word].forEach((i) =>

  {

    arrResult[i] = word;

  });

}

let result = arrResult.join(' ');

console.log(result);


查看完整回答
反对 回复 2021-04-15
  • 3 回答
  • 0 关注
  • 199 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信