What do the numbers in Youtube Suggestion API mean? - javascript

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.

Related

how to sort this two sub array [ { [ ] } ,{ [ ] } , { [ ] } ,] in javascript

how can i sort this tests array based on discount_price i have sorted only the lab_partners array,,,can anyone tell me how to sort the main array nd so that array should look something like : 200 ,250, 299 ,500 so my array is something like this - [ { [ ] } ,{ [ ] } , { [ ] } ,]
"tests": [
{
"id": 11,
"habit_test_id": 3,
"test_id": 1,
"test": {
"id": 1,
"test_name": "Blood Test",
},
"lab_partners": [
{
"id": 1,
"discounted_price": 250,
},
{
"id": 7,
"discounted_price": 299,
}
]
},
{
"test": {
"id": 4,
"test_name": "MRI",
},
"lab_partners": [
{
"id": 6,
"discounted_price": 500,
},
{
"id": 6,
"discounted_price": 200,
}
]
}
]
}
//this only sort the lab_partners array i want to sort on the basis of both test array
const condition_based= tests.map((itr, idx) =>
itr.lab_partners.sort((firstItem, secondItem) => ( firstItem.discounted_price -
secondItem.display_price)));
console.log(condition_based,'condition_based');
I don't really know what you are trying to achieve, or what you expect from this array, but please look into this
https://www.w3schools.com/JS/js_object_constructors.asp
It seems to me like there are some similarities in all the object values, and you are missing out some maybe just because they are null values.

How to print words from object to specified position in javascript

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.

how to access nested values inside json object inside javascript [duplicate]

This question already has answers here:
How can I access object properties containing special characters?
(2 answers)
Closed 5 years ago.
I have this URL which returns me a JSON value, my task is to access 3 values out of it that is name, title and ISBN.
Since the content is heavily nested, I am not able to access the values inside it.
All the tutorials I have referred so far, have simple nesting in them
I have attached my code that I have written so far:
$(document).ready(function() {
var globalJsonVar;
alert("check");
$.getJSON("https://openlibrary.org/api/books?bibkeys=ISBN:9781606868829&jscmd=details&format=json", function(result) {
if (Object.keys(result).length > 0) {
alert("conetnt is present");
console.log(result);
alert(Object.values(result));
alert(result.ISBN: 9781606868829. bib_key); // my attempt at accessing the values
} else {
alert("hoax");
}
});
});
The JSON content that I am receiving is
{
"ISBN:9780143039648": {
"bib_key": "ISBN:9780143039648",
"preview": "noview",
"preview_url": "https://openlibrary.org/books/OL17924003M/The_guide",
"info_url": "https://openlibrary.org/books/OL17924003M/The_guide",
"details": {
"number_of_pages": 196,
"series": [
"Penguin classics"
],
"lc_classifications": [
"PR9499.3.N3 G85 2006"
],
"latest_revision": 7,
"uri_descriptions": [
"Contributor biographical information",
"Publisher description"
],
"genres": [
"Fiction."
],
"title": "The guide",
"languages": [{
"key": "/languages/eng"
}],
"subjects": [
"Malgudi (India : Imaginary place) -- Fiction.",
"Tour guides (Persons) -- Fiction.",
"Bharata natyam dancers -- Fiction.",
"Spiritual life -- Hinduism -- Fiction.",
"India -- Fiction."
],
"publish_country": "nyu",
"by_statement": "R.K. Narayan ; introduction by Michael Gorra.",
"type": {
"key": "/type/edition"
},
"uris": [
"http://www.loc.gov/catdir/enhancements/fy0716/2006044314-b.html",
"http://www.loc.gov/catdir/enhancements/fy0716/2006044314-d.html"
],
"revision": 7,
"publishers": [
"Penguin Books"
],
"dewey_decimal_class": [
"823/.914"
],
"last_modified": {
"type": "/type/datetime",
"value": "2012-08-05T21:10:58.887826"
},
"key": "/books/OL17924003M",
"authors": [{
"name": "Rasipuram Krishnaswamy Narayan",
"key": "/authors/OL5911201A"
}],
"publish_places": [
"New York"
],
"oclc_number": [
"65644730"
],
"pagination": "xxiv, 196 p. ;",
"created": {
"type": "/type/datetime",
"value": "2008-10-07T19:30:19.584308"
},
"url": [
"http://www.loc.gov/catdir/enhancements/fy0716/2006044314-b.html",
"http://www.loc.gov/catdir/enhancements/fy0716/2006044314-d.html"
],
"notes": {
"type": "/type/text",
"value": "Originally published: New York : Viking Press, 1958.\n\nIncludes bibliographical references (p. [xxi]-xxii)."
},
"identifiers": {
"goodreads": [
"129877"
],
"librarything": [
"6930"
]
},
"isbn_13": [
"9780143039648"
],
"lccn": [
"2006044314"
],
"isbn_10": [
"0143039644"
],
"publish_date": "2006",
"works": [{
"key": "/works/OL1057183W"
}]
}
}
}
You have to use [] if you want to have access to keys that have special chars, for example result['something-with:special-chars']
result.ISBN:9781606868829.bib_key
is a syntax error. : is not allowed here. You want to use the following syntay :
result["ISBN:9781606868829"].bib_key

How to get difference one of array from other array?

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]

JSON Syntax assistance

So I'm trying to manipulate some rave charts using JSON. The charts are used in Cognos and it uses visJSON if that helps. I am trying to add a trend line to my bar charts but all the syntax I have found isn't working. Any help would be greatly appreciated.
"id":"dataSet",
"fields":
[
{
"id":"categories",
"label":"",
"categories":
[
"abc",
"abc",
"abc"
]
},
{
"id":"series",
"label":"",
"categories":
[
"abc",
"abc",
"abc"
]
},
{
"id":"size",
"label":"abc"
}
],
"rows":
[
[
0,
0,
1500
],
[
0,
1,
1700
],
[
0,
2,
1600
],
[
1,
0,
2400
],
[
1,
1,
2200
],
[
1,
2,
2600
],
[
2,
0,
2800
],
[
2,
1,
1600
],
[
2,
2,
1800
]
]
}
],
A tool that you'd probably find helpful in the future:
http://jsonlint.com/
I ran your JSON through it and it returned this:
Parse error on line 1:
"id":"dataSet",
^
Expecting '{', '['
The problem was your extra array bracket at the end. Here it is corrected:
{
"id": "dataSet",
"fields": [
{
"id": "categories",
"label": "",
"categories": [
"abc",
"abc",
"abc"
]
},
{
"id": "series",
"label": "",
"categories": [
"abc",
"abc",
"abc"
]
},
{
"id": "size",
"label": "abc"
}
],
"rows": [
[
0,
0,
1500
],
[
0,
1,
1700
],
[
0,
2,
1600
],
[
1,
0,
2400
],
[
1,
1,
2200
],
[
1,
2,
2600
],
[
2,
0,
2800
],
[
2,
1,
1600
],
[
2,
2,
1800
]
]
}

Categories

Resources