I have a 2D array of x and y coordinates in Javascript, where the array looks like this:
---> 0: 0 1 .....
{x:1, y:1}, {x:1, y:2} .....
---> 1: 0 1 .....
{x:1, y:1}, {x:1, y:2} .....
So if I for instance write Array[0][0].x, the output is 1.
I would like to turn this array into a JSON string, which would have the following syntax:
{
"name0": [
{
"0": [
{
"x": "1",
"y": "1"
},
{
"x": "1",
"y": "2"
}
],
"1": [
{
"x": "1",
"y": "2"
},
{
"x": "2",
"y": "1"
}
]
}
],
"name1": [
{
"0": [
{
"x": "1",
"y": "1"
},
{
"x": "1",
"y": "2"
}
],
"1": [
{
"x": "1",
"y": "2"
},
{
"x": "2",
"y": "1"
}
]
}
]
}
where name0 and name1 (nameX) are not inside the mentioned 2d array, but passed from somewhere else inside the function where I am creating the JSON. Also, each nameX object is supposed to be pushed into the JSON with the call of this function.
var data;
data[name1] = yourDataA;
data[name2] = yourDataB;
json = JSON.parse(data);
Just prepare your data var the way you need it.
use https://github.com/douglascrockford/JSON-js library, just include the code and use the JSON.stringify() method for converting your array.
Related
I would like to create a jsonpath to take first part of whole json. I have trouble because I dont have any key for sub jsons.
For Example, I have this json;
[
[
"name",
"surname",
"age"
],
[
{
"X": "Mike",
"Y": "Tyson"
},
{
"X": "Irina",
"Y": "Shat"
}
]
]
I want to divide this json to 2 different json;
[
"name",
"surname",
"age"
]
[
{
"X": "Mike",
"Y": "Tyson"
},
{
"X": "Irina",
"Y": "Shat"
}
]
I try to use index but doesn't work.
From this array:
[{
"map": {
"name": "2",
"y": 2
}
}, {
"map": {
"name": "4",
"y": 17494
}
}, {
"map": {
"name": "3",
"y": 2
}
}, {
"map": {
"name": "1",
"y": 1
}
}]
I want this data structure:
[{
"name": "2",
"y": 2
}, {
"name": "4",
"y": 17494
}, {
"name": "3",
"y": 2
}, {
"name": "1",
"y": 1
}]
How can I do this?
Use JavaScript mapping:
let json = [{
"map": {
"name": "2",
"y": 2
}
}, {
"map": {
"name": "4",
"y": 17494
}
}, {
"map": {
"name": "3",
"y": 2
}
}, {
"map": {
"name": "1",
"y": 1
}
}];
let result = json.map(item => item.map);
result.forEach(i => console.log(i));
Above code prints:
{name: "2", y: 2}
{name: "4", y: 17494}
{name: "3", y: 2}
{name: "1", y: 1}
See:
Array.map
Use Array.prototype.reduce()
let input=[{
"map": {
"name": "2",
"y": 2
}
}, {
"map": {
"name": "4",
"y": 17494
}
}, {
"map": {
"name": "3",
"y": 2
}
}, {
"map": {
"name": "1",
"y": 1
}
}]
let revisedarray=input.reduce((acc,val)=>{
acc.push(val.map);
return acc;
},[])
console.log(revisedarray)
For more info about array reduce you can look at-mdn docs
Hi you can do this via pure js by looping through the array and extracting out the required output. Please check the code below.
var arr = [{ "map": { "name": "2", "y": 2 } }, { "map": { "name": "4", "y": 17494 } }, { "map": { "name": "3", "y": 2 } }, { "map": { "name": "1", "y": 1 } }];
var resultArr = arr.map(function(item) {
return item.map;
})
ES6 Short syntax, here A is original array of objects and A2 is output array of objects
let A2 = A.map(o=>o.map);
let jsonString = your_jsonString;
var blankArr = [];
let result = jsonString.map(item => item.map);
result.forEach(i => blankArr.push(i));
console.log(blankArr);
I get some records from a database in the following JSON format:
data: [{
"y": "0.652008685",
"x": "-0.13926217",
"geneName": "ADAMTS2",
"cond": "Cell"
},
{
"y": "-3.486001",
"x": "-2.295312",
"geneName": "IGSF22",
"cond": "ECM"
},
{
"y": "-3.597706",
"x": "-2.672138",
"geneName": "OXA1L",
"cond": "ECM"
}
]
I would like to transform the above result and group the y,x and geneName name/value pairs based on the cond key using JavaScript.
The result I'd like is shown below:
series: [{
name: 'Cell',
color: '#fff',
data: [{
"name": "ADAMTS2",
"x": -0.13926217,
"y": 0.652008685
}]
},
{
name: 'ECM',
color: '#000',
data: [{
"name": "IGSF22",
"x": -2.295312,
"y": -3.486001
},
{
"name": "OXA1L",
"x": -2.672138,
"y": -3.597706
}
]
}
]
For every different grouping I'd like to add an extra name/value pair color.
Is there any smart and quick way to do it using JavaScript by avoiding the naive approach of the for loops?
Thanks in advance
You could use Array.prototype.reduce to group common objects by object.cond, like so:
var data = [{
"y": "0.652008685",
"x": "-0.13926217",
"geneName": "ADAMTS2",
"cond": "Cell"
},
{
"y": "-3.486001",
"x": "-2.295312",
"geneName": "IGSF22",
"cond": "ECM"
},
{
"y": "-3.597706",
"x": "-2.672138",
"geneName": "OXA1L",
"cond": "ECM"
}
];
var dataMap = data.reduce((result, item) => {
// create root-level object for a name if it doesn't already exist
if (!result[item.cond]) {
result[item.cond] = {
name: item.cond,
color: ''/*not sure what your logic is here*/,
data: []
}
}
// add current item to the root-level object data
result[item.cond].data.push({
name: item.geneName,
x: parseFloat(item.x),
y: parseFloat(item.y)
});
return result;
}, {/*resulting map*/});
// last step is to get an array of the values since that's the desired format
data = Object.values(dataMap);
document.getElementById('result').innerHTML = JSON.stringify(data, null, 4);
<pre id="result"></pre>
You can use .reduce here. Make a object whose key is basically cond and then from that object transform a array using .map.
var x = {
data: [{
"y": "0.652008685",
"x": "-0.13926217",
"geneName": "ADAMTS2",
"cond": "Cell"
},
{
"y": "-3.486001",
"x": "-2.295312",
"geneName": "IGSF22",
"cond": "ECM"
},
{
"y": "-3.597706",
"x": "-2.672138",
"geneName": "OXA1L",
"cond": "ECM"
}
]
};
x.data = Object.entries(x.data.reduce((acc, el) => {
let cond = el.cond;
delete el.cond;
if(acc.hasOwnProperty(cond)){
acc[cond].data.push(el);
}
else{
acc[cond] = {};
acc[cond].data = [el];
}
return acc;
}, {})).map(el => {
return {name: el[0], data: el[1].data};
});
console.log(x);
data = [{
"y": "0.652008685",
"x": "-0.13926217",
"geneName": "ADAMTS2",
"cond": "Cell"
},
{
"y": "-3.486001",
"x": "-2.295312",
"geneName": "IGSF22",
"cond": "ECM"
},
{
"y": "-3.597706",
"x": "-2.672138",
"geneName": "OXA1L",
"cond": "ECM"
}
]
series = []
definedName = []
data.forEach(function(item) {
var ind = definedName.findIndex((element) => {
return element == item.cond;
});
if (ind === -1) {
obj = {
name: item.cond,
color: '#fff',
data: [{
"name": item.geneName,
"x": item.x,
"y": item.y
}]
}
definedName.push(item.cond)
series.push(obj)
} else {
obj = {
"name": item.geneName,
"x": item.x,
"y": item.y
}
series[ind]["data"].push(obj)
}
});
console.log(series)
I'm using Underscore.js to map a new object array out of an existing object array but cannot really get the desired results.
Essentially I have an object array like:
[
{
"total": 5.21,
"number": 3,
"a": "Paid",
"y": 2015,
"m": 1,
"d": "2015-01-17T23:58:34.115Z"
},
{
"total": 374.65,
"number": 3,
"a": "Scheduled",
"y": 2015,
"m": 1,
"d": "2015-01-18T02:16:03.503Z"
},
{
"total": 310.84,
"number": 1,
"a": "Paid",
"y": 2015,
"m": 1,
"d": "2015-01-17T23:58:34.115Z"
},
{
"total": 284.41,
"number": 3,
"a": "Scheduled",
"y": 2015,
"m": 1,
"d": "2015-01-18T02:16:03.503Z"
}
]
which I would like to map into something like:
[
{
"key": "Paid",
"values": [
[
"2015-01-17T23:58:34.115Z",
5.21
],
[
"2015-01-17T23:58:34.115Z",
310.84
]
]
},
{
"key": "Scheduled",
"values": [
[
"2015-01-18T02:16:03.503Z",
374.65
],
[
"2015-01-18T02:16:03.503Z",
284.41
]
]
}
]
I've tried using the ._map method returns a map like this (JSFiddle):
var mapped_bill = _.map(bill, function(item) {
return {"key": item.a, "values": [item.d, item.total]}
});
console.log(JSON.stringify(mapped_bill));
/* returns:
[
{
"key": "Paid",
"values": [
"2015-01-17T23:58:34.115Z",
5.21
]
},
{
"key": "Scheduled",
"values": [
"2015-01-18T02:16:03.503Z",
374.65
]
},
{
"key": "Paid",
"values": [
"2015-01-17T23:58:34.115Z",
310.84
]
},
{
"key": "Scheduled",
"values": [
"2015-01-18T02:16:03.503Z",
284.41
]
}
]
*/
How do I group the resulting map above so that I can achieve the desired map?
You can use two _.map methods with _.groupBy:
var result = _.map(_.groupBy(data, 'a'), function(el, key) {
return {
key: key,
values: _.map(el, function(item) {
return [item.d, item.total];
})
};
});
Check the demo below.
var data = [
{
"total": 5.21,
"number": 3,
"a": "Paid",
"y": 2015,
"m": 1,
"d": "2015-01-17T23:58:34.115Z"
},
{
"total": 374.65,
"number": 3,
"a": "Scheduled",
"y": 2015,
"m": 1,
"d": "2015-01-18T02:16:03.503Z"
},
{
"total": 310.84,
"number": 1,
"a": "Paid",
"y": 2015,
"m": 1,
"d": "2015-01-17T23:58:34.115Z"
},
{
"total": 284.41,
"number": 3,
"a": "Scheduled",
"y": 2015,
"m": 1,
"d": "2015-01-18T02:16:03.503Z"
}
];
var result = _.map(_.groupBy(data, 'a'), function(el, key) {
return {
key: key,
values: _.map(el, function(item) {
return [item.d, item.total];
})
};
});
pre.innerHTML = JSON.stringify(result, null, 4);
<script src="//cdnjs.cloudflare.com/ajax/libs/underscore.js/1.7.0/underscore-min.js"></script>
<pre id="pre"></pre>
Going by your history here then it could be assumed this is actually a mongodb question even though you are just looking at the resulting JavaScript object in the question as presented.
So with the structure you mention actually being the members of a MongoDB collection then the answer to produce the the required output would be:
db.mapped.aggregate([
{ "$group": {
"_id": "$a",
"values": {
"$push": {
"$map": {
"input": { "$literal": ["A","B"] },
"as": "l",
"in": {
"$cond": [
{ "$eq": [ "$$l", "A" ] },
"$d",
"$total"
]
}
}
}
}
}}
])
So the $map operator there processes our "two element array template" provided in the $literal operator section, and "transposes" the values via the $cond "ternary" to either produce the element from "$d" where the first "A" element is matched or the element "$total" where the element is not "A" but therefore "B" as the only other logical choice.
Results in mapping an array that has the first elementof the first match and the second element as the other expected value. These can then be provided to $push, to create and "array of arrays" as requested.
Which produces from your source as a collection:
{
"_id" : "Scheduled",
"values" : [
[
"2015-01-18T02:16:03.503Z",
374.65
],
[
"2015-01-18T02:16:03.503Z",
284.41
]
]
},
{
"_id" : "Paid",
"values" : [
[
"2015-01-17T23:58:34.115Z",
5.21
],
[
"2015-01-17T23:58:34.115Z",
310.84
]
]
}
So you didn't need this post processing in JavaScript as you thought you did. Using the operators that are appropriate to match your conditions on the server side $group is all you need.
I have a json like so:
{
"a": {
"x": {
"y": {
"a": {},
"z": {},
"b": {}
}
},
"c": {},
"b": {
"c": {
"d": {}
}
},
"d": {},
...
}
}
Is there a quick way to convert it to flare.json format?
Like so:
{
"name":"a",
"children":[
{
"name":"x",
"children":
{
"name":"y",
"children":[{"name":"a", "size":0},{"name":"z","size":0},{"name":"b","size":0}]
...
}
Thank you.
I have come up with a series of regex transforms for this.
#replace-this #with-this
^\s*" \{"name":"
: \{\}, \},
: \{\} \}
": \{$ ","children":\[
^\s*\}, \]\},
^\s*\} \]\}
#in that order
and just delete the first and last line (should be an extra { and ]} )
on applying these regex transforms,
this:
{
"a": {
"x": {
"y": {
"a": {},
"z": {},
"b": {}
}
},
"c": {},
"b": {
"c": {
"d": {}
}
},
"d": {}
}
}
will become this:
{
"name": "a",
"children": [{
"name": "x",
"children": [{
"name": "y",
"children": [{
"name": "a"
},
{
"name": "z"
},
{
"name": "b"
}]
}]
},
{
"name": "c"
},
{
"name": "b",
"children": [{
"name": "c",
"children": [{
"name": "d"
}]
}]
},
{
"name": "d"
}]
}
which can then be used with some of the d3js examples.
You are going to have to write your own JSON or a function to dynamically change the JSON. Flare.json just follows a schema that adheres to Mike Bostock's d3 files.
I'll give you a hint. The schema that you wrote appears to be (in psuedocode)
array("name":"a", "children":array("name":"x","children":array(.....
Basically, you need to create a multidimensional array in order to get the desired results. I don't know how you are getting your data, unfortunately, so I can't tell you much more. If using php use the json_encode method
echo json_encode($jsonArray)
or in javascript use json.stringify
var json = JSON.stringify($jsonArray)
in order to get the array to turn into json.
You can use d3.nest(), however this function won't give you the size as desired, even . You might want to format your data accordingly before sending it to the app.
If you want to create a custom function, then you can have a look at this answer and iterate on the keys of your object, as it is not even an array.