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 6 days ago.
Improve this question
The code below returns the output in two separate arrays. I would like to set these up as key-value pairs so that the two arrays combine and match up (The first element of the first array should match up with the first element of the second array).
Before:
[
[
'NAME',
'Annual Income',
'Labor Force',
'Total Employment',
'Manufacturing Employment',
'Total Unemployed',
'state',
'place'
],
[
'Bolivar city, Missouri',
'37233',
'4318',
'3988',
'388',
'173',
'29',
'06976'
]
]
After:
[['NAME':'Bolivar city, Missouri'],['ANNUAL INCOME':37233]...]
The purpose of this is to export the data to an excel file and have that data put into a database.
var XMLHttpRequest = require('xhr2');
let request = new XMLHttpRequest();
year = "2021";
request.open("GET", "https://api.census.gov/data/"+ year +"/acs/acs5/subject?get=NAME,S1901_C01_012E,S2406_C01_001E,S1701_C01_028E,S2407_C01_004E,S1701_C01_031E&for=place:06976&in=state:29");
//let array = [(request[0,1])];
request.send();
request.onload = () =>
{
console.log(request);
if (request.status == 200)
{
console.log(JSON.parse(request.response.replace("S1901_C01_012E", "Annual Income").replace("S2406_C01_001E", "Labor Force").replace("S1701_C01_028E", "Total Employment").replace("S2407_C01_004E", "Manufacturing Employment").replace("S1701_C01_031E", "Total Unemployed")));
//console.log(JSON.parse(array));
}
else
{
console.log(`error ${request.status} ${request.statusText}`)
}
}
I tried declaring an array but I think I was inputting the "request" variable incorrectly
Related
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 2 months ago.
Improve this question
// I wanted to create a variable to hold the month and a variable that hold the number...
var finances = [
['Jan-1999', 867886],
['Feb-1999', 984653],
['Mar-1999', 322012],
['Apr-1999', -69417],
['May-1999', 310503],
['Jun-1999', 522857],
['Jul-1999', 1033096],
['Aug-1999', 604885],
['Sep-1999', -216386],
['Oct-1999', 477532],
['Nov-1999', 893810]];
What you need is map() that returns a new array with the result of a function applied to your initial array. In this case your functions would either select the first element of the subarray (the date), or the second element (the number).
var finances = [
['Jan-1999', 867886],
['Feb-1999', 984653],
['Mar-1999', 322012],
['Apr-1999', -69417],
['May-1999', 310503],
['Jun-1999', 522857],
['Jul-1999', 1033096],
['Aug-1999', 604885],
['Sep-1999', -216386],
['Oct-1999', 477532],
['Nov-1999', 893810]];
const dates = finances.map((e) => e[0]);
const numbers = finances.map((e) => e[1]);
console.log(dates);
console.log(numbers);
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 3 months ago.
Improve this question
I am fairly new to Javascript is there a way in which we can achieve the following result?
const bikes = ["honda","kawasaki",suzuki]
The main object array can be divided into equal parts based on the number of bikes, like the first few rows with the main object will be "honda", the next few groups of rows "Kawasaki" and if more rows are added to the bikes that divided the main object further into equal parts and update
const mainObject =[
{ name:"john", age:"23"},
{ name:"Max", age:"26"},
{ name:"Tim", age:"28"},
{ name:"Jim", age:"28"},
{ name:"Jacob", age:"28"},
{ name:"Luke", age:"28"}
]
The expected Outcome should be
const mainObject =[
{ name:"john", age:"23",bike:"honda"},
{ name:"Max", age:"26",bike:"honda"},
{ name:"Tim", age:"28",bike:"kawasaki"},
{ name:"Jim", age:"28",bike:"kawasaki"},
{ name:"Jacob", age:"28",bike:"suzuki"},
{ name:"Luke", age:"28", bike:"suzuki"}
]
Something like this should do the trick:
result = mainObject.map((o, i) => ({
...o,
bike: bikes[Math.floor(i * bikes.length / mainObject.length)]
}))
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 months ago.
Improve this question
Suppose I have following array
array = [
{
optionA: "QA1",
optionB: "QB1",
optionC: "QC1",
optionD: "QD1",
},
{
optionA: "QA2",
optionB: "QB2",
optionC: "QC2",
optionD: "QD2",
optionE: "QD2",
},
{
optionA: "QA3",
optionB: "QB3",
}
]
Expected output
tableHeader = {OptionA,OptionB,OptionC,OptionD,OptionE}
Wanted display questions data in a table format, which is not organised and has n number for options.
You can get all keys as follows
const array = [{optionA: "QA1",optionB: "QB1",optionC: "QC1",optionD: "QD1",},{optionA: "QA2",optionB: "QB2",optionC: "QC2",optionD: "QD2",optionE: "QD2",},{optionA: "QA3",optionB: "QB3",}]
const allKeys = Object.keys(Object.assign({}, ...array))
console.log(allKeys)
Try this one
let header = new Set();
array.forEach((a) => {
Object.keys(a).forEach((item) => {
header.add(item);
});
});
console.log(header);
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
Could you please help me to prepare below Object from string using JS. I am new in JS.
health status index uuid pri rep docs.count docs.deleted store.size pri.store.size
yellow open new_log jdbc1233q6AiRUSRCczayjz0rw 1 1 603442 0 127.6mb 127.6mb
From above string how do I create below object?
{
"health": "yellow",
"status": "open",
"index": "new_log",
"uuid": "jdbc1233q6AiRUSRCczayjz0rw",
"pri": "1",
"rep": "1",
"docs.count": "603442",
"docs.deleted": "0",
"store.size": "127.6mb",
"pri.store.size": "127.6mb"
}
Here is how you can use split
const str = `health status index uuid pri rep docs.count docs.deleted store.size pri.store.size
yellow open new_log jdbc1233q6AiRUSRCczayjz0rw 1 1 603442 0 127.6mb 127.6mb`
const lines = str.split(/\n/); // split lines on \n
const arrs = lines.map(line => line.split(/\s+/)); // split line on whitespace
const obj = {}; // define an object, we could use reduce but this is easier to read
arrs[0].forEach((word,i) => obj[word] = arrs[1][i])
console.log(obj); // show it
console.log(JSON.stringify(obj)); // show the JSON
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
I have a json file like below:
{ "userdata": { "userid123": {"uname": " john", "uemail": "john#mail.com"}, "userid124": {"uname": "sam", "uemail": "sam#mail.com"} }
I want to parser user details if someone go to url http://example.com/?userid123
So only details of userid123 (which is in the url, get it from there) will be shown.
Json string you added to the question is wrong.
If you need to get the string after the question mark and test if the corresponding value exist or not, you can write:
var js = {
"userdata": {
"userid123": {"uname": " john", "uemail": "john#mail.com"},
"userid124": {"uname": "sam", "uemail": "sam#mail.com"}
}
};
var sp = window.location.search.substr(1);
//for testing purposes
sp='userid123';
if (js.userdata[sp] !== undefined) {
console.log('uname: ' + js.userdata[sp].uname + ' uemail: ' + js.userdata[sp].uemail)
}
First of all you have to save userId to variable like
var userId = location.search.substr(1);
.substr(1) <- this will remove ? from result of location.search();
Now since you are using jquery you can access your json (and user data) as below
$.getJSON( "users.json", function( data ) {
var userData = data[userId]; // your user data
}