im using JSON to return an array.
Json:
const data = [{
"week": 1,
"lost": 10,
"recovery_timespan": [{
"week": 2,
"count": 1
}, {
"week": 3,
"count": 0
}],
"netLost": 10,
"netReturned": 20
}, {
"week": 2,
"lost": 7,
"recovery_timespan": [{
"week": 3,
"count": 1
}, {
"week": 4,
"count": 3
}],
"netLost": 30,
"netReturned": 200
}, {
"week": 3,
"lost": 8,
"recovery_timespan": [{
"week": 4,
"count": 1
}],
"netLost": 50,
"netReturned": 40
}];
i need to get the data into a array with lost,counts of recovery_timespan,netLost,netReturned.
Expected Output:
[ [ 10, 1, 0, 10, 20 ],
[ 7, 1, 3, 30, 200 ],
[ 8, 1, 50, 40 ] ]
My approach:
const result = data.map(({lost, recovery_timespan,netLost,netReturned}) => [
lost,
...recovery_timespan.map(({count}) => count),
,netLost,netReturned
]);
console.log(result);
and this return array with <1 empty item>:
[ [ 10, 1, 0, <1 empty item>, 10, 20 ],
[ 7, 1, 3, <1 empty item>, 30, 200 ],
[ 8, 1, <1 empty item>, 50, 40 ] ]
Wha is the issue here?
Why am i getting <1 empty item>
You have an extra comma:
const result = data.map(({lost, recovery_timespan,netLost,netReturned}) => [
lost,
...recovery_timespan.map(({count}) => count),
here ---> ,netLost,netReturned
]);
Just remove it.
You have an additional , after the nested map:
const result = data.map(({lost, recovery_timespan,netLost,netReturned}) => [
lost,
...recovery_timespan.map(({count}) => count), // <--
,netLost,netReturned
//^--
]);
That creates a hole in the array. That's why you are seeing <1 empty item> in the output
console.log([1,,2])
const res = data.map((el) => [
el.lost,
...el.recovery_timespan.map((timespan) => timespan.count),
/* extra comma here --> */, el.netLost,
el.netReturned
])
[ [ 10, 1, 0, 10, 20 ], [ 7, 1, 3, 30, 200 ], [ 8, 1, 50, 40 ] ]
Not completly sure, but maybe try this.
...recovery_timespan.map(({count}) => count.count)
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.
This question already has answers here:
Copy array by value
(39 answers)
Closed 5 years ago.
Here is my code:
var arr = [];
var obj = {};
(function() {
for(let i = 1; i <= 10; i++) {
arr.push(i);
obj[i] = arr;
}
})();
This code gave me this output:
{
'1': [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ],
'2': [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ],
'3': [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ],
'4': [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ],
'5': [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ],
'6': [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ],
'7': [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ],
'8': [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ],
'9': [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ],
'10': [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ]
}
Why doesn't it give an output like this?
{
'1': [ 1 ],
'2': [ 1, 2 ],
'3': [ 1, 2, 3 ],
'4': [ 1, 2, 3, 4 ],
'5': [ 1, 2, 3, 4, 5 ],
'6': [ 1, 2, 3, 4, 5, 6 ],
'7': [ 1, 2, 3, 4, 5, 6, 7 ],
'8': [ 1, 2, 3, 4, 5, 6, 7, 8 ],
'9': [ 1, 2, 3, 4, 5, 6, 7, 8, 9 ],
'10': [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ]
}
When you push, you're pushing the reference to the same array each time. What you actually want is to create a new array each time rather than reference the same one.
Check out this SO answer for more info Javascript by reference vs. by value
You could do this by pushing a copy arr using arr.slice or Array#from. At the very end arr will have all 10 numbers in it, but obj will look exactly like your output.
var arr = [];
var obj = {};
(function() {
for(let i = 1; i <= 10; i++) {
arr.push(i);
obj[i] = arr.slice();
}
console.log(obj);
})();
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
]
]
}