var Employees = [
{
"id": "382740",
"PayrollID": "8117817425",
"EmployeeName": "Bob Jones",
"StartTime": "15:15:00.0000000",
"FinishTime": "18:15:00.0000000",
"BreakTime": "45",
"TotalTime": 2,
"Comments": "Test",
"Rate": "19"
},
{
"id": "439617",
"PayrollID": "8117817425",
"EmployeeName": "Peter Pan",
"StartTime": "16:15:00.0000000",
"FinishTime": "21:15:00.0000000",
"BreakTime": "60",
"TotalTime": 4,
"Comments": "Test",
"Rate": "32"
},
{
"id": "201636",
"PayrollID": "5042289623",
"EmployeeName": "Bob Jones",
"StartTime": "09:56:00.0000000",
"FinishTime": "11:56:00.0000000",
"BreakTime": "45",
"TotalTime": 1.25,
"Comments": "Test Comments",
"Rate": "19"
},
{
"id": "799653",
"PayrollID": "5042289623",
"EmployeeName": "Clarke Kent",
"StartTime": "16:49:00.0000000",
"FinishTime": "21:49:00.0000000",
"BreakTime": "60",
"TotalTime": 4,
"Comments": "Test",
"Rate": "19"
},
{
"id": "951567",
"PayrollID": "5042289623",
"EmployeeName": "Bob Jones",
"StartTime": "01:49:00.0000000",
"FinishTime": "16:49:00.0000000",
"BreakTime": "60",
"TotalTime": 14,
"Comments": "Test",
"Rate": "10"
}
]
I have the above array and I want to sum the TotalTime where the EmployeeName is the same. It should return a new array with like entries combined and the TotalTime added. I've used the below but it only returns two values because of Map. Is there a way I can achieve this while still maintaining all the values in the original array?
const CombinedArray = Array.from(Employees.reduce(
(m, {EmployeeName, TotalTime}) => m.set(EmployeeName, (m.get(EmployeeName) || 0) + TotalTime,), new Map
), ([EmployeeName, TotalTime]) => ({EmployeeName, TotalTime}));
In the Map, set the value not to the cumulative total time for the employee so far, but to a whole employee object that contains the total time inside it. Spread the first object found so as not to mutate the input.
var Employees=[{id:"382740",PayrollID:"8117817425",EmployeeName:"Bob Jones",StartTime:"15:15:00.0000000",FinishTime:"18:15:00.0000000",BreakTime:"45",TotalTime:2,Comments:"Test",Rate:"19"},{id:"439617",PayrollID:"8117817425",EmployeeName:"Peter Pan",StartTime:"16:15:00.0000000",FinishTime:"21:15:00.0000000",BreakTime:"60",TotalTime:4,Comments:"Test",Rate:"32"},{id:"201636",PayrollID:"5042289623",EmployeeName:"Bob Jones",StartTime:"09:56:00.0000000",FinishTime:"11:56:00.0000000",BreakTime:"45",TotalTime:1.25,Comments:"Test Comments",Rate:"19"},{id:"799653",PayrollID:"5042289623",EmployeeName:"Clarke Kent",StartTime:"16:49:00.0000000",FinishTime:"21:49:00.0000000",BreakTime:"60",TotalTime:4,Comments:"Test",Rate:"19"},{id:"951567",PayrollID:"5042289623",EmployeeName:"Bob Jones",StartTime:"01:49:00.0000000",FinishTime:"16:49:00.0000000",BreakTime:"60",TotalTime:14,Comments:"Test",Rate:"10"}];
const m = new Map();
for (const emp of Employees) {
if (!m.has(emp.EmployeeName)) m.set(emp.EmployeeName, { ...emp });
else m.get(emp.EmployeeName).TotalTime += emp.TotalTime;
}
console.log([...m.values()]);
Related
This question already has an answer here:
How to merge two list of objects in a new one by date
(1 answer)
Closed 8 months ago.
I'm learning Javascript and I'm stuck in a doubt with the joining of two arrays of objects through an ID, I can join the two, but the result is not what I expect.
So, I have these two object arrays:
"product": [
{
"id": "1000",
"code": "f230fh0g3",
"name": "Bamboo Watch",
"description": "Product Description",
"image": "bamboo-watch.jpg",
"price": 65,
"category": "Accessories",
"quantity": 24,
}
]
"orders": [
{
"id": "1000",
"productID": "f230fh0g3",
"date": "2020-09-13",
"amount": 65,
"quantity": 1,
},
{
"id": "1000",
"productID": "f230fh0g3",
"date": "2020-09-13",
"amount": 65,
"quantity": 1,
},
]
and I want to join both by key (id) to get one array like this one:
"product": [
{
"id": "1000",
"code": "f230fh0g3",
"name": "Bamboo Watch",
"description": "Product Description",
"image": "bamboo-watch.jpg",
"price": 65,
"category": "Accessories",
"quantity": 24,
"orders": [
{
"id": "1000",
"productCode": "f230fh0g3",
"date": "2020-09-13",
"amount": 65,
"quantity": 1,
"customer": "David James",
"status": "PENDING"
},
{
"id": "1001",
"productCode": "f230fh0g3",
"date": "2020-05-14",
"amount": 130,
"quantity": 2,
"customer": "Leon Rodrigues",
"status": "DELIVERED"
},
]
},
{
"id": "1001",
"..."
"orders": [
{
"id": "1001",
"..."
}
]
}]
Is it possible to map these arrays in this way?
Thanks in advance
Yes, it is possible. You can use .map() and .filter() together. Assuming you have products in product variable and orders in order variable, you can compute your combined list like this:
const result = products.map(
product => ({
...product,
orders: orders.filter(
({ productID }) => productID === product.id
)
})
);
I am working on NuxtJs and i have to return a list array from a column of json data when and if matches to a value of another columns
The below code returns all unique value names from the column. But i want to get unique value names when the album name matches to "BAD" / "Thriller" alone
getNameList(){
var lookup = {};
var items = this.testdata.songs;
var result_name = [];
for (var item, i = 0; item = items[i++];) {
var unique_name = item.name;
if (!(unique_name in lookup)) {
lookup[unique_name] = 1;
result_name.push(unique_name);
}
}
return result_name
}
Please help me with some codes
json data file name "testdata.json"
{
"songs": [
{
"id": "1",
"name": "Thriller",
"album": "Thriller"
},
{
"id": "2",
"name": "Smooth Criminal",
"album": "BAD"
},
{
"id": "3",
"name": "Thriller",
"album": "Thriller"
},
{
"id": "4",
"name": "Smooth Criminal",
"album": "BAD"
},
{
"id": "5",
"name": "BAD",
"album": "BAD"
},
{
"id": "6",
"name": "Billy Jean",
"album": "Thriller"
},
{
"id": "7",
"name": "BAD",
"album": "BAD"
},
{
"id": "8",
"name": "Smooth Criminal",
"album": "BAD"
},
{
"id": "9",
"name": "BAD",
"album": "BAD"
},
{
"id": "10",
"name": "Billy Jean",
"album": "Thriller"
},
{
"id": "11",
"name": "Smooth Criminal",
"album": "BAD"
},
{
"id": "12",
"name": "Beat It",
"album": "Thriller"
},
{
"id": "13",
"name": "Billy Jean",
"album": "Thriller"
},
]
}
I am new to Javascript & nuxtjs. I tried with several if statements between the above script code, but nothing works to me.
You can filter the array by album name and use Set to get unique array value.
function getNameList(testdata){
const filteredArray = testdata.songs
.filter(item => item.album === 'BAD' || item.album === 'Thriller');
return [...new Set(filteredArray.map(item => item.name))];
}
const a = {"songs":[{"id":"1","name":"Thriller","album":"Thriller"},{"id":"2","name":"Smooth Criminal","album":"BAD"},{"id":"3","name":"Thriller","album":"Thriller"},{"id":"4","name":"Smooth Criminal","album":"BAD"},{"id":"5","name":"BAD","album":"BAD"},{"id":"6","name":"Billy Jean","album":"Thriller"},{"id":"7","name":"BAD","album":"BAD"},{"id":"8","name":"Smooth Criminal","album":"BAD"},{"id":"9","name":"BAD","album":"BAD"},{"id":"10","name":"Billy Jean","album":"Thriller"},{"id":"11","name":"Smooth Criminal","album":"BAD"},{"id":"12","name":"Beat It","album":"Thriller"},{"id":"13","name":"Billy Jean","album":"Thriller"}]};
console.log(getNameList(a));
I Want to join two arrays into one array containing two different arrays
First Array. I do not mean a simple array but this time around a more complex array with field having the same values on both sides of the arrays.
var arr1 = [{
"id": "4",
"ip_address": "127.0.0.1",
"username": "superuser",
"password": "$2y$08$awherOdjNPRoDHAiNBGZNuA92UGfT7jsIpsMMcNnyyJMxBA8Ug9q6",
"salt": null,
"email": "super#admin.com",
"activation_code": null,
"forgotten_password_code": "NULL",
"forgotten_password_time": null,
"remember_code": "cBjcajHj8qXaNrOhkAAqPe",
"created_on": "2018-09-13",
"last_login": "1540549332",
"active": "1",
"first_name": "Super",
"last_name": "Admin",
"phone": "0",
"user_id": "4",
"groups": [{
"id": "10",
"name": "superusers",
"description": "Super Administrators",
"$$hashKey": "object:38"
}],
"$$hashKey": "object:11"
}];
var arr2 = [{
"id": "1",
"ip_address": "127.0.0.1",
"username": "administrator",
"password": "$2y$08$DoULTzDyGFyh.DTNOvxRtujA3CT2yVBMpp6joYnfUcD0FQgbm9rmy",
"salt": "",
"email": "admin#admin.com",
"activation_code": "",
"forgotten_password_code": null,
"forgotten_password_time": null,
"remember_code": "wYiqzg7AM2QbEPdVrqUhkO",
"created_on": "2010-03-18",
"last_login": "1537468397",
"active": "1",
"first_name": "Admin",
"last_name": "istrator",
"phone": "0",
"user_id": "1",
"groups": [{
"id": "3",
"name": "admins",
"description": "Administrators",
"$$hashKey": "object:32"
}],
"$$hashKey": "object:8"
}];
let's say you declared your arrays as arr1 and arr2. To merge them:
var $users = arr1.concat(arr2);
If you want $users to be an array with two elements, and each of one being an array, you would do
var $users = [arr1, arr2];
But that doesn't match your desired result and it would make little sense.
var a = [10, 20];
var b = [30, 40, 50];
Array.prototype.push.apply(a,b);
console.log(a);
var arr1 = [{
"id": "4",
"ip_address": "127.0.0.1",
"username": "superuser",
"password": "$2y$08$awherOdjNPRoDHAiNBGZNuA92UGfT7jsIpsMMcNnyyJMxBA8Ug9q6",
"salt": null,
"email": "super#admin.com",
"activation_code": null,
"forgotten_password_code": "NULL",
"forgotten_password_time": null,
"remember_code": "cBjcajHj8qXaNrOhkAAqPe",
"created_on": "2018-09-13",
"last_login": "1540549332",
"active": "1",
"first_name": "Super",
"last_name": "Admin",
"phone": "0",
"user_id": "4",
"groups": [{
"id": "10",
"name": "superusers",
"description": "Super Administrators",
"$$hashKey": "object:38"
}],
"$$hashKey": "object:11"
}];
var arr2 = [{
"id": "1",
"ip_address": "127.0.0.1",
"username": "administrator",
"password": "$2y$08$DoULTzDyGFyh.DTNOvxRtujA3CT2yVBMpp6joYnfUcD0FQgbm9rmy",
"salt": "",
"email": "admin#admin.com",
"activation_code": "",
"forgotten_password_code": null,
"forgotten_password_time": null,
"remember_code": "wYiqzg7AM2QbEPdVrqUhkO",
"created_on": "2010-03-18",
"last_login": "1537468397",
"active": "1",
"first_name": "Admin",
"last_name": "istrator",
"phone": "0",
"user_id": "1",
"groups": [{
"id": "3",
"name": "admins",
"description": "Administrators",
"$$hashKey": "object:32"
}],
"$$hashKey": "object:8"
}];
let $users = arr1.concat(arr2);
console.log($users);
Concatenate both arrays using concat function
const arr1 = [/\*values\*/];
const arr2 = [/\*values\*/];
// As in description:
const $users = arr1.concat(arr2); //[firstValues, secondValues]
// As in title:
const $users = [arr1, arr2]; //[[firstValues], [secondValues]]
I am new to javascript. i want to parse this response .
var date=[];
var details=[];
for(post in resulttable.posts){
date=date.concat(resulttable.posts[post].days.Date);
details= details.concat(resulttable.posts[post].days.details);
}
I dont know where am missing. please help me , I want those details in one array and dates to be another array.
{
"status": 1,
"count": 2,
"posts": [{
"days": {
"details": [{
"place": "labs",
"StartTime": "01:00:00",
"EndTime": "02:00:00",
"Description": "Meeting with team",
"participants": [{
"Name": "KK",
"Designation": "VP, Operations",
"ContactNumber": "111"
}, {
"Name": "MN1",
"Designation": "Project Lead",
"ContactNumber": "111"
}]
}],
"Date": ["2017-02-02"]
},
"name": "test"
}, {
"days": {
"details": [{
"place": "India",
"StartTime": "01:00:00",
"EndTime": "03:00:00",
"Description": "Agenda1",
"participants": [{
"Name": "Kk",
"Designation": "VP, Operations",
"ContactNumber": "11111"
}, {
"Name": "MN",
"Designation": "Project Lead",
"ContactNumber": "111"
}]
}, {
"place": "microsoft",
"StartTime": "01:00:00",
"EndTime": "02:00:00",
"Description": "Meet CEO",
"participants": [{
"Name": "VR",
"Designation": "Project Lead",
"ContactNumber": "111"
}]
}, {
"place": "microsoft",
"StartTime": "01:00:00",
"EndTime": "02:00:00",
"Description": "Meet CEO",
"participants": [{
"Name": " VR",
"Designation": "Project Lead",
"ContactNumber": "111"
}]
}, {
"place": "Formule",
"StartTime": "10:50:00",
"EndTime": "11:50:00",
"Description": "Meet Rajesh",
"participants": [{
"Name": "MN",
"Designation": "Project Lead",
"ContactNumber": "111"
}]
}, {
"place": "Dell",
"StartTime": "04:00:00",
"EndTime": "08:00:00",
"Description": "Agenda 2",
"participants": [{
"Name": "MN",
"Designation": "Project Lead",
"ContactNumber": "1111111"
}]
}],
"Date": ["2017-02-03"]
},
"name": "test"
}]
}
Check this fiddle
var details = new Array();
var dates = new Array();
for (var i = 0; i < resulttable.posts.length; i++) {
dates = dates.concat(resulttable.posts[i].days.Date);
details = details.concat(resulttable.posts[i].days.details);
}
console.log(details);
console.log(dates);
Or
var details = new Array();
var dates = new Array();
for (post of resulttable.posts) {
dates = dates.concat(post.days.Date);
details = details.concat(post.days.details);
}
console.log(details);
console.log(dates);
I have a situation where I'm getting list of complex JSON data(nested type).I'm new to AngularJS, not getting solution to run ng-repeat or forEach over that.
My returned data is like below.
[{
"studPersonalDetails": {
"id": 0,
"name": "Digvijay",
"middleName": "",
"lastName": "Singh",
"fatherName": "abac",
"motherName": "abc",
"dob": "5/7/1990 12:00:00 AM"
},
"clients": {
"clientID": 1,
"clientName": null,
"clientDescriptions": null
},
"studentAddress": {
"address1": "12",
"address2": "12",
"city": "21",
"state": "212",
"pin": 2
},
"courseDetails": {
"courseID": 12,
"courseName": "12",
"courseDetail": null
},
"studentContacts": {
"email": "12",
"alternatePhone": "12",
"phone": "qw"
}
}, {
"studPersonalDetails": {
"id": 0,
"name": "Anil",
"middleName": "kumar",
"lastName": "Sharma",
"fatherName": "bac",
"motherName": "bac",
"dob": "2/11/1989 12:00:00 AM"
},
"clients": {
"clientID": 1,
"clientName": null,
"clientDescriptions": null
},
"studentAddress": {
"address1": "21",
"address2": "21",
"city": "5456",
"state": "8",
"pin": 7
},
"courseDetails": {
"courseID": 58,
"courseName": "58",
"courseDetail": null
},
"studentContacts": {
"email": "12",
"alternatePhone": "12",
"phone": "abc"
}
}]
This is the response returned by API.
Any help would be appreciated!Thanks!!
You can try something like this:
JSFiddle.
<div ng-repeat="currentRow in data">
<span>{{currentRow.studPersonalDetails.name}}</span>
<span>{{currentRow.studPersonalDetails.lastName}}</span>
</div>
$scope.arr = [{
"studPersonalDetails": {
"id": 0,
"name": "Digvijay",
"middleName": "",
"lastName": "Singh",
"fatherName": "Shyam Bahadur Singh",
"motherName": "ramawati Devi",
"dob": "5/7/1990 12:00:00 AM"
},
"clients": {
"clientID": 1,
"clientName": null,
"clientDescriptions": null
},
"studentAddress": {
"address1": "12",
"address2": "12",
"city": "21",
"state": "212",
"pin": 2
},
"courseDetails": {
"courseID": 12,
"courseName": "12",
"courseDetail": null
},
"studentContacts": {
"email": "12",
"alternatePhone": "12",
"phone": "qw"
}
}, {
"studPersonalDetails": {
"id": 0,
"name": "Anil",
"middleName": "kumar",
"lastName": "Sharma",
"fatherName": "bac",
"motherName": "bac",
"dob": "2/11/1989 12:00:00 AM"
},
"clients": {
"clientID": 1,
"clientName": null,
"clientDescriptions": null
},
"studentAddress": {
"address1": "21",
"address2": "21",
"city": "5456",
"state": "8",
"pin": 7
},
"courseDetails": {
"courseID": 58,
"courseName": "58",
"courseDetail": null
},
"studentContacts": {
"email": "12",
"alternatePhone": "12",
"phone": "abc"
}
}]
Using angular.forEach you can do it like this.
angular.forEach($scope.arr,function(value,key){
console.log(value.studPersonalDetails.lastName);
})
Using ng-repeat,you can do it like this.
<tr ng-repeat="oneArr in arr">
<td> {{oneArr.studPersonalDetails.name}}</td>
</tr>
Here is the plunker
You can see an angular ngRepeat example with your data here
First, create a controller with your data:
var myApp = angular.module('myApp',[]);
function MyCtrl($scope) {
$scope.datalist = [
{
"studPersonalDetails":{
"id":0,
"name":"Digvijay",
"middleName":"",
"lastName":"Singh",
"fatherName":"Shyam Bahadur Singh",
"motherName":"ramawati Devi",
"dob":"5/7/1990 12:00:00 AM"
},
"clients":{
"clientID":1,
"clientName":null,
"clientDescriptions":null
},
"studentAddress":{
"address1":"12",
"address2":"12",
"city":"21",
"state":"212",
"pin":2
},
"courseDetails":{
"courseID":12,
"courseName":"12",
"courseDetail":null
},
"studentContacts":{
"email":"12",
"alternatePhone":"12",
"phone":"qw"
}
},
{
"studPersonalDetails":{
"id":0,
"name":"Anil",
"middleName":"kumar",
"lastName":"Sharma",
"fatherName":"bac",
"motherName":"bac",
"dob":"2/11/1989 12:00:00 AM"
},
"clients":{
"clientID":1,
"clientName":null,
"clientDescriptions":null
},
"studentAddress":{
"address1":"21",
"address2":"21",
"city":"5456",
"state":"8",
"pin":7
},
"courseDetails":{
"courseID":58,
"courseName":"58",
"courseDetail":null
},
"studentContacts":{
"email":"12",
"alternatePhone":"12",
"phone":"abc"
}
}
];
}
Then attach this controller to your view and use the ngRepeat to extract each top-level object from dataList array (during the itaration the current object has alias data)
Inside the repeat statement you can display requested values accessing the data object using dot notation:
<div ng-controller="MyCtrl">
<div class="row" ng-repeat="data in datalist">
<p><span>Name</span> {{ data.studPersonalDetails.name }}</p>
<p><span>CourseID</span> {{ data.courseDetails.courseID }}</p>
</div>
</div>