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!
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]));
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?
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)
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!