Using cheerio, I have managed to scrape a PHP generated table that contains a column of dates, locations, etc. As the number of rows is variable, I opted to use .map() to iterate through each row, setting the matching starting event dates (startDate) with the provided CSS selectors. The above process seems to be functioning well, as when I call console.log(startDate) I recieve the output below. it would appear, however, that the process creates an array for each time it moves to the next row, appending an additional date each time. How can I set a variable to only the last array in the array startDate?
[ '03/18/2014' ]
[ '03/18/2014', '03/01/2014' ]
[ '03/18/2014', '03/01/2014', '02/15/2014' ]
[ '03/18/2014', '03/01/2014', '02/15/2014', '01/31/2014' ]
[ '03/18/2014',
'03/01/2014',
'02/15/2014',
'01/31/2014',
'01/17/2014' ]
[ '03/18/2014',
'03/01/2014',
'02/15/2014',
'01/31/2014',
'01/17/2014',
'12/06/2013' ]
[ '03/18/2014',
'03/01/2014',
'02/15/2014',
'01/31/2014',
'01/17/2014',
'12/06/2013',
'11/16/2013' ]
So the desired output of console.log(newArray) would be:
[ '03/18/2014',
'03/01/2014',
'02/15/2014',
'01/31/2014',
'01/17/2014',
'12/06/2013',
'11/16/2013' ]
If startDate is an array, you should be able to get the last item in the array by using the index, like this:
var lastStartDate = startDate[startDate.length-1]; //now lastStartDate contains the last item in the array
Related
I am trying to create postman tests for an API.
The response I am getting from post man is in a json form. It looks like an Object of arrays.
{
"389": [
[
"2021-04-30T00:00:00Z",
16.130089309443093
]
],
"390": [
[
"2021-04-30T00:00:00Z",
14.899161948201808
]
],
"391": [
[
"2021-04-30T00:00:00Z",
17.495245579925736
]
],
"392": [
[
"2021-04-30T00:00:00Z",
16.78176061001777
]
],
"393": [
[
"2021-04-30T00:00:00Z",
25.473437964096448
]
],
"394": [
[
"2021-04-30T00:00:00Z",
56.746358310562826
]
],
"388": [
[
"2021-04-30T00:00:00Z",
18.49559245290604
]
]
}
I am trying to test the integer value that comes after the date is greater than 0 but cant seem to figure out how to traverse the structure in javascript.
With normal response Jsons they usually have the ID beside them and you can use that value, but not with this response
This is the test so far
pm.test("Check performance > 0", function () {
var jsonData = pm.response.json();
pm.expect(jsonData.value).to.greaterThan(0);
});
It seems you are testing the whole JSON to be > 0. You should loop throught the values :
pm.test("Check performance > 0", function () {
var jsonData = pm.response.json();
Object.values(jsonData).forEach(record => {
pm.expect(record[0][1]).to.greaterThan(0);
});
/* record will be equal to
[
[
"2021-04-30T00:00:00Z",
16.130089309443093
]
]
and then
[
[
"2021-04-30T00:00:00Z",
14.899161948201808
]
]
and so on */
});
Object.values retrieves the values of a dictionary. See Object.values
(if we want the keys ["389", "390", ...] we can use Object.keys)
I have two arrays I'm trying to combine in a very specific way and I need a little guidance. Array1 is an array of dates 30-40 items, Array 2 is a list of objects with a date inside one of the attributes. I'm trying to append the object in array 2 to the index of array1 when the dates match.
I want to put arr2 in the same index as arr1 if the dates match.
const arr = [
"2022-06-26T07:00:00.000Z",
"2022-06-27T07:00:00.000Z",
"2022-06-28T07:00:00.000Z",
"2022-06-29T07:00:00.000Z",
"2022-06-30T07:00:00.000Z",
"2022-07-01T07:00:00.000Z",
"2022-07-02T07:00:00.000Z",
"2022-07-03T07:00:00.000Z",
"2022-07-04T07:00:00.000Z",
"2022-07-05T07:00:00.000Z",
"2022-07-06T07:00:00.000Z",
"2022-07-07T07:00:00.000Z",
"2022-07-08T07:00:00.000Z",
"2022-07-09T07:00:00.000Z",
"2022-07-10T07:00:00.000Z",
"2022-07-11T07:00:00.000Z",
"2022-07-12T07:00:00.000Z",
"2022-07-13T07:00:00.000Z",
"2022-07-14T07:00:00.000Z",
"2022-07-15T07:00:00.000Z",
"2022-07-16T07:00:00.000Z",
"2022-07-17T07:00:00.000Z",
"2022-07-18T07:00:00.000Z",
"2022-07-19T07:00:00.000Z",
"2022-07-20T07:00:00.000Z",
"2022-07-21T07:00:00.000Z",
"2022-07-22T07:00:00.000Z",
"2022-07-23T07:00:00.000Z",
"2022-07-24T07:00:00.000Z",
"2022-07-25T07:00:00.000Z",
"2022-07-26T07:00:00.000Z",
"2022-07-27T07:00:00.000Z",
"2022-07-28T07:00:00.000Z",
"2022-07-29T07:00:00.000Z",
"2022-07-30T07:00:00.000Z",
"2022-07-31T07:00:00.000Z",
"2022-08-01T07:00:00.000Z",
"2022-08-02T07:00:00.000Z",
"2022-08-03T07:00:00.000Z",
"2022-08-04T07:00:00.000Z",
"2022-08-05T07:00:00.000Z",
"2022-08-06T07:00:00.000Z"
]
const arr2 = [
{
"gsi1SK": "name ",
"searchPK": "thing",
"SK": "uuid",
"Desc": "place #1205",
"PK": "thing uuid",
"searchSK": "7/1/2022",
"gsi1PK": "thing",
"Complete": false,
"Users": [
"person1",
"person2"
]
},
{
"gsi1SK": "name",
"searchPK": "thing",
"SK": "uuid",
"Desc": "place#124124",
"PK": "thing uuid",
"searchSK": "7/4/2022",
"gsi1PK": "thing",
"Complete": false,
"Users": [
"person2",
"person45"
]
}
]
console.log([arr, arr2]);
You seem to have a handle on the date conversion part. Here I've defined two short sample arrays to represent arr2 and newArr. Then, a map function to create the output.
const arr2 = [
{
"OTHER_FIELDS": "TOP SECRET",
"searchSK":"7/4/2022"
},
{
"OTHER_FIELDS": "TOP SECRET",
"searchSK":"7/9/2022"
}
];
const newArr = [
[
"7/2/2022"
],
[
"7/3/2022"
],
[
"7/4/2022"
],
[
"7/5/2022"
],
[
"7/6/2022"
],
[
"7/7/2022"
],
[
"7/8/2022"
],
[
"7/9/2022"
],
[
"7/10/2022"
]
];
// for each subarray in newArr, return an array containing the existing element plus any elements from arr2 found by the filter function
const output = newArr.map(el => [...el, ...arr2.filter(a2 => a2.searchSK === el[0])]);
console.log(output);
Plan
You've got two obvious options:
A. Look at each of the objects, finding a home for each one in turn
B. Look at each of the dates, collecting all the objects that belong to it
Which method makes more sense for you will depend on other factors you haven't covered in your post. I think the main question is: is it guaranteed that the date list will contain a proper home for every object? If no, then do you want to drop the objects without proper homes, or do you want to create a proper home for the objects
Performance can also matter, but really only if you expect either list to be very long or if you need to run this process multiple times (such as in a React component in the browser).
Implement
Loop through the list you chose. For each item, scan the other list for the relevant item(s): its home or its children. Take the appropriate action for those items depending on which plan you chose.
Another consideration is: don't mutate your arguments. That means you probably need to create copies of the two input arrays before you do the work. If the arrays contain objects rather than scalars, you can't just do array.slice() to create a copy.
For an array of POJOs, you can convert the source to a string and then back again to create a clone.
The array of dates will need special handling, because JSON.parse will not revive dates.
Mutating arguments is generally a bad practice, at least in the functional paradigm that underlies many popular front-end frameworks today. Plus, if you create your own copies of the input data, you can gain efficiency by moving items from the source arrays to the output array, which means that subsequent iterations won't have to re-examine items that have already been processed.
I want to convert this:
[null, 1890, null, NGU]
...into this:
[[], [1890], [], [NGU]]
I've tried creating a new array and pushing values to it, but that just ends up looking the same. Honestly, I'm unsure of what to even call what I'm trying to create. Is it a two-dimensional array or an array of objects?
This is for a google app script and the documentation calls it a two-dimensional array of values.
var arr = [null, 1890, null, 'NGU']
var arr2d = arr.map(x => [x])
console.log(arr2d) // output --> [ [ null ], [ 1890 ], [ null ], [ 'NGU' ] ]
I'm retrieving hashmap data from an api into my react app. the data looks like this..
[
[
"d243dc7-6fe8-151b-4ca8-1be528f2b36",
"[\"Jonny\",70]"
],
[
"8affa17-76d1-13e-6380-7cd2a3e3647",
"[\"Lucy\",106,"pic3.jpg"]"
],
[
"841cb28-24c7-872-3c66-63253800c8d",
"[\"Mike\",0]"
],
[
"6128e-182-cfb4-708b-c40a3ba2e6e",
"[\"Elodie\",23,"me.jpg"]"
],
[
"e55ef4c-8d41-3be4-27d-aae53330584",
"[\"Jacob\",9,"img-004.jpeg"]"
]
]
I need to render this data into a table.
Using map and with with data.slice(0, 1) I've managed to pull out the key (the long string), but I need help separating the name, number and optional image. I tried experimenting with various operators. I thought this would work
{data.slice(1, 2).toString().substring(2,data.slice(1, 2).length-2)}
but it just returns ["
I'm hoping there is a simpler way to do it!
You presented the data in a strange format, but this works:
const data = [
[
"d243dc7-6fe8-151b-4ca8-1be528f2b36",
"[\"Jonny\",70]"
],
[
"8affa17-76d1-13e-6380-7cd2a3e3647",
"[\"Lucy\",106,\"pic3.jpg\"]"
],
[
"841cb28-24c7-872-3c66-63253800c8d",
"[\"Mike\",0]"
],
[
"6128e-182-cfb4-708b-c40a3ba2e6e",
"[\"Elodie\",23,\"me.jpg\"]"
],
[
"e55ef4c-8d41-3be4-27d-aae53330584",
"[\"Jacob\",9,\"img-004.jpeg\"]"
]
];
const data1 = JSON.parse(data.slice(1, 2)[0][1]);
console.log('name', data1[0]);
console.log('numb', data1[1]);
console.log('file', data1[2]);
{data.slice(1, 2).toString().substring(2,data.slice(1, 2).length-2)}
^ ^^^^^^^^^^^^^^^^
In the string "["Jonny",70]" , you only want to skip the first character "[", so your first parameter of substring() should be index 1
you forgot to transfer data.slice(1, 2) to string, so it's still an array and its length would be 1
so your code should be revised to:
data.slice(1, 2).toString().substring(1, data.slice(1, 2).toString().length - 2)
I'm trying to display this kind of list on a chart:
[ [ Moment<2020-08-02T02:54:24+02:00>, 9.99 ],
[ Moment<2020-08-05T21:55:33+02:00>, 10.99 ],
[ Moment<2020-08-05T22:00:50+02:00>, 26.87 ],
[ Moment<2020-08-06T23:43:32+02:00>, 10.99 ],
[ Moment<2020-08-07T22:57:13+02:00>, 26.87 ],
[ Moment<2020-08-07T23:01:20+02:00>, 9.99 ],
[ Moment<2020-08-08T22:10:14+02:00>, 10.99 ],
[ Moment<2020-08-08T22:12:20+02:00>, 35.83 ],
[ Moment<2020-08-10T19:03:51+02:00>, 20.24 ],
[ Moment<2020-08-11T17:24:39+02:00>, 10.99 ],
[ Moment<2020-08-11T17:27:41+02:00>, 27.03 ],
[ Moment<2020-08-13T14:04:34+02:00>, 29.54 ],
[ Moment<2020-08-17T04:17:37+02:00>, 9.99 ],
[ Moment<2020-08-25T04:57:57+02:00>, 10.99 ] ]
What I try to get is to have the x-axis separated by one day each square but keeping the fact that there can be multiple values for one day (so every point is separated by a different distance). I don't really know how to approach it, if you can give me some directions on how to start it, it would be much appreciated. Many thanks.
You need to create a range for the x-axis, listing all the dates.
For that you can use: https://www.npmjs.com/package/moment-range
Then loop through that range, for each day get the values from your list (by filtering for example).
It then depends on your chart library and business logic on how you'd combine multiple values per day (for example adding them) or display them individually as stacked column chart.