export json to xls doc in java/ js - javascript

I'm trying to export json data into an Excel file. My json is not consistent which is why I have problem because the excel mapping is all scattered and the columns are messed up. Here is a sample JSON I'm working on:
[
{
"path": "/conte/nt",
"title": "First",
"date": "January 14, 2015 8:59:57 PM EST"
},
{
"path": "/content/o",
"title": "Third Annual",
"date": "January 14, 2015 8:59:57 PM EST"
},
{
"path": "/co/en",
"title": "Motivating Factors",
"date": "January 14, 2015 8:59:57 PM EST"
},
{
"path": "/con/ent",
"title": "Meeting",
"description": "test",
"date": "January 14, 2015 8:59:57 PM EST",
"key": "xyz, abc"
}
]

Related

JSON manipulation - count keys with same value, create new object with count as value

I want to count the JSON nodes having the same date, and create a new object with the date as the key and the count as the value.
So, using either jQuery or vanilla JS, how would I turn this:
[
{
"date": "January 1"
},
{
"date": "January 1"
},
{
"date": "January 1"
},
{
"date": "January 1"
},
{
"date": "February 14"
},
{
"date": "February 14"
},
{
"date": "July 8"
},
{
"date": "July 8"
},
{
"date": "July 8"
},
{
"date": "December 11"
}
]
Into this:
[
{
"January 1": 4 // Number or String "4", doesn't matter
},
{
"February 14": 2
},
{
"July 8": 3
},
{
"December 11": 1
}
]
Try something like this.
const data = [
{
"date": "January 1"
},
{
"date": "January 1"
},
{
"date": "January 1"
},
{
"date": "January 1"
},
{
"date": "February 14"
},
{
"date": "February 14"
},
{
"date": "July 8"
},
{
"date": "July 8"
},
{
"date": "July 8"
},
{
"date": "December 11"
}
]
let counter = {}
data.forEach(function(obj) {
var key = obj.date
counter[key] = (counter[key] || 0) + 1
})
console.log(counter)

Nested ngFor only returns last item on inner ngFor

I am trying to loop through firebase RTDB reference to retrieve a list and then use those results in a subsequent firestore query. the console logs the correct data but my code only displays the last item in the loop in the inside ngFor. Any help would be appreciated. Thanks
this.ref = firebase.database().ref('stations/');
this.ref.on('value', resp => {
this.infos = [];
this.infos = snapshotToArray(resp);
});
const snapshotToArray = snapshot => {
let returnArr = [];
snapshot.forEach(childSnapshot => {
let item = childSnapshot.val();
item.key = childSnapshot.key;
returnArr.push(item.StationtName);
let sku = item.StationtName;
this.queues = this.afs.collection('projects', ref => ref.where('station', '==', item.StationtName)).valueChanges()
});
return returnArr;
}
html:
<div *ngFor="let info of infos">
{{info}}
<div *ngFor="let queue of queues | async">
<ion-button> {{queue.SKU}}</ion-button>
</div>
</div>
json and new error:
ERROR Error: Cannot find a differ supporting object '[
{
"SKU": "908897",
"buyerEmail": "m",
"comments": "c",
"createdAt": 1550285024451,
"queue": 1,
"station": "The Warehouse",
"timeTest": "Fri Feb 15 2019",
"type": "Project"
}
]' of type 'string'. NgFor only supports binding to Iterables such as Arrays.
this.infos:
["Lathe", "Press 1", "Press 2", "Mill 1", "Mill 2", "Ready", "Plasma", "Grinding", "Grinding", "Flange shop", "the office", "The Warehouse"]
added an .subscribe() to this.queues and got the correct info logged to the console on every loop. The associated projects for each station are printed to the console. Here is one of them;
["09987", "66553", "3", "98774", "4", "987654321", "1", "6533343434343"]
these are all SKU's that are associated with each station.
Here is the subscribe code I added to get the value of the observable, now Im getting another error though:
.subscribe(data=>{
console.log(data);
this.items = data.map(function (obj) {
return obj.SKU;
});
console.log(this.items);
// this.stationArr.push(this.item);
})
The error I'm getting now is:
Error: InvalidPipeArgument: '[object Object]' for pipe 'AsyncPipe'
And this in the the chrome inspector:
<!--bindings={
"ng-reflect-ng-for-of": null
}-->
current output:
Lathe
----- 908897
Press 1
----- 908897
Press 2
----- 908897
Mill 1
----- 908897
Mill 2
----- 908897
Ready
----- 908897
Plasma
----- 908897
Grinding
----- 908897
Grinding
----- 908897
Flange shop
----- 908897
the office
----- 908897
The Warehouse
----- 908897
desired output:
Lathe
----- 908897
Press 1
----- 123432,5476344
Press 2
----- empty
Mill 1
----- 0987654, 7777777, 673654
Mill 2
----- 12321
Ready
----- 909090990, 878787878, 67767
etc.
each station has a SKU associated, but my code displays the same one for each station.
json file:
[{
"id": "0rikFyAEt4Zg83sPeUNG",
"SKU": "88",
"buyerEmail": "",
"comments": "",
"createdAt": 1550297319125,
"queue": 1,
"station": "Press 1",
"timeTest": "Sat Feb 16 2019",
"type": "Project"
}, {
"id": "1UJftXBqdmvjwJAs2XO5",
"SKU": "77777e7e7",
"buyerEmail": "b",
"comments": "c",
"createdAt": 1550291447696,
"queue": 1,
"station": "Lathe",
"timeTest": "Fri Feb 15 2019",
"type": "Project"
}, {
"id": "1aFkV5z0JBzfii0edQCd",
"SKU": "911111111",
"buyerEmail": "matt#duhon.com",
"comments": "comment",
"createdAt": 1550286952568,
"queue": 1,
"station": "Grinding",
"stations": ["Addy"],
"timeTest": "Fri Feb 15 2019",
"type": "Project"
}, {
"id": "5BylWSmtH02v8oCq7T2B",
"SKU": "123456789",
"buyerEmail": "MAtts",
"comments": "comment",
"createdAt": 1550372243065,
"queue": 1,
"station": "Press 1",
"timeTest": "Sat Feb 16 2019",
"type": "Project"
}, {
"id": "BVNaMjK5oqSslnfE6Uar",
"SKU": "11111111",
"buyerEmail": "",
"comments": "",
"createdAt": 1550290392880,
"queue": 1,
"station": "Mill",
"timeTest": "Fri Feb 15 2019",
"type": "Project"
}, {
"id": "HvxQF4suTBaiCwpWgf9D",
"SKU": "908897",
"buyerEmail": "m",
"comments": "c",
"createdAt": 1550285024451,
"queue": 1,
"station": "The Warehouse",
"timeTest": "Fri Feb 15 2019",
"type": "Project"
}, {
"id": "KfJ4qvVyDzVwFxIeMRzM",
"SKU": "09987",
"buyerEmail": "",
"comments": "",
"createdAt": 1550286034246,
"queue": 1,
"station": "Ready",
"timeTest": "Fri Feb 15 2019",
"type": "Project"
}, {
"id": "MrrioE5IHpImz52qnHW5",
"SKU": "1211221",
"buyerEmail": "",
"comments": "",
"createdAt": 1550290371569,
"queue": 1,
"station": "Lathe",
"timeTest": "Fri Feb 15 2019",
"type": "Project"
}, {
"id": "TczmkayGuWonT6SQZf6F",
"SKU": "3",
"buyerEmail": "",
"comments": "3 years old yo!",
"createdAt": 1550298020154,
"queue": 1,
"station": "Ready",
"timeTest": "Sat Feb 16 2019",
"type": "Project"
}, {
"id": "ZspOz31o0uYT0Msull5J",
"SKU": "909987",
"buyerEmail": "matt#duhon.com",
"comments": "comment",
"createdAt": 1550286926441,
"queue": 1,
"station": "Packaging",
"timeTest": "Fri Feb 15 2019",
"type": "Project"
}, {
"id": "hXC1uswgbW1UVTfVD96k",
"SKU": "98774",
"buyerEmail": "matt#duhon.com",
"comments": "comments",
"createdAt": 1550296878271,
"queue": 1,
"station": "Ready",
"timeTest": "Sat Feb 16 2019",
"type": "Project"
}, {
"id": "jeR8wCiPdCwObZvajT04",
"SKU": "4",
"buyerEmail": "matt",
"comments": "comments",
"createdAt": 1550298181612,
"queue": 1,
"station": "Ready",
"timeTest": "Sat Feb 16 2019",
"type": "Project"
}, {
"id": "lx2Hh3rpQ1rLkGHAgEzo",
"SKU": "987654321",
"buyerEmail": "yomama",
"comments": "com mints",
"createdAt": 1550296669267,
"queue": 1,
"station": "Ready",
"timeTest": "Fri Feb 15 2019",
"type": "Project"
}, {
"id": "qAYSdYYC2wxBwYDYI71T",
"SKU": "1",
"buyerEmail": "",
"comments": "",
"createdAt": 1550297026128,
"queue": 1,
"station": "Ready",
"timeTest": "Sat Feb 16 2019",
"type": "Project"
}, {
"id": "sBXhW6lPaYtUn4fz5Gsf",
"SKU": "6533343434343",
"buyerEmail": "MAtt",
"comments": "Comment",
"createdAt": 1550372086063,
"queue": 1,
"station": "Ready",
"timeTest": "Sat Feb 16 2019",
"type": "Project"
}]
As per you code check this Demo code where you can see the nested *ngFor
<ul>
<li *ngFor="let info of infos">{{info}}
<ol *ngFor="let queue of queues | async">{{queue.SKU}}</ol>
</li>
</ul>

Deleting an entry from a JSON array [duplicate]

This question already has answers here:
remove objects from array by object property
(15 answers)
Closed 4 years ago.
How can I delete a specific element (eg: using its id) from a JSON array?
A delete button will perform this task, passing its id.
For example, how to delete item with id = 2 :
Note: id number 2 can be in an array index of 10 or 20.
"id": 2, "date": "27 Mar 2018", "name": "name 2", "type":
"meeting"
var id = 1;
var events = [{
"id": id++,
"date": "26 Mar 2018",
"name": "name 1",
"type": "task"
},
{
"id": id++,
"date": "27 Mar 2018",
"name": "name 2",
"type": "meeting"
},
{
"id": id++,
"date": "27 Mar 2018",
"name": "name 3",
"type": "meeting"
}
];
events.push({
"id": id++,
"date": "25 Mar 2018",
"name": "name 4",
"type": "meeting"
});
You can do like below
var events = [{
"id": 1,
"date": "26 Mar 2018",
"name": "name 1",
"type": "task"
},
{
"id": 10,
"date": "27 Mar 2018",
"name": "name 2",
"type": "meeting"
},
{
"id": 15,
"date": "27 Mar 2018",
"name": "name 3",
"type": "meeting"
}
];
let entry_to_delete=events.find(e => e.id === 10);
events.splice(events.indexOf(entry_to_delete),1);
console.log(events);

showing File System Structure using JSON in angularJS

I need a JSON array which contains a sample file system with corresponding attributes(type, name, size, created, modified, last_accessed etc.). Then I want to plot it as a web file system in a tree structure.
So, how will my JSON be look like & how should I make that tree in angularJS?
Editions:
I made JSON as -
{
"data" : {
"path":[
"My Files",
"Documents"
],
"data" : [
{
"name": "Work Documents",
"type": "folder",
"owner": "me",
"size": "",
"modified": "May 24, 2017",
"opened": "May 22, 2017",
"created": "May 22, 2017",
"extention": "",
"location": "My Files > Documents",
"offline": true,
"children" : [
{
"name": "Public Documents",
"type": "folder",
"owner": "public",
"size": "",
"modified": "July 8, 2015",
"opened": "July 8, 2015",
"created": "July 8, 2015",
"extention": "",
"location": "My Files > Documents",
"offline": true
},
{
"name": "Ongoing projects",
"type": "document",
"owner": "Emily Bennett",
"size": "1.2 Mb",
"modified": "July 8, 2015",
"opened": "July 8, 2015",
"created": "July 8, 2015",
"extention": "",
"location": "My Files > Documents",
"offline": true,
"preview": "assets/images/etc/sample-file-preview.jpg"
},
{
"name": "Shopping list",
"type": "document",
"owner": "Emily Bennett",
"size": "980 Kb",
"modified": "July 8, 2015",
"opened": "July 8, 2015",
"created": "July 8, 2015",
"extention": "",
"location": "My Files > Documents",
"offline": true,
"preview": "assets/images/etc/sample-file-preview.jpg"
}
]
},
{
"name": "Private Documents",
"type": "folder",
"owner": "me",
"size": "",
"modified": "July 8, 2015",
"opened": "July 8, 2015",
"created": "July 8, 2015",
"extention": "",
"location": "My Files > Documents",
"offline": true
}
]
}
}
But how can I iterate it in html ? I'm currently displaying it, but unable to understand how to show 'Children' on click of folder. This is my html -
<tr ng-repeat="file in vm.data | filter:global.search" ng-click="vm.select(file)" ng-class="{'selected' : vm.selected === file}">
<td class="file-icon">
<i class="icon-{{file.type}}"></i>
</td>
<td class="name">{{file.name}}</td>
<td class="type" hide show-gt-sm>{{file.type}}</td>
<td class="owner" hide-xs>{{file.owner}}</td>
<td class="size" hide-xs>{{file.size === '' ? '-': file.size}}</td>
<td class="last-modified" hide show-gt-md>{{file.modified}}</td>
<td class="show-details" hide-gt-md>
<md-button class="md-icon-button sidenav-toggle" ng-click="vm.toggleDetails(file)" aria-label="Details" translate translate-attr-aria-label="FM.DETAILS">
<md-icon md-font-icon="icon-information-outline"></md-icon>
</md-button>
</td>
</tr>
You can create a Json like this if it fullfil your requirements. Suppose you have a root dir of name root and it has child dirs books and videos.
Var root = {
dirName: "root",
metadata: {
size: " 1 gb",
type: "dir",
last_modified: " 24 may 2017",
// Other attributes
},
children: [
{
dirName: "books",
metadata: {
size: " 10 mb",
type: "dir",
last_modified: " 24 may 2017",
// Other attributes
},
Children:{}
},
{
dirName: "videos",
metadata: {
size: " 10 mb",
type: "dir",
last_modified: " 24 may 2017",
// Other attributes
}
}
]
}
You can now modify it as per your needs
I used this to display tree structure - https://github.com/eu81273/angular.treeview

What are the lodash combinations to achieve the below scenario?

I get below data from twitter in which you can see it has multilevel array in it.I need a single array which has all the objects in it.You can find below how i need the data to be shown.
[
[{
"created_at": "Tue May 19 04:36:36 +0000 2015",
"id": "asdfasdf",
"id_str": "ASdfasdfasdf"
}, {
"created_at": "Tue May 19 04:36:36 +0000 2015",
"id": "asdfasdf",
"id_str": "ASdfasdfasdf"
}],
[{
"created_at": "Tue May 19 04:36:36 +0000 2015",
"id": "asdfasdf",
"id_str": "ASdfasdfasdf"
}, {
"created_at": "Tue May 19 04:36:36 +0000 2015",
"id": "asdfasdf",
"id_str": "ASdfasdfasdf"
}],
[{
"created_at": "Tue May 19 04:36:36 +0000 2015",
"id": "asdfasdf",
"id_str": "ASdfasdfasdf"
}, {
"created_at": "Tue May 19 04:36:36 +0000 2015",
"id": "asdfasdf",
"id_str": "ASdfasdfasdf"
}]
]
but I need to loop only single array like this having all the data combined in single array.
[{
"created_at": "Tue May 19 04:36:36 +0000 2015",
"id": "asdfasdf",
"id_str": "ASdfasdfasdf"
}, {
"created_at": "Tue May 19 04:36:36 +0000 2015",
"id": "asdfasdf",
"id_str": "ASdfasdfasdf"
{
"created_at": "Tue May 19 04:36:36 +0000 2015",
"id": "asdfasdf",
"id_str": "ASdfasdfasdf"
}, {
"created_at": "Tue May 19 04:36:36 +0000 2015",
"id": "asdfasdf",
"id_str": "ASdfasdfasdf"
},
{
"created_at": "Tue May 19 04:36:36 +0000 2015",
"id": "asdfasdf",
"id_str": "ASdfasdfasdf"
}, {
"created_at": "Tue May 19 04:36:36 +0000 2015",
"id": "asdfasdf",
"id_str": "ASdfasdfasdf"
}]
Use _.flatten(arr, isDeep)
Flattens a nested array. If isDeep is true the array is recursively flattened, otherwise it’s only flattened a single level.
var flattenedArr = _.flatten(arr);
If your array is nested then use the true as second argument for deep flatten.
var flattenedArr = _.flatten(arr, true);
Demo
var arr = [
[{
"created_at": "Tue May 19 04:36:36 +0000 2015",
"id": "asdfasdf",
"id_str": "ASdfasdfasdf"
}, {
"created_at": "Tue May 19 04:36:36 +0000 2015",
"id": "asdfasdf",
"id_str": "ASdfasdfasdf"
}],
[{
"created_at": "Tue May 19 04:36:36 +0000 2015",
"id": "asdfasdf",
"id_str": "ASdfasdfasdf"
}, {
"created_at": "Tue May 19 04:36:36 +0000 2015",
"id": "asdfasdf",
"id_str": "ASdfasdfasdf"
}],
[{
"created_at": "Tue May 19 04:36:36 +0000 2015",
"id": "asdfasdf",
"id_str": "ASdfasdfasdf"
}, {
"created_at": "Tue May 19 04:36:36 +0000 2015",
"id": "asdfasdf",
"id_str": "ASdfasdfasdf"
}]
];
console.log(_.flatten(arr));
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/3.10.1/lodash.js"></script>

Categories