简体   繁体   English

从有组织的单词列表构建JSON? 的NodeJS

[英]Building JSON from a list of organised words? NodeJS

Excuse the terrible title, I have this as an example: The numbers represent how similar the word is compared to the search term. 不好意思的标题,我举个例子:数字代表单词与搜索词的相似度。

0.3333333333333333: Jerome
0: Murphy
0: Louis
0.14285714285714285: Chester
0.2: Judah
0.125: Fletcher
0.14285714285714285: Gregory
0.16666666666666666: Daniel
0: Orson
0.16666666666666666: Rafael
0: Nasim
0: Tarik
0.16666666666666666: Daniel
0.25: Dale
0: Mannix
0: Dolan
0.14285714285714285: Bernard
0: Ethan
0: Brody
0.2: Arden

I tried to do this, Stage is from the 我试图做到这一点,舞台是从

 for (Stage in results) {    

 var organisedsearch = 
      {
         "Name"  : results[Stage].Name,
         "Number" : Similarity
      }
  } 

results is from a mysql query, Name is from the database. 结果来自mysql查询,名称来自数据库。

but that gives this, Shortened example. 但这就是这个例子。

{"Number":0.3333333333333333}
{"Number":0.16666666666666666}

it also only returns the first line in the list, when I try to print it out of the loop, how can I put it into JSON? 它也仅返回列表中的第一行,当我尝试将其打印出循环时,如何将其放入JSON中?

What I would like to achieve: 我想实现的目标:

 {

  "Name":"Jerome",
  "RelevanceToSearch": 0.3"

 }

Repeated for each word/number in the list. 对列表中的每个单词/数字重复此操作。

You could map the data, if you have them in an array with objects and a name property. 如果您将数据放在带有对象和名称属性的数组中,则可以映射数据。

 function similarity(name) { return Math.random(); // a dummy value } var data = [{ name: 'Jane' }, { name: 'Mary' }, { name: 'Lou' }, { name: 'Betty' }]; result = data.map(function (object) { return { name: object.name, relevance: similarity(object.name) }; }); console.log(result); 
 .as-console-wrapper { max-height: 100% !important; top: 0; } 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM