Parse CSV to JSON tree in Javascript [closed] - javascript

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 days ago.
Improve this question
My client provides me with a simple CSV of product configurations in a format like this:
Product
Config A
Config B
Price
A
1
1
100
A
1
2
200
A
2
1
300
A
2
2
200
B
1
1
100
B
2
1
100
In my front end, I would like to just work with this as a JSON tree of sorts (as this fits well to my logic). Something like:
products = {
A: {
1: {
1: 100,
2: 200,
},
2: {
1: 300,
2: 200,
},
},
B: {
1: {
1: 100,
2: 100,
},
},
};
However, I am having trouble trying to code something that feels efficient (and can scale - the number of configs could increase over time). Is there a standard library or pragmatic way to solve such a challenge? My research hasn't pointed me to something that is as simple as I would expect this to.

Related

javascript array to list of array [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed last month.
Improve this question
I have a following data, and I want to convert to list of array without key.
How do I do it?
Data
[{
"time": "01/13 15:50",
"price": 3000,
"changeRate": -0.27
},
{
"time": "01/13 15:40",
"price": 4000,
"changeRate": -0.27
}]
Desired output
[
[01/13 15:50, 3000],
[01/13 15:40, 4000]
]
Thank you in advance!
You can use the map method for that. It's used to transform (or "map") one array into a new one. The new array has the same length as the original, and its items are derived from the items in the original array.
data.map(item => [item.time, item.price]);

how to filter API data Find the 15 most spoken? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
Use the countries API to fetch data about countries. strong text
API_URL = 'https://restcountries.eu/rest/v2/all'
Use the countries API to fetch data about countries.
How many languages are there in the countries API
Find the 15 most spoken languages
// data fetched from API
let data = allcountrydata
// array to store lang with stats
let lang = []
data.forEach(country => {
for(let i = 0; i < country.languages.length; i++) {
if(Object.keys(lang).indexOf(country.languages[i].name) == -1)
lang[country.languages[i].name] = 1
else lang[country.languages[i].name] += 1
}
})
console.log(lang) // [Punjabi: 2 ,Afrikaans: 2, Albanian: 3, Amharic:
1, Arabic: 25, Armenian: 2, Aymara: 1, Azerbaijani: 1, Belarusian: 1,
Bengali: 1, Bislama: 1, Bosnian: 2.......] there are 112 different
languages
The data for each country has a population value for the country and a list of languages spoken in the country.
However each language in the list does not seem to have a count of speakers, so unless that data is there, you cannot figure out the most spoken languages unless you assume that 100 % of the population of each country speaks all the languages spoken in the country - and that would be quite an assumption.
I think it is not possible to answer "15 most spoken languages" from the data provided in https://restcountries.eu/rest/v2/all

How to create an Auto Increment Sequence with mongoose library [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
how to create an auto increment sequence with prefix, for example: USR00001 etc…
Little details: language Nodejs, and library mongoose.
Thank you.
Regards,
Michele
You can create on collection where you can keep all your sequences and then can use findAndUpdate to get them incremented automatically
collection - sequences
[
{
_id: "sequence",
"value": 100000
}
]
Then you can use this query to get the latest number in your format
db.sample.findAndModify({
query:{
_id: "sequence"
},
update: {
$inc:{
value:1
}
},
fields:{
count: {$concat: [ "USR", { $toString: "$value" } ] },
_id: 0
}
})
Output:
{ "value" : "USR100000" }
For more details you can check the official documentation of findAndUpdate
You can read this link, This has explained the way of doing it. Not sure how you will use this with Mongoose.
https://www.tutorialspoint.com/mongodb/mongodb_autoincrement_sequence.htm

Creating a graph with multiple levels [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 4 years ago.
Improve this question
I need to create a graph with a dynamic number of vertices, which will look like this
enter image description here
Which layout is best used to get vertical vertices?
Force directed graph might be a solution to your problem. Here are a few options:
https://zoomcharts.com/en/chart-types-demo/graph-chart-using-netchart
https://bl.ocks.org/mbostock/4062045
http://visjs.org/examples/network/basicUsage.html
If you want to have the layout going explicitly in vertical way, you might need to consider either hierarchical or fixed layout like here:
http://jsfiddle.net/L9m34ev5/1/
var t = new NetChart({
container: document.getElementById("demo"),
area: { height: null },
navigation:{
focusNodeExpansionRadius: 2,
initialNodes: ["syslog"],
mode:"focusnodes"
},
style:{
node:{display:"image",lineWidth:2, lineColor: "red", imageCropping: true},
nodeStyleFunction: function(node){node.radius = 50;}
},
data: { url:"https://zoomcharts.com/dvsl/data/net-chart/bubbles.json" },
layout:{
mode:"hierarchy",
nodeSpacing: 45, // horizontal spacing between nodes
rowSpacing: 80, // vertical spacing between node rows in the hierarchy layout
rotation: 90
}
});

how can I collet data from JSON file in r? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
If in the JSON file it's like:
"A": {
"O": "F(1)",
"C": 12,
"N": 39783,
"D": 4233,
"H": 38174,
"W": 281,
"S": 2624
},
"E": [ { "#1": 382.35 }]
}
I need value of C, N,D .....E
You can get the values of individual elements using library(jqr)
library(jqr)
jq(js, ".A.C")
# 12
jq(js, ".A.N")
# 39783
or you can read the JSON into R using library(jsonlite), which will put it into an R data structure (with the JSON the way it is, it will be a list). You then access the elements of the list using the usual list subsetting techniques
library(jsonlite)
lst <- fromJSON(js)
lst$A$C
# 12
lst$E
# #1
# 1 382.35

Categories

Resources