I have object in javascript and keys of the object are words of my paragraph how to print it in paragraph.Value(key:value) holds its position.
Tried using for loop to fetch didnt work for me
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
]
};
"Ten blue links" have defined web search results for the last fifteen years -- snippets of text combined with document titles and URLs. In this paper, we establish the notion of enhanced search results that extend web search results to include multimedia objects such as images and video, intent-specific key value pairs, and elements that allow the user to interact with the contents of a web page directly from the search results page. We show that users express a preference for enhanced results both explicitly, and when observed in their search behavior. We also demonstrate the effectiveness of enhanced results in helping users to assess the relevance of search results. Lastly, we show that we can efficiently generate enhanced results to cover a significant fraction of search result pages.
Convert the object to word/indexes pair with Object.entries(). Iterate the entries using Array.reduce(). Inside the reduce, iterate the indexes with Array.forEach(), and assign each word to its index in the accumulator (r). Join the the array of words with a space.
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);
You can loop through that object and
get the key name (word)
use the provided positions (index) from userdata[word]
define in a result array the index and word to use, such as arrResult[index] = word.
And then, join that array in a string using ' ' as delimiter
In example :
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);
In your case, your data is the property name itself, so, the easiest way is to get that names with:
var foo = Object.getOwnPropertyNames(userdata);
That function returns you all the property names of the object in an array, so you simply join it:
foo.join(" ");
Hope it suits for you.
EDIT
The above code is not a functional solution in this case, didn't consider the words' index. So the correct way to aproach the solution whould be something like Cid's answer (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);
His answer still being more readable and efficient in my opinion.
Related
Hello I have an obj(came from JSON parsed) and I'm trying to get only one value from it the "VALUE"(default.timelineData)
for example from this obj I want an array [38,35,87,63,34].
I tried with Object.values and also to pass it to an array and then work with it but it's very complicated and I believe there is a shortcut to it. function without success hopes for help thanks...
{
"default":{
"timelineData":[
{
"time":"1610323200",
"formattedTime":"Jan 11, 2021",
"formattedAxisTime":"Jan 11",
"value":[
38
],
"hasData":[
true
],
"formattedValue":[
"38"
]
},
{
"time":"1610409600",
"formattedTime":"Jan 12, 2021",
"formattedAxisTime":"Jan 12",
"value":[
35
],
"hasData":[
true
],
"formattedValue":[
"35"
]
},
{
"time":"1610496000",
"formattedTime":"Jan 13, 2021",
"formattedAxisTime":"Jan 13",
"value":[
87
],
"hasData":[
true
],
"formattedValue":[
"87"
]
},
{
"time":"1610582400",
"formattedTime":"Jan 14, 2021",
"formattedAxisTime":"Jan 14",
"value":[
63
],
"hasData":[
true
],
"formattedValue":[
"63"
]
},
{
"time":"1610668800",
"formattedTime":"Jan 15, 2021",
"formattedAxisTime":"Jan 15",
"value":[
34
],
"hasData":[
true
],
"formattedValue":[
"34"
]
}
],
"averages":[
]
}
}
The property you are trying to access is an Array. You can use Array.map to iterate through the elements and push the returned values into a new array at the same time. Like this:
const values = obj.default.timelineData.map(item => item.value[0])
console.log(values); // [38, 35, 87, 63, 34]
Here is a portion of the response from Youtube Suggestion API for the URL (https://clients1.google.com/complete/search?client=youtube&q=b%20walmart&gl=&jsonp=jsonpCB&_=1600401501454)
[
"b walmart",
[
[
"walmart b complex",
0,
[
22,
30
]
],
[
"b walmart",
0
],
[
"walmart b&m",
0,
[
22,
30
]
]
],
{
"k": 1,
"q": "295XbO_S8qtvvBAI-FdfMCmHaHk"
}
]
Could someone help me understand the significance of the numbers?
Thanks.
I've been struggling to find a convex-hull library for Javascript that gives the same output as the Matlab convhulln function.
I am transcribing some code from Matlab to Javascript and I need to find triangles that make up the convex hull of a set of vertices. Matlab calculates the convex hull of some 3D points using convhulln (which makes use of the qhull function). In this case Matlab convhulln outputs a different set of triangle faces to that of quickhull3d when I pass the same vertices to each. Some faces coincide but in general most do not. The only similarity they have is they both output the same number of faces (although the actual vertices of the faces are not the same).
http://www.mathworks.com/help/matlab/ref/convhulln.html
In Javascript, I've tried different libraries and each one gives a different output..
From npm I've tried:
quickhull3d - https://github.com/maurizzzio/quickhull3d
convex-hull
delaunay-triangulate
most other algorithms have been designed for 2D points, so I have ruled them out.
Any thoughts, tips or feedback would be greatly appreciated! Thank you!
Here are the vertices I'm using and the results of both MATlab's convhulln and quickhull3d. Please note I've used a sorting algorithm to sort them in the from first vertices to last. This should not effect the actual triangle faces since I sorted them in exactly the same way in both the MATlab and JS code.
Here are plots of the two hulls.
Notice the holes. I have spoken to the developer of the quickhull3d algorithm (thanks for your response!!) and he has suggested it might be that the triangulation process he used is different to one matlab uses.
vertices = [
[ 0.9510565162951535, -0.3090169943749474, 0 ],
[ 0.5877852522924731, -0.8090169943749475, 0 ],
[ 6.123233995736766e-17, -1, 0 ],
[ -0.5591929034707466, 0.8290375725550418, 0 ],
[ -0.9510565162951535, -0.3090169943749475, 0 ],
[ -0.9510565162951536, 0.3090169943749473, 0 ],
[ -0.5877852522924732, 0.8090169943749473, 0 ],
[ -1.8369701987210297e-16, 1, 0 ],
[ 0.5877852522924729, 0.8090169943749476, 0 ],
[ 0.9510565162951535, 0.3090169943749476, 0 ],
[ 0.984807753012208, 0, -0.17364817766693033 ],
[ 0.30432233187297814, -0.9366078308002486, -0.17364817766693033 ],
[ -0.796726208379082, -0.5788554735638644, -0.17364817766693033 ],
[ -0.7967262083790821, 0.5788554735638641, -0.17364817766693033 ],
[ 0.3043223318729779, 0.9366078308002487, -0.17364817766693033 ],
[ 0.5000000000000001, -0.5, 0.7071067811865475 ],
[ -0.5, -0.5000000000000001, 0.7071067811865475 ],
[ -0.5000000000000001, 0.5, 0.7071067811865475 ],
[ 0.4999999999999999, 0.5000000000000001, 0.7071067811865475 ],
[ 6.123233995736766e-17, 0, 1 ]
]
triangles from quickhull3d:
dim = 36x3
trianglesqh = [
[ 0, 1, 11 ],
[ 0, 9, 18 ],
[ 0, 10, 9 ],
[ 0, 11, 10 ],
[ 0, 15, 1 ],
[ 0, 18, 15 ],
[ 1, 2, 11 ],
[ 1, 15, 2 ],
[ 2, 12, 11 ],
[ 2, 15, 16 ],
[ 2, 16, 12 ],
[ 3, 6, 17 ],
[ 3, 7, 14 ],
[ 3, 13, 6 ],
[ 3, 14, 13 ],
[ 3, 17, 7 ],
[ 4, 5, 13 ],
[ 4, 12, 16 ],
[ 4, 13, 12 ],
[ 4, 16, 17 ],
[ 4, 17, 5 ],
[ 5, 6, 13 ],
[ 5, 17, 6 ],
[ 7, 8, 14 ],
[ 7, 17, 18 ],
[ 7, 18, 8 ],
[ 8, 9, 10 ],
[ 8, 10, 14 ],
[ 8, 18, 9 ],
[ 10, 11, 12 ],
[ 10, 12, 14 ],
[ 12, 13, 14 ],
[ 15, 18, 19 ],
[ 15, 19, 16 ],
[ 16, 19, 17 ],
[ 17, 19, 18 ]
]
triangles from MATlab:
dim = 36x3
trianglesm = [
[ 0, 1, 11 ],
[ 0, 9, 18 ],
[ 0, 10, 9 ],
[ 0, 11, 10 ],
[ 0, 15, 1 ],
[ 0, 18, 15 ],
[ 1, 2, 11 ],
[ 1, 18, 2 ],
[ 2, 3, 11 ],
[ 2, 15, 16 ],
[ 2, 16, 3 ],
[ 3, 4, 12 ],
[ 3, 12, 11 ],
[ 3, 16, 4 ],
[ 4, 5, 12 ],
[ 4, 17, 5 ],
[ 5, 8, 13 ],
[ 5, 13, 12 ],
[ 5, 16, 17 ],
[ 5, 17, 6 ],
[ 6, 7, 14 ],
[ 6, 14, 13 ],
[ 6, 17, 7 ],
[ 7, 8, 14 ],
[ 7, 17, 18 ],
[ 7, 18, 8 ],
[ 8, 9, 10 ],
[ 8, 10, 14 ],
[ 8, 18, 9 ],
[ 10, 11, 14 ],
[ 11, 12, 13 ],
[ 11, 13, 14 ],
[ 15, 18, 19 ],
[ 15, 19, 16 ],
[ 16, 19, 17 ],
[ 17, 19, 18 ]
]
You could try the Bowyer-Watson algorithm but with circumsphere and tetrahedrons:https://en.m.wikipedia.org/wiki/Bowyer%E2%80%93Watson_algorithm.
I have :
mncs: [ [ 0 ], [ 3 ], [ 5 ], [ 90 ] ],
upMncs: [ [ 0 ], [ 3 ], [ 90 ] ],
criteria.mncs
criteria.upMncs
I need to get addMncs: [ [ 5 ] ], to cut same elements from first array and get unlike
I use lodash
Help please
You can use difference after flatten:
var criteria = {
mncs: [ [ 0 ], [ 3 ], [ 5 ], [ 90 ] ],
upMncs: [ [ 0 ], [ 3 ], [ 90 ] ],
}
console.log(_.difference(_.flatten(criteria.mncs), _.flatten(criteria.upMncs)))
// [5]
I'm new to javascript. And I tried to use Google Geochart to create a nice report.
From the example, the code should like:
function drawRegionsMap() {
var data = google.visualization.arrayToDataTable([
['Country', 'Popularity'],
['Germany', 200],
['United States', 300],
['Brazil', 400],
['Canada', 500],
['France', 600],
['RU', 700]
]);
var options = {};
var chart = new google.visualization.GeoChart(document.getElementById('chart_div'));
chart.draw(data, options);
};
And in my case, the data is from a ajax request. So I change my code as:
function drawRegionsMap() {
var jsonData = $.ajax({
url: "json/geo.pl",
dataType: 'json',
async: false
}).responseText;
var data = google.visualization.arrayToDataTable(jsonData);
var options = {};
var chart = new google.visualization.GeoChart(document.getElementById('chart_div'));
chart.draw(data, options);
};
But it not works for me. The Error message in Firefox is "Error: Not an array".
Hope someone can help me. Thanks!
The output of json/geo.pl is:
-bash-4.1$ json/geo.pl
Content-Type: application/json; charset=ISO-8859-1
[
[
"Country",
"Uploads"
],
[
"AL",
1
],
[
"AP",
1
],
[
"AR",
47
],
[
"AT",
25
],
[
"AU",
75
],
[
"AZ",
10
],
[
"BD",
4
],
[
"BE",
65
],
[
"BG",
15
],
[
"BN",
1
],
[
"BR",
177
],
[
"CA",
536
],
[
"CH",
37
],
[
"CI",
3
],
[
"CL",
35
],
[
"CN",
947
],
[
"CO",
4
],
[
"CS",
1
],
[
"CY",
2
],
[
"CZ",
20
],
[
"DE",
135
],
[
"DK",
51
],
[
"DO",
1
],
[
"EE",
26
],
[
"ES",
53
],
[
"EU",
2
],
[
"EU",
9
],
[
"FI",
19
],
[
"FR",
183
],
[
"GB",
341
],
[
"GE",
14
],
[
"GR",
11
],
[
"HK",
94
],
[
"HR",
2
],
[
"HU",
7
],
[
"ID",
37
],
[
"IE",
36
],
[
"IL",
122
],
[
"IN",
543
],
[
"IS",
5
],
[
"IT",
129
],
[
"JP",
200
],
[
"KG",
4
],
[
"KR",
451
],
[
"LB",
3
],
[
"LK",
1
],
[
"LT",
5
],
[
"LU",
4
],
[
"MA",
2
],
[
"MD",
3
],
[
"MO",
3
],
[
"MU",
2
],
[
"MX",
77
],
[
"MY",
43
],
[
"NG",
1
],
[
"NI",
1
],
[
"NL",
33
],
[
"NO",
27
],
[
"NZ",
4
],
[
"OM",
1
],
[
"PA",
4
],
[
"PH",
9
],
[
"PK",
20
],
[
"PL",
76
],
[
"PT",
14
],
[
"PY",
2
],
[
"QA",
2
],
[
"RO",
6
],
[
"RU",
164
],
[
"SA",
30
],
[
"SE",
148
],
[
"SG",
101
],
[
"SK",
34
],
[
"TH",
22
],
[
"TR",
28
],
[
"TT",
1
],
[
"TW",
110
],
[
"UA",
26
],
[
"US",
9386
],
[
"VE",
1
],
[
"VN",
28
],
[
"ZA",
89
],
[
"ZM",
1
]
]
I have fixed the issue by convert json/array from ajax responseText in to javascript array.
function drawRegionsMap() {
var jsonData = $.ajax({
url: "json/geo.pl",
dataType: 'json',
async: false
}).responseText;
var output = new Array();
output = JSON.Parse(jsonData);
var data = google.visualization.arrayToDataTable(output);
var options = {};
var chart = new google.visualization.GeoChart(document.getElementById('chart_div'));
chart.draw(data, options);
};