Changing JSON Object Format [closed] - javascript

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 8 years ago.
Improve this question
i got a JSOn object with over 200 entrys. Actually it looks like
params = {
key_a_1 : "value_a_1"
key b_1 : "value_b_1"
key_c_1 : "value_c_1"
key d_1 : "value_d_1"
key_a_2 : "value_a_2"
key b_2 : "value_b_2"
key_c_2 : "value_c_2"
key d_2 : "value_d_2"
}
and I need to convert it to
params = {
1
{
key_a : "value_a_1"
key b : "value_b_1"
key_c : "value_c_1"
key d : "value_d_1"
}
2
{
"key_a : value_a_2"
"key b : value_b_2"
"key_c : value_c_2"
"key d : value_d_2"
}
}
key can be named anything else but the number is always the id.
any idea whats the best way? thanks!
Basically the problem is, that I need the ID which is at the end of the key as an own array inside the JSON Object and my Javascript skills aren't great enough.

Just iterate over the properties and fill them into another object:
var newObject = {};
Object.keys(oldObject).forEach(function (key) {
var name = /(key_\w+)_\d+/.exec(key)[1],
index = /key_\w+_(\d+)/.exec(key)[1];
if (!newObject[index]) {
newObject[index] = {};
}
newObject[index][name] = oldObject[key];
});
console.log(newObject);

Related

How to find keys from undefined schema [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 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);

Create JavaScript Object from string [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
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

Javascript sorting array by partial string [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 3 years ago.
Improve this question
I have an array like this
var myArray = [
"J20J205147902_B_B716202",
"R20Q205147902_F_B716202",
"P20W205147_902_alternate1",
"M20K205147_902_alternate4",
"F20G205147_902_alternate3",
"K20J205147_902_alternate2",
"L20H205147_902_main"];
I want to sort this array by this pattern
var map = {
"_F_":1,
"_main":2,
"_alternate1":3,
"_alternate2":4,
"_alternate3":5,
"_alternate4":6,
"_alternate5":7,
"_B_":8
};
So, the output should be like this
"R20Q205147902_F_B716202",
"L20H205147_902_main",
"P20W205147_902_alternate1",
"K20J205147_902_alternate2",
"F20G205147_902_alternate3",
"M20K205147_902_alternate4",
"J20J205147902_B_B716202"
You can use a custom sorting comparator function as follows:
var myArray = [
"J20J205147902_B_B716202",
"R20Q205147902_F_B716202",
"P20W205147_902_alternate1",
"M20K205147_902_alternate4",
"F20G205147_902_alternate3",
"K20J205147_902_alternate2",
"L20H205147_902_main"];
var map = {
"_F_":1,
"_main":2,
"_alternate1":3,
"_alternate2":4,
"_alternate3":5,
"_alternate4":6,
"_alternate5":7,
"_B_":8
}
myArray.sort((...args) => {
const [a, b] = args.map(str => Object.keys(map).find(key => str.includes(key)))
return map[a] - map[b];
})
console.log(myArray)

Json parse by search id [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
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
}

Handle multiple string in Array of String javascript [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 5 years ago.
Improve this question
I am getting data in array like this, left side are account name and right one are subscriptions.
7ef:" 3sdc 12exf"
12ef:" 8ecg"
10ef:" 3ecf 3egf 3elm 3ecf 3egf 3elm "
mean 7ef index have 2 strings of data and there is space between both. and 10ef have 6 strings of data.I need this data in form of like
7ef:" 3sdc"
7ef:" 12exf"
12ef:" 8ecg"
10ef:" 3ecf "
10ef:"3egf"
10ef:"3elm"
10ef:"3ecf"
10ef:"3egf"
10ef:"3elm "
As I have to sent it to make its csv file. How Can I do that in javascript ?
Code of creating this is like
foreach ($subscriptions as $subscription) {
$str=$str.' '.$subscription->uuid;
}$anew[account[$i]]=$str;
EDIT :
Now after reading comments can some one tell me how can I make it like
0:" 3sdc"
1:" 12exf"
2:" 8ecg"
like I will save account for later and now I want to arrange them all.
This won't be possible because Javascript Object at same level can have only have unique key, so it will just override the value
SUGGESTED:
use the structure like this
{
7ef:["3sdc", "12exf"],
12ef:["8ecg"],
10ef:["3ecf","3egf", "3elm", "3ecf", "3egf", "3elm"]
}
code :
for (var keys in a){
for (var data in a[keys]){
console.log(keys + " : "+ data)
}
}
a is the var holding the above js object
Change your PHP code to produce a better structure:
$lst = [];
foreach ($subscriptions as $subscription) {
$lst[] = $subscription->uuid;
}
$anew[account[$i]] = $lst;
I suppose you have somewhere:
echo json_encode($anew);
This will give you the following structure in JavaScript:
{
"7ef": ["3sdc", "12exf"],
"12ef": ["8ecg"],
"10ef": ["3ecf","3egf", "3elm", "3ecf", "3egf", "3elm"]
}
To output this as CSV, you could do this (assuming the data is in response):
const response = {
"7ef": ["3sdc", "12exf"],
"12ef": ["8ecg"],
"10ef": ["3ecf","3egf", "3elm", "3ecf", "3egf", "3elm"]
};
const csv = Object.keys(response).reduce( (acc, key) =>
acc.concat(response[key].map( uuid => [key, uuid].join(", ") ))
, []).join("\n");
console.log(csv);

Categories

Resources