Can't get json data inside array - javascript

[
{
"May": [
{
"year": 1994,
"date": "2"
},
{
"Sequence": 2,
"Type": "Images"
}
],
"_id": "1122"
}
]
I can get id except "date" which is inside array. And date just show me undefined.

var data = [
{
"May": [
{
"year": 1994,
"date": "2"
},
{
"Sequence": 2,
"Type": "Images"
}
],
"_id": "1122"
}
];
alert(data[0].May[0]["date"]);
You should use arr[i].May[0]['date'], date is inside the object of the 0th index of array May.

Array will be start with the index 0 only . so from your json data 0th position only have the key named "date". So you should use like #void answer.
If you had doubt about whether data is have or not use like below
if(data[0].May[0]["date"])
alert(data[0].May[0]["date"]);
Thank you :)

Related

Remove special characters from a specific JSON object in JSON payload

I have a large JSON payload and I want to format, the specific object of the payload using JS.
I want flightdetails array object to edit and remove the special characters from it. How can I achieve this?
I have been working on this using XSLT for the past 2 days, and I went nowhere hence I decided to remove it using JS.
Example of the array(there can be more than 30 records inside the flightdetails)
"flightdetails": [
{
"id": XF-2092,
"trips": 2,
"categories": {
"flights": [
"\"return\",\"oneway\""
]
},
"id": XF-2093,
"trips": 1,
"categories": {
"flights": [
"\"return\""
]
}
}
]
Expected Output
"flightdetails": [
{
"id": XF-2092,
"trips": 2,
"categories": {
"flights": [
"return","oneway"
]
},
"id": XF-2093,
"trips": 1,
"categories": {
"flights": [
"return"
]
}
}
]
The flightdetails object inside the //destinations/flightdetails path

PouchDB Grouping and Summarizing

I am using pouchdb and have a document structure like this:
{
"_id": "1",
"title": "Title",
"categories": {
"category1": {
"salesx": [
{
"date": "2016-09-15 00:00:00",
"qty": "2"
},
{
"date": "2016-09-16 00:00:00",
"qty": "3"
}
],
"salesy": [
[
{
"date": "2016-09-15 00:00:00",
"qty": "1"
}
]
]
}
},
"_rev": "rev"
}
I now want to get something like this as a result from pouchdb:
{
"_id": "1",
"title": "Title",
"categories": {
"category1": {
"salesx": 2 + 3 = 5,
"salesy": [
[
{
"date": "2016-09-15 00:00:00",
"qty": "1"
}
]
]
}
},
"_rev": "rev"
}
So, salesx should be grouped and the qty should be summarized.
salesy should not be changed.
I also want to add a where clause to the date field.
I tried to start with a map function which selects the correct dates. But I don't know what to emit to group the qty later.
function myMapFunction(doc) {
doc.categories.forEach(category => {
category.salesx.forEach(salex => {
if (salex.date >= mydate) {
emit(?)
}
})
})
}
I can't find a good example on pouchdb docs. They only provide examples for grouping multiple documents together.
Does anyone know how I can achieve grouping the document?
I modified the documents like below, to convert the qty field from string values to number values, to be able to do math operations:
{
"_id": "sale0000",
"_rev": "2-5cd6fff48f9fd099481d3b523ff1191f",
"title": "Title",
"categories": {
"category1": {
"salesx": [
{
"date": "2016-09-15 00:00:00",
"qty": 2
},
{
"date": "2016-09-16 00:00:00",
"qty": 3
}
],
"salesy": [
[
{
"date": "2012-09-15 00:00:00",
"qty": 1
}
]
]
}
}
}
As you mentioned in the comments:
All documents look exactly like the one I already showed. Except the
number of entries in salesx and the number of categories (e.g.
category1, category2 ...) is modified
Based on your above statement, I created in my sample database some documents with varying number of entries in salesx and varying number of categoiry1, category2, etc., but each salesy has only one entry.
Here is another one of my sample documents:
{
"_id": "sale0002",
"_rev": "3-12bf5c872fc124c1f0a5a4a108cb4f7a",
"title": "Title",
"categories": {
"category1": {
"salesx": [
{
"date": "2016-09-15 00:00:00",
"qty": 2
},
{
"date": "2016-09-16 00:00:00",
"qty": 3
},
{
"date": "2017-10-16 00:00:00",
"qty": 9
}
],
"salesy": [
[
{
"date": "2014-09-15 00:00:00",
"qty": 1
}
]
]
},
"category2": {
"salesx": [
{
"date": "2018-01-15 00:00:00",
"qty": 2
},
{
"date": "2012-08-16 00:00:00",
"qty": 3
},
{
"date": "2015-10-16 00:00:00",
"qty": 7
}
],
"salesy": [
[
{
"date": "2011-03-15 00:00:00",
"qty": 1
}
]
]
}
}
}
I'm not sure if I understand what exactly you intend, but I developed the following view map function:
function (doc) {
if(doc.categories){
for(var categ in doc.categories){
var salesx_qty_sum=0;
for(var i=0, len=doc.categories[categ].salesx.length; i<len; i++){
salesx_qty_sum += doc.categories[categ].salesx[i].qty
}
emit(
doc.categories[categ].salesy[0][0]['date'], //emit the date of salesy as the key
//my understanding is that you need to index/sort
//according to date of salesy
[
salesx_qty_sum, //emit the sum of qty for salesx as value
doc.categories[categ].salesy[0][0]['qty'] //emit qty for salesy (unchanged) as value
]
);
}
}
}
In my sample database I created four documents like yours with different number of entries in salesx and also varying number of categories. There is only one entry inside each salesy:
$ curl -k -X GET 'https://admin:****#192.168.1.106:6984/sample/_all_docs'
{"total_rows":53,"offset":0,"rows":[
...
...
{"id":"_design/by_date","key":"_design/by_date","value":{"rev":"20-04cb881624aa819dac13f69c688f2124"}},
...
...
{"id":"sale0000","key":"sale0000","value":{"rev":"2-5cd6fff48f9fd099481d3b523ff1191f"}},
{"id":"sale0001","key":"sale0001","value":{"rev":"2-e8ee019fb0ad076b82d3f4772f736559"}},
{"id":"sale0002","key":"sale0002","value":{"rev":"3-12bf5c872fc124c1f0a5a4a108cb4f7a"}},
{"id":"sale0003","key":"sale0003","value":{"rev":"2-6d40368ddd16b9d02b908ea95d061c7e"}}
]}
The above view map (named by_date) is indexing my documents like below, in which the key is the date of salesy. Based your description, I assume there should be only one entry in each salesy in each category. Also, the value below is an array in which first element is sum of qty for salesx, and the second element is unchanged qty of salesy:
$ curl -k -X GET 'https://admin:admin#192.168.1.106:6984/sample/_design/by_date/_view/by_date'
{"total_rows":6,"offset":0,"rows":[
{"id":"sale0002","key":"2011-03-15 00:00:00","value":[12,1]},
{"id":"sale0000","key":"2012-09-15 00:00:00","value":[5,1]},
{"id":"sale0001","key":"2013-05-20 00:00:00","value":[12,1]},
{"id":"sale0002","key":"2014-09-15 00:00:00","value":[14,1]},
{"id":"sale0003","key":"2015-04-15 00:00:00","value":[5,1]},
{"id":"sale0003","key":"2018-04-05 00:00:00","value":[5,1]}
]}
You can query the by_date view with startkey and endkey to get the results between two dates, like this:
$ curl -k -X GET 'https://admin:admin#192.168.1.106:6984/sample/_design/by_date/_view/by_date?startkey="2013"&endkey="2015"'
{"total_rows":6,"offset":2,"rows":[
{"id":"sale0001","key":"2013-05-20 00:00:00","value":[12,1]},
{"id":"sale0002","key":"2014-09-15 00:00:00","value":[14,1]}
]}

How to sort array of objects

I need your help to understand how to sort array of objects with another array inside each of object. I have response from api with array bunch of objects like below:
[
{
"name": "[C US Equity] CITIGROUP INC",
"stats": [
{
"label": "DTD",
"value": null
},
{
"label": "MTD",
"value": null
},
{
"label": "YTD",
"value": 0.0536530913792681
},
{
"label": 2016,
"value": 0.18102519526139493
},
{
"label": 2015,
"value": -0.012946569977188238
},
{
"label": 2014,
"value": null
}
]
}...
]
I should be sort it by value of label "YTD" exclude of null value. Any help are welcome
You can use array#sort method
var oldArray = [{
"name": "[C US Equity] CITIGROUP INC",
"stats": [{
"label": "DTD",
"value": null
},
{
"label": "MTD",
"value": null
},
{
"label": "YTD",
"value": 0.0536530913792681
},
{
"label": 2016,
"value": 0.18102519526139493
},
{
"label": 2015,
"value": -0.012946569977188238
},
{
"label": 2014,
"value": null
}
]
}];
oldArray[0].stats.sort(function(a, b) {
return a.label - b.label;
})
console.log(oldArray)
You can use collection sort method provided by lodash. https://lodash.com/docs/4.17.4#sortBy
var users = [{
"name": "[C US Equity] CITIGROUP INC",
"stats": [
{
"label": "DTD",
"value": null
},
{
"label": "MTD",
"value": null
},
{
"label": "YTD",
"value": 0.0536530913792681
},
{
"label": null,
"value": 0.18102519526139493
},
{
"label": "2015",
"value": -0.012946569977188238
},
{
"label": "dtd",
"value": null
}
]
}]
console.log(_.sortBy(users[0].stats, [function(o) { if(o.label){return  o.label.toUpperCase();} }]));
This takes case of null values too.
I think this post may help you.
Take a look of it, it teaches you about how compare works.
All you need to do is to put that JSON thing into an object array,
then compare the value of YTD(make two for-loops, result [i] .stats [j] .label === 'YTD') to make the array to re-order it.
A comparison function is work like below(extracted from the above link)
function CompareNumbers( v1, v2 ) {
if ( v1 < v2 ) return -1; // v1 goes nearer to the top
if ( v1 > v2 ) return 1; // v1 goes nearer to the bottom
return 0; // both are the same
}
when it return -1(or sometimes false), the former one will be pushed to the front of your array, meanwhile 1 or true will push the former one to the back. And 0 (means not true or false, or you can handle the exception by yourself, push back or forward) will happen nothing.
But becare if your array exist two 'YTD' labelled value, if it happens it will make two value to appear in the same data.
This may help you in sorting the data while you cannot directly get a sorted data from your db server.
Oh, forgot to say, the compare function can compare the whole object.(of course you need to extract something like this ---> object[index].['Something your needed']), not only numbers.

Create collections through iterating a JSON file

I'm trying to make an event-based calendar by iterating through an array of json objects with individual dates using javascript.
What I want to achieve is a new structure from these objects, where i get the year from the object and make that a unique key which can contain multiple months as keys retrieved from the objects, which then again can contain multiple days as keys retrieved from the objects, which again contains multiple objects... phew..
F.ex., say I have this json file:
"objects": [
{
"dateTime": {
"date": "2016-12-24"
},
"values": {
"value1": "some value",
"value2": 700
}
}, {
"dateTime": {
"date": "2016-12-24"
},
"values": {
"value1": "other value",
"value2": 3100
}
}, {
"dateTime": {
"date": "2016-12-25"
},
"values": {
"value1": "random value",
"value2": 300
}
}, {
"dateTime": {
"date": "2017-01-02"
},
"values": {
"value1": "value",
"value2": 100
}
}]
Now I want to iterate through each object and make something like this:
[{
"2017": [{
"january": [{
"monday": [
object
]
}]
},
"2016": {
"december": [{
"saturday": [
object,
object
],
"sunday": [
object
]
}]
}]
}]
So I don't want any duplicates of any object which is set as key. What path should I go for? I've been looking into Map, WeakMap, Set and WeakSet, but I just can't figure out how to do this properly.

Angularjs ui.grid display nested rows

I have been trying to get nested json data inside columns. The json structure is:
[{
"ratings": [{
"rating": "Detractor",
"employees": [{
"empName": "Pavan",
"quarters": [{
"quarterName": "q1 2015",
"weeks": [{
"weekName": "week1",
"month": "January",
"points": [{
"0": "point1"
}, {
"1": "point2"
}]
}
, {
"weekName": "week2",
"month": "January",
"points": [{
"0": "point1"
}, {
"1": "point2"
}]
}, {
"weekName": "week3",
"month": "January",
"points": [{
"0": "point1"
}, {
"1": "point2"
}]
}
]
}, {
"quarterName": "q2 2015",
"weeks": [{
"weekName": "week4",
"month": "January",
"points": [{
"0": "point1"
}, {
"1": "point2"
}]
}
, {
"weekName": "week5",
"month": "January",
"points": [{
"0": "point1"
}, {
"1": "point2"
}]
}, {
"weekName": "week6",
"month": "January",
"points": [{
"0": "point1"
}, {
"1": "point2"
}]
}
]
}]
}]
}
]
}]
The output should have rating in one column, the corresponding employees in the next and the points in the third column.
I found a solution here, but it deals with only one level and I could not extend it to multiple levels.
I need to display weekly data.
Expected Output
There is no problem accessing nested objects in ui-grid, generally speaking.
In your case the problem seems to be more related to the fact that you have a dynamic structure.
In ui-grid you can assign a value to a column with a string containing the full path for that value, for example:
Suppose you assing ratings sub-array to gridOptions.data, the value of field for week1, week2 and week4 would respectively be:
'employees[0].quarters[0].weeks[0].points[0]'
'employees[0].quarters[0].weeks[1].points[0]'
'employees[0].quarters[1].weeks[0].points[0]'
Now, add to it that the number of weeks and quarters is not fixed and you will find that there is no direct way to programmatically relate the number of the week to the index needed for weeks and quarters array.
I think the best way to handle this would be iterate through the original data you receive and build a new JSON more suited for the grid.
In pseudo code you could do something like this:
initialize results array
iterate through ratings
create new empty object (current_object)
add current_object to results array
iterate through employees
iterate through quarters
iterate through weeks
add value for i-th week as week_i to current_object

Categories

Resources