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

时间:2019-04-02 11:53:39

标签: javascript arrays

我在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
          ],
          "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
          ]
        };

  

“十个蓝色链接”定义了过去十五年的网络搜索结果-文本片段以及文档标题和URL。在本文中,我们建立了增强搜索结果的概念,将Web搜索结果扩展到包括图像和视频等多媒体对象,特定于意图的键值对以及允许用户直接与网页内容进行交互的元素从搜索结果页面。我们显示,用户明确地以及在其搜索行为中观察到时都表达了对增强结果的偏好。我们还展示了增强结果在帮助用户评估搜索结果的相关性方面的有效性。最后,我们证明了我们可以有效地生成增强的结果,以覆盖搜索结果页面的很大一部分。

3 个答案:

答案 0 :(得分:2)

使用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);

答案 1 :(得分:2)

您可以遍历该对象,然后

  • 获取密钥名称(word
  • 使用index中提供的职位(userdata[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);

答案 2 :(得分:0)

在您的情况下,您的数据就是属性名称本身,因此,最简单的方法是通过以下方式获取该名称:

var foo = Object.getOwnPropertyNames(userdata);

该函数返回数组中对象的所有属性名称,因此您只需将其加入:

foo.join(" ");

希望它适合您。


编辑

在这种情况下,上面的代码不是功能解决方案,没有考虑单词的索引。因此,提出解决方案的正确方法应该类似于Cid的答案(https://stackoverflow.com/a/55474582/9925983):

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]};

let words = [];

Object.getOwnPropertyNames(userdata).forEach(propertyName =>
  userdata[propertyName].forEach(value => words[value] = propertyName)
);

const text = words.join(' ');
console.log(text);

我认为他的答案仍然更具可读性和效率。