JSON data filtering using javaScript [closed] - javascript

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
I have assigned a task to group data in angular js using underscore js.
My JSON :
data = [
{
"Building*": "Building A",
"Wing*": "Wing C",
"Floor*": "Floor 3",
"Room Name*": "Room 3",
"Room Type*": "AC",
"Location*": "Location 1",
"Device ID*": 27,
"Category*": "Soap Hygene",
"Dispenser Name*": "Dispenser 34",
"Type*": "Manual",
"Cartridge Type*": "Type 1",
"Date of installation": "2016-04-11T06:06:22 -06:-30",
"Contact Last Name": "Maynard",
"Email Address": "thomas.boscher#gmail.com",
"Mobile Number with country code": "+1 (949) 590-3465",
"Description": "Description of device",
"Model": 37
},
{
"Building*": "Building B",
"Wing*": "Wing B",
"Floor*": "Floor 3",
"Room Name*": "Room 1",
"Room Type*": "AC",
"Location*": "Location 3",
"Device ID*": 26,
"Category*": "Soap Hygene",
"Dispenser Name*": "Dispenser 33",
"Type*": "Manual",
"Cartridge Type*": "Type 2",
"Date of installation": "2015-07-24T12:42:24 -06:-30",
"Contact Last Name": "Holland",
"Email Address": "thomas.boscher#gmail.com",
"Mobile Number with country code": "+1 (947) 491-2353",
"Description": "Description of device",
"Model": 32
}
]
I need data in below format, where it has each building details containing the wing and floor data
updateData = [{
building: 'Building A' ,
buildingData:[ {
wing: "Wing A",
wingData: [{
floor:'Floor 2',
floorData:[{
room:'Room 3',
roomData:[]
}]
}]
}]
}];
I tried :
js fiddle
But it fails. Need help. Thanks in advance.

From my understanding you want to groupBy in the following nested order:
Building ->> Wing ->> Floor ->> Room
You can try calling groupBy recursively (or looping through, up to you) by passing an array of the order of nested attribute you want to groupBy. Try this snippet below:
const nestedOrder = ['Building*', 'Wing*', 'Floor*', 'Room Name*']
function groupFilter (rawData, attrList) {
if (attrList.length == 0) return rawData
var currentAttr = _(attrList).first()
return _(rawData)
.chain()
.groupBy(currentAttr)
.map((list, attrName) => Object({
[currentAttr]: attrName,
[`${currentAttr}Data`]: groupFilter(list, _(attrList).rest())
}))
.value()
}
console.log(groupFilter(data, nestedOrder))
The raw data will then be compacted to the deepest nested attribute, in this case 'Room Name*', you can then write your custom filter to output what you want RoomData to hold.
Not sure if its the right way to do it, but if runtime isnt a big issue for you then I think you can make it work fine.
Hopefully this helps/works out for you.

Related

How do I create working assertion versus JSON structure in Cypress?

I`m performing API automation testing and use GET request via Cypress and receive in the body the next structure:
{
my_list: [
{
code: "SAT-12-33-1",
description: "FLOOR 1",
payment: "128"
},
{
code: "SAT-12-33-2",
description: "FLOOR 2",
payment: "33"
},
{
code: "SAT-12-33-3",
description: "FLOOR 3",
payment: "311"
},
{
code: "SAT-12-33-4",
description: "FLOOR 4",
payment: "342"
},
{
....and so on. The full structure is 3400 records.
Could somebody give me an exact example, HOW to validate, that for instance this chunk (2 elements only) of code:
{
code: "SAT-12-33-2",
description: "FLOOR 2",
payment: "33"
},
{
code: "SAT-12-33-3",
description: "FLOOR 3",
payment: "311"
}
exactly matching on the received array of 3400 records. In my case, I will have 3 or 4 elements and I have to compare them/validate versus the big array.
I have tried expect(response.body).to.contain(), deep.equal(), .should('include') and more, but does not work to me.
Can someone provide the exact code that matching part of the structure above to the whole request body with the same structure? Again, I will have 2 or 3 elements and want to compare them together to a big array of 3400 elements?
Thank you very much!

Sort an array of objects(with multiple properties) in a specified order of Keys with JavaScript [duplicate]

This question already has answers here:
Sort JavaScript object by key
(37 answers)
Closed 2 years ago.
I have a json response with the following data in alphabetical order,
let sheetCells =
{
0: [
{
"Actual Hours": 16
"Assigned": "Jerry"
"Completion %": 48.2
"Days Scheduled": 2
"Device 1": 10
"Device 2": 43
"Device 3": 72
"Device 4": 91
"Device 5": 25
"End Date": "2018-01-03T23:00:00.000Z"
"Estimated Hours": 16
"ID": "1dbk3"
"Notes": "Note 1"
"Parent ID": "2k59f"
"Priority": ""
"Start Date": "2018-01-01T23:00:00.000Z"
"Stories": "A User"
"Tests": "Y"
},
{
//...same keys as above, different values
}
]
}
but this is the DESIRED format in which I want the resulting objects in the array be sorted,
[
{
"Stories": "A User"
"ID": "1dbk3"
"Parent ID": "2k59f"
"Completion %": 48.2
"Priority": ""
"Device 1": 10
"Device 2": 43
"Device 3": 72
"Device 4": 91
"Device 5": 25
"Assigned": "Jerry"
"Notes": "Note 1"
"Tests": "Y"
"Estimated Hours": 16
"Days Scheduled": 2
"Start Date": "2018-01-01T23:00:00.000Z"
"End Date": "2018-01-03T23:00:00.000Z"
"Actual Hours": 16
},
{
//...same keys as above, different values
}
]
Here is the solution that i've tried
let sortOrder = ['Stories', 'ID', 'Parent ID', 'Completion %', 'Priority', 'Device 1', 'Device 2', 'Device 3', 'Device 4', 'Device 5', 'Assigned', 'Notes', 'Tests', 'Estimated Hours', 'Days Scheduled', 'Start Date', 'End Date', 'Actual Hours']
let result = sheetCells[0].sort((a,b) => sortOrder.indexOf(a.Stories) > sortOrder.indexOf(b.Stories)))
but this result gets just the story key and ignores the rest, please help with a working and optimal solution.Thanks
Rather than resorting the original data, you can map it to a new sorted array by indexing it with every element in your sortOrder in order. Something like this:
let result = sortOrder.map(key => sheetCells[0][key]);
While that's a working version of your solution, based on the data structure in your example, this may be closer to what you're looking for:
let result = sheetCells[0].map(cell => sortOrder.map(key => cell[key]));

Reformat Javascript Object for use with Foreach

I have a form which has a repeatable field group. I am using serializeArray() on the form submit to get all of the form values however the repeatable fields aren't in an easily workable format so I need to modify these so I am able to utilise them within a foreach
This is the response from the serializeArray()
{
"_acffr_repeatable_group_fields": "[\"acrepeater-vehical[0][vehicle-make]\",\"acrepeater-vehical[0][vehicle-model]\",null,\"acrepeater-vehical[0][vehicle-year]\",\"acrepeater-vehical[1][vehicle-make]\",\"acrepeater-vehical[1][vehicle-model]\",null,\"acrepeater-vehical[1][vehicle-year]\",\"acrepeater-vehical[2][vehicle-make]\",\"acrepeater-vehical[2][vehicle-model]\",null,\"acrepeater-vehical[2][vehicle-year]\"]",
"_acffr_repeatable_groups": "[\"acrepeater-vehical\"]",
"your-name": "Name",
"your-email": "test#test.com",
"YourZipCode": "zip code",
"acrepeater-vehical[0][vehicle-make]": "Make 1",
"acrepeater-vehical[0][vehicle-model]": "Model 1",
"acrepeater-vehical[0][vehicle-type]": "Car",
"acrepeater-vehical[0][vehicle-year]": "2001",
"acrepeater-vehical[1][vehicle-make]": "Make 2",
"acrepeater-vehical[1][vehicle-model]": "Model 2",
"acrepeater-vehical[1][vehicle-type]": "Truck",
"acrepeater-vehical[1][vehicle-year]": "2002",
"acrepeater-vehical[2][vehicle-make]": "Make 3",
"acrepeater-vehical[2][vehicle-model]": "Model 3",
"acrepeater-vehical[2][vehicle-type]": "Commercial",
"acrepeater-vehical[2][vehicle-year]": "2003"
}
As you can see from the above, for each repeated group of fields it adds the [N] to the acpeater-vehical however this flat format doesn't permit me to loop through the fieldsets
How could I reformat this to be in the appropriate format?

Making new array and push there keys on that new array [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
I have a list of an object array
var list = {
Achievement: ["110", "100", "104", "110"],
Emp Code : ["1000001", "1000001", "1000001", "1000001"],
Product :["Product A ", "Product B", "Product A ", "Product B"],
Reportee Name :["Harry", "Harry", "Peter", "Peter"],
Target : ["116", "94", "105", "114"],
percentage: ["94.82758621", "106.3829787", "99.04761905", "96.49122807"]
}
and the array I want that is mention below. Where key item should be the part of array.
var list = {
0: ["Achievement","110", "100", "104", "110"],
1 : ["Emp Code","1000001", "1000001", "1000001", "1000001"],
2 :["Product" ,"Product A ", "Product B", "Product A ", "Product B"],
3 :["Reportee Name","Harry", "Harry", "Peter", "Peter"],
4 : ["Target","116", "94", "105", "114"],
5: ["percentage","94.82758621", "106.3829787", "99.04761905", "96.49122807"]
}
You can use Object.keys() and .reduce() method:
let data = {'Achievement': ["110", "100", "104", "110"],'Emp Code' : ["1000001", "1000001", "1000001", "1000001"],'Product' :["Product A ", "Product B", "Product A ", "Product B"],'Reportee Name' :["Harry", "Harry", "Peter", "Peter"],'Target' : ["116", "94", "105", "114"],'percentage': ["94.82758621", "106.3829787", "99.04761905", "96.49122807"]};
let result = Object.keys(data)
.reduce((a, c, i) => (a[i] = [c, ...data[c]], a), {});
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
Why do you need an object with indexes like an array? Array solution:
let list = {'Achievement': ["110", "100", "104", "110"],'Emp Code' : ["1000001", "1000001", "1000001", "1000001"],'Product' :["Product A ", "Product B", "Product A ", "Product B"],'Reportee Name' :["Harry", "Harry", "Peter", "Peter"],'Target' : ["116", "94", "105", "114"],'percentage': ["94.82758621", "106.3829787", "99.04761905", "96.49122807"]};
let result = []
for (let key in list) {
result.push([key, ...list[key]])
}
console.log(result)

.innerHTML displaying [object htmldivelement]

Sorry guys I'm really new at this!
I'm looping through an array of people I get back from the server (please ignore the syntax if it's incorrect, it's just an arbitrary example)
[{
"group1": [
{
"name": "Steve Smith";
"age": "23",
},
{
"name": "Taylor Smith";
"age": "28",
}
],
"group2": [
{
"name": "Henry Thomas";
"age": "24";
},
{
"name": "Susan Thomas";
"age": "22";
}
]
}]
So basically I'm looping through these objects and I'm displaying each person in it's own container. I'm taking each "individual person" container and appending
that to their own "group container" (i.e. one group container will contain all of "group 1" and the other will contain all of "group 2").
On a button press, I used .appendChild to append the correct "group" container but that would just keep adding whichever "group" container I wanted to display to the main container instead of only displaying the one I wanted to look at, which made sense. I tried using mainContainer.innerHTML = group but that displayed [object htmldivelement].
What would be the best way to go about doing this? Thanks!

Categories

Resources