I'm retrieving hashmap data from an api into my react app. the data looks like this..
[
[
"d243dc7-6fe8-151b-4ca8-1be528f2b36",
"[\"Jonny\",70]"
],
[
"8affa17-76d1-13e-6380-7cd2a3e3647",
"[\"Lucy\",106,"pic3.jpg"]"
],
[
"841cb28-24c7-872-3c66-63253800c8d",
"[\"Mike\",0]"
],
[
"6128e-182-cfb4-708b-c40a3ba2e6e",
"[\"Elodie\",23,"me.jpg"]"
],
[
"e55ef4c-8d41-3be4-27d-aae53330584",
"[\"Jacob\",9,"img-004.jpeg"]"
]
]
I need to render this data into a table.
Using map and with with data.slice(0, 1) I've managed to pull out the key (the long string), but I need help separating the name, number and optional image. I tried experimenting with various operators. I thought this would work
{data.slice(1, 2).toString().substring(2,data.slice(1, 2).length-2)}
but it just returns ["
I'm hoping there is a simpler way to do it!
You presented the data in a strange format, but this works:
const data = [
[
"d243dc7-6fe8-151b-4ca8-1be528f2b36",
"[\"Jonny\",70]"
],
[
"8affa17-76d1-13e-6380-7cd2a3e3647",
"[\"Lucy\",106,\"pic3.jpg\"]"
],
[
"841cb28-24c7-872-3c66-63253800c8d",
"[\"Mike\",0]"
],
[
"6128e-182-cfb4-708b-c40a3ba2e6e",
"[\"Elodie\",23,\"me.jpg\"]"
],
[
"e55ef4c-8d41-3be4-27d-aae53330584",
"[\"Jacob\",9,\"img-004.jpeg\"]"
]
];
const data1 = JSON.parse(data.slice(1, 2)[0][1]);
console.log('name', data1[0]);
console.log('numb', data1[1]);
console.log('file', data1[2]);
{data.slice(1, 2).toString().substring(2,data.slice(1, 2).length-2)}
^ ^^^^^^^^^^^^^^^^
In the string "["Jonny",70]" , you only want to skip the first character "[", so your first parameter of substring() should be index 1
you forgot to transfer data.slice(1, 2) to string, so it's still an array and its length would be 1
so your code should be revised to:
data.slice(1, 2).toString().substring(1, data.slice(1, 2).toString().length - 2)
Related
I want to convert this:
[null, 1890, null, NGU]
...into this:
[[], [1890], [], [NGU]]
I've tried creating a new array and pushing values to it, but that just ends up looking the same. Honestly, I'm unsure of what to even call what I'm trying to create. Is it a two-dimensional array or an array of objects?
This is for a google app script and the documentation calls it a two-dimensional array of values.
var arr = [null, 1890, null, 'NGU']
var arr2d = arr.map(x => [x])
console.log(arr2d) // output --> [ [ null ], [ 1890 ], [ null ], [ 'NGU' ] ]
I am trying to calculate and create an Array with Average Prices for different stocks.
For every stock, I have the Data in this format:
{
prices: [
[
1634304009628,
0.7118076876774715
],
[
1634307586874,
0.7063647246346818
],
[
1634311139365,
0.7049706990925925
],
[
1634313858611,
0.7085543691926037
],
[
1634318343009,
0.7057442983161784
]
]
}
For every stock API call I get the data like how I posted above, it has 2 values timestamp and the second one is the price. Now let's say I want the average trend for 5 stocks I will get the data in 5 different arrays and I just want to somehow make an average out of those 5 arrays in one to find the trend.
For the final result, I want the Array to be in the same format just with the calculated average altogether (the goal is to identify the trend direction).
What would be the best way to do that? I am Using React
first create and array of prices only and then using reduce you can just do this:
let myObj = {
prices: [
[
1634304009628,
0.7118076876774715
],
[
1634307586874,
0.7063647246346818
],
[
1634311139365,
0.7049706990925925
],
[
1634313858611,
0.7085543691926037
],
[
1634318343009,
0.7057442983161784
]
]
};
const average = arr => arr.reduce( ( p, c ) => p + c, 0 ) / arr.length;
const pricesArr = myObj.prices.map(function(value,index) { return value[1]; })
const result = average( pricesArr );
console.log(result);
I need to know the best way to get following results
courseFrequency : [
{
'courses': [
'a.i'
],
'count' : 1
},
{
'courses': [
'robotics'
],
'count' : 2
},
{
'courses': [
'software engineering', 'a.i'
],
'count' : 2
},
{
'courses': [
'software engineering', 'a.i','robotics'
],
'count' : 1
}
]
from following json data.
arr = [
{
'courses': [
'a.i'
]
},
{
'courses': [
'robotics'
]
},
{
'courses': [
'software engineering', 'a.i'
]
},
{
'courses': [
'robotics'
]
},
{
'courses': [
'software engineering', 'a.i'
],
'courses': [
'software engineering', 'a.i','robotics'
]
}];
Basically i need to find out the unique courses and their frequency. What is the most optimal way to do that ?
const hash = {}, result = [];
for(const {courses} of arr){
const k = courses.join("$");
if(hash[k]){
hash[k].count++;
} else {
result.push(hash[k] = { courses, count : 1 });
}
}
Simply use a hashmap to find duplicates. As arrays are compared by reference, we need to join it to a string for referencing ( note that this will fail if a coursename contains the joining symbol ($))
There both of them are best for area relates to them.These concepts are heaving their own property and methods to accomplish a certain task like JSON used for data transfer and cross browsing aspect as the common type data value.Arrays are really good at storing ordered lists and ordering things while the cost of removing/splicing elements is a bit higher.
JSON is a representation of the data structure, it's not an object or an array.
JSON can be used to send data from the server to the browser, for example, because it is easy for JavaScript to parse into a normal JavaScript data structure.for doing an action on JSON data you need to convert it into an object which is also seamed some property like ARRAY.
Arrays are really good at storing ordered lists and ordering things while the cost of removing/splicing elements is a bit higher.
Relative link
Relative link
Using cheerio, I have managed to scrape a PHP generated table that contains a column of dates, locations, etc. As the number of rows is variable, I opted to use .map() to iterate through each row, setting the matching starting event dates (startDate) with the provided CSS selectors. The above process seems to be functioning well, as when I call console.log(startDate) I recieve the output below. it would appear, however, that the process creates an array for each time it moves to the next row, appending an additional date each time. How can I set a variable to only the last array in the array startDate?
[ '03/18/2014' ]
[ '03/18/2014', '03/01/2014' ]
[ '03/18/2014', '03/01/2014', '02/15/2014' ]
[ '03/18/2014', '03/01/2014', '02/15/2014', '01/31/2014' ]
[ '03/18/2014',
'03/01/2014',
'02/15/2014',
'01/31/2014',
'01/17/2014' ]
[ '03/18/2014',
'03/01/2014',
'02/15/2014',
'01/31/2014',
'01/17/2014',
'12/06/2013' ]
[ '03/18/2014',
'03/01/2014',
'02/15/2014',
'01/31/2014',
'01/17/2014',
'12/06/2013',
'11/16/2013' ]
So the desired output of console.log(newArray) would be:
[ '03/18/2014',
'03/01/2014',
'02/15/2014',
'01/31/2014',
'01/17/2014',
'12/06/2013',
'11/16/2013' ]
If startDate is an array, you should be able to get the last item in the array by using the index, like this:
var lastStartDate = startDate[startDate.length-1]; //now lastStartDate contains the last item in the array
HI I am trying to create a JSON file in which I want to store some data for different files. The problem is I cannot figure the correct syntax. Here is what I have so far:
var object = {
"id-1" :[
{
"type":"Corporate Website",
"tech":"HTML" ,"CSS" , "Javascript/jQuery"
}
],
"id-2" :[
]
}
I seem to be getting an error at "tech".If that is not corect how can I enumarate multiple elements?I am sorry for the noob question I have been using javascript for a short period of time and I am still very confused with the language.
{
"id-1": [
{
"type": "Corporate Website",
"tech": [
"HTML",
"CSS",
"Javascript/jQuery"
]
}
],
"id-2": []
}
Note the array like syntax for "tech".
Tech should be an array (enclosed in square brackets):
"tech": ["HTML", "CSS", "Javascript/jQuery"]
Source:
An array is an ordered collection of values. An array begins with [
(left bracket) and ends with ] (right bracket). Values are separated
by , (comma).
http://www.json.org/