Json object pushed into javascript array - arrays

I have this javascript array:
var data = ([
['Price', 'Time'],
['1.10661', 1467923348, ],
['1.10675', 1467923349, ],
['1.10681', 1467923350, ],
['1.10690', 1467923351, ],
]);
Im sending a request to my server using ajax and Im reciving as response this json objects:
[{"PRICE":"1.10662","TIME":"1467923352"},{"PRICE":"1.10663","TIME":"1467923353"}]
What im trying to do is to parse the response and push the objects inside of my array so the final result should be this:
var data = ([
['Price', 'Time'],
['1.10661', 1467923348, ],
['1.10675', 1467923349, ],
['1.10681', 1467923350, ],
['1.10690', 1467923351, ],
['1.10662', 1467923352, ],
['1.10663', 1467923353, ],
]);
Please keep in mind the number of objets I receive as response changes in every request. In the example Im giving is received just two objects but this changes all the time.
Thank you in advance

I guess you can break your problem down into 2 parts. First think about how you can make the response data to look the same as data, in terms of data format. Then merge the 2 arrays into one.
Here is my approach. First use the map api to go through every object in resp and extract the property value of PRICE and TIME and store it into a new array. This should leave you with an array of array with position 0 as price and position 1 as time.
[ [ '1.10662', '1467923352' ], [ '1.10663', '1467923353' ] ]
Then use the concat api to combine the 2 arrays into one.
Example:
var data = ([
['Price', 'Time'],
['1.10661', 1467923348, ],
['1.10675', 1467923349, ],
['1.10681', 1467923350, ],
['1.10690', 1467923351, ],
]);
var resp = [{"PRICE":"1.10662","TIME":"1467923352"},{"PRICE":"1.10663","TIME":"1467923353"}];
data = data.concat(
resp.map(entity => { return [ entity.PRICE, entity.TIME ]; })
);
Output:
[ [ 'Price', 'Time' ],
[ '1.10661', 1467923348 ],
[ '1.10675', 1467923349 ],
[ '1.10681', 1467923350 ],
[ '1.10690', 1467923351 ],
[ '1.10662', '1467923352' ],
[ '1.10663', '1467923353' ] ]
You might want to read more about the map and concat api here;
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map
https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Array/concat

Thank you very much for the answers! I found the solution, let me share it with you.
(The example is a bit different but the idea is exactly the same)

<html>
<body>
<script>
var myArray = [{"ASK":"1.10662","TIME":"1467923348"},{"ASK":"1.10663","TIME":"1467923346"},{"ASK":"1.10661","TIME":"1467923340"},{"ASK":"1.10663","TIME":"1467923346"},
{"ASK":"1.10661","TIME":"1467923340"}];
var data = ([
['PRICE', 'TIME'],
]);
var actualrows = data.length
var finalrows = data.length + myArray.length;
for( var i=actualrows; i<finalrows; i++ ) {
data.push( [] );
}
while(actualrows < finalrows){
var column = 0;
while(column <2){
data[actualrows][0] = myArray[actualrows-1]['ASK'];
data[actualrows][1] = myArray[actualrows-1]['TIME'];;
column++;
}
actualrows++ ;
}
</script>
</body>
</html>

Related

How to loop over multiple arrays for a resource

I want to create a resource for many regions, environments, apps, etc at once.
I'd like to do something like this:
param apps array = [
'app1'
'app2'
'app3'
]
param environments array = [
'alpha'
'beta'
]
param regions array = [
'ne'
'we'
'uks'
]
resource origin_group 'Microsoft.Cdn/profiles/origingroups#2021-06-01' = [ for region in regions: {
[ for env in environments: {
[ for app in apps: {
parent: profiles_global_fd_name_resource
name: '${env}-${region}-${app}-origin-group'
properties: {
loadBalancingSettings: {
sampleSize: 4
successfulSamplesRequired: 3
additionalLatencyInMilliseconds: 50
}
healthProbeSettings: {
probePath: '/'
probeRequestType: 'HEAD'
probeProtocol: 'Http'
probeIntervalInSeconds: 100
}
sessionAffinityState: 'Disabled'
}
}]
}]
}]
All docs mentioning nested loops talk about looping inside a resource to create many sub-resources. Not what I'm after. Perhaps another way would be to somehow merge all these arrays into a single array of objects of every possible iteration. Not sure where to start with that either.
Any help much appreciated.
This is not supported for the moment but it will (see Is there plans to support nested loop on resources?).
Using a little bit of math, you could achieve what you'd like (Not sure if you should):
param environments array = [ 'alpha', 'beta' ]
param regions array = [ 'ne', 'we', 'uks' ]
param apps array = [ 'app1', 'app2', 'app3' ]
// Setting some variables for clarity
var envCount = length(environments)
var regionCount = length(regions)
var appCount = length(apps)
// Setting the total number of combination
var originGroupCount = envCount * regionCount * appCount
// Iterate all possible combinations
output originGroupNames array = [for i in range(0, originGroupCount): {
name: '${environments[i / (regionCount * appCount) % envCount]}-${regions[i / appCount % regionCount]}-${apps[i % appCount]}-origin-group'
}]
this will output all possible combinations (I think) for origin group name.

How to get data from arrays inside of array

I'm working on an project that uses React with hooks and typescript. I'm currently trying to get data from arrays inside an array that I get from my json response. Data what i log after data handling looks like this:
[
[
"2021-09-07",
43
],
[
"2021-09-08",
47
],
[
"2021-09-09",
52
]
]
So the question is, how can i get the numerical values seen in the json, to an array?
EDIT: Got it working now. Had to add this:
const numbers = data.map((item: any) => item[1]);
console.log(numbers);
Thanks alot #Ryan Le
You would map all the numbers from the original array like so:
type Data = [string, number][];
const data: Data = [
["2021-09-07", 43],
["2021-09-08", 47],
["2021-09-09", 52]
];
const result = data.map(item => item[1]);
console.log(result);
If by numerical values you mean the second element of each array such as (43 , 47 , 52) you can simply map to a new array like this
const oldArray = [
[
"2021-09-07T08:53:34.4067874+00:00",
43
],
[
"2021-09-07T09:53:34.4067881+00:00",
47
],
[
"2021-09-07T10:53:34.4067884+00:00",
52
]
]
const newArray = oldArray.map((el)=>
el[1])
console.log(newArray)

How to compare 2 JSON response (array) in Postman

I have 2 api responses and want to compare only one of the column values 'logline'. How can I do it?
I have tried the following, but the Test Result is always Failed.
pm.test("status Check", function () {
var jsonData = pm.response.json();
var resp_arr = jsonData.results[0].series[0].values;
_.each(jsonData, (arrItem) => {
pm.expect(arrItem.status).to.be.oneOf(resp_arr);
})
});
Basically, I want to compare 2 JSON responses which are in array format and only the second column.
My API Response: Both response are in same format & structure
{
"results":[
{
"statement_id":0,
"series":[
{
"name":"process_log",
"columns":[
"time",
"logline"
],
"values":[
[
"2020-09-21T12:41:01.199552139Z",
"9/10/2020 15:57:31.073\t abc"
],
[
"2020-09-21T12:41:01.199824076Z",
"9/10/2020 15:57:31.078\t xyz"
]
]
}
]
}
]
}

Changing time format in an array using moment and angularjs and lodash

I have Json data which has timings based on dates.
obj = {
"2017-12-08": [
"2017-12-08T13:00:00+0530",
"2017-12-08T15:00:00+0530",
"2017-12-08T15:30:00+0530",
"2017-12-08T16:00:00+0530"
],
"2017-12-09": [
"2017-12-09T09:00:00+0530",
"2017-12-09T09:30:00+0530",
"2017-12-09T10:00:00+0530"
],
"2017-12-10": [
"2017-12-10T09:00:00+0530",
"2017-12-10T09:30:00+0530",
"2017-12-10T10:00:00+0530",
"2017-12-10T10:30:00+0530"
]
}
I want to convert this json object into the format below:
obj = {
"2017-12-08": ["13:00","15:00","15:30","16:00"],
"2017-12-09": ["09:00","09:30","10:00"],
"2017-12-10": ["09:00","09:30","10:00","10:30"]
}
Currently I'm using a for loop to traverse through each element in the array and then replace each element using
obj[Object.keys(obj)[date]][time]=moment(obj[Object.keys(obj)[date]][time]).format(HH:mm);
how do i accomplish this using lodash , moment and angularjs?
You only need momentjs for this, just to format the date in desired format. AngularJS and lodash are not required.
var obj = {
"2017-12-08": [
"2017-12-08T13:00:00+0530",
"2017-12-08T15:00:00+0530",
"2017-12-08T15:30:00+0530",
"2017-12-08T16:00:00+0530"
],
"2017-12-09": [
"2017-12-09T09:00:00+0530",
"2017-12-09T09:30:00+0530",
"2017-12-09T10:00:00+0530"
],
"2017-12-10": [
"2017-12-10T09:00:00+0530",
"2017-12-10T09:30:00+0530",
"2017-12-10T10:00:00+0530",
"2017-12-10T10:30:00+0530"
]
};
Object.keys(obj).forEach(key => {
obj[key] = obj[key].map(item => {
return moment(item).format("HH:mm");
});
});
console.log(obj);
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.19.3/moment.min.js"></script>
This modifies the object in-place. If you don't want that, clone the object first, and then transform the clone.
You can use _.forOwn() to loop over your object properties, _.map() to loop over each array items and use momentjs to change the item format.
This is how should be your code:
_.forOwn(obj, function(value, key) {
obj[key] = _.map(value, function(item) {
return moment(item).format("HH:mm");
});
});
Demo:
var obj = {
"2017-12-08": [
"2017-12-08T13:00:00+0530",
"2017-12-08T15:00:00+0530",
"2017-12-08T15:30:00+0530",
"2017-12-08T16:00:00+0530"
],
"2017-12-09": [
"2017-12-09T09:00:00+0530",
"2017-12-09T09:30:00+0530",
"2017-12-09T10:00:00+0530"
],
"2017-12-10": [
"2017-12-10T09:00:00+0530",
"2017-12-10T09:30:00+0530",
"2017-12-10T10:00:00+0530",
"2017-12-10T10:30:00+0530"
]
};
_.forOwn(obj, function(value, key) {
obj[key] = _.map(value, function(item) {
return moment(item).format("HH:mm");
});
});
console.log(obj);
<script src="https://cdn.jsdelivr.net/npm/lodash#4.17.4/lodash.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.19.3/moment.min.js"></script>
or you can use _.mapValues
const obj = {
"2017-12-08": [
"2017-12-08T13:00:00+0530",
"2017-12-08T15:00:00+0530",
"2017-12-08T15:30:00+0530",
"2017-12-08T16:00:00+0530"
],
"2017-12-09": [
"2017-12-09T09:00:00+0530",
"2017-12-09T09:30:00+0530",
"2017-12-09T10:00:00+0530"
],
"2017-12-10": [
"2017-12-10T09:00:00+0530",
"2017-12-10T09:30:00+0530",
"2017-12-10T10:00:00+0530",
"2017-12-10T10:30:00+0530"
]
};
const res = _.mapValues(obj, el => el.map(d => moment(d).format('HH:mm')));
console.log(res);
<script src="https://cdn.jsdelivr.net/npm/lodash#4.17.4/lodash.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.19.3/moment.min.js"></script>

Cannot get data from a js array with Node js

I have a js object array like this.
[
{
name:"Japanilainen ravintola Koto",
rating:3.9,
photo:[
{
height:2160,
html_attributions:[
"Hannes Junnila"
],
photo_reference:"CoQBdwAAAMDlivT0nOnYg8jC1txZ3RbfBR59XvKN0WphDbRVUXaUTQclzzaIaXJ8-p7s3x_aG67AUsM_HLNML6pzGl3v_wV2D-eudH_3wy2cB1ROrRgGcGyf4lRuNpE3WwXYbYZu6EK8oEPiJ5B17Lybj-eVbYM2EgVVBgOrUJhsblY1mfxWEhAZ4oHCFakH-hgkbksfGa2uGhQe4aUeOrS2isAir01KUwQ7N3Ce2Q",
width:2269
}
]
},
{
name:"Kin Sushi Helsinki",
rating:4.2,
photo:[
{
height:2988,
html_attributions:[
"Stephan Winter"
],
photo_reference:"CoQBdwAAAN4iMumSbQjtRnJIH1AKRdbSfnI02WGh11r1xaVnZl1ohebKp6zpAS4mmJFqTagrIqUJ39kzulVI0sz2UzzfaVdsAFc5f80PnOCzSLqL5gnpsqv90dVJIqUWD3Bcc9TgYPPs3oGwyekkOsmjQ59o9yqdoF5GzrpaKkojhMNLxpfzEhBKpRkA2CzINpUzAAe3e90TGhQ_KbYCmtJYLfVGIu1kZkzQIAwE4A",
width:5312
}
]
}
]
I get this array above by doing this for each.
response.results.forEach((entry)=>{
var restaurantName = {
"name" : entry.name,
"rating" : entry.rating,
"photo_reference" : entry.photos
}
arr.push(restaurantName);
});
res.send(arr);
And I send the array to my browser so I can see it.
What I am trying to do is to get photo_reference from the entry.photos
I tried entry.photos[0].photo_reference and many more ways and in all of them I am getting a cannot read properly, and now I am not sure how to get that information out.
I edited some of the variable names to make it easier to simulate here, but just map the objects in the photo arrays to their references, and you'll get an array of photo references.
const data = [
{
name:"Japanilainen ravintola Koto",
rating:3.9,
photo:[
{
height:2160,
html_attributions:[
'Hannes Junnila'
],
photo_reference:"CoQBdwAAAMDlivT0nOnYg8jC1txZ3RbfBR59XvKN0WphDbRVUXaUTQclzzaIaXJ8-p7s3x_aG67AUsM_HLNML6pzGl3v_wV2D-eudH_3wy2cB1ROrRgGcGyf4lRuNpE3WwXYbYZu6EK8oEPiJ5B17Lybj-eVbYM2EgVVBgOrUJhsblY1mfxWEhAZ4oHCFakH-hgkbksfGa2uGhQe4aUeOrS2isAir01KUwQ7N3Ce2Q",
width:2269
}
]
},
{
name:"Kin Sushi Helsinki",
rating:4.2,
photo:[
{
height:2988,
html_attributions:[
'Stephan Winter'
],
photo_reference:"CoQBdwAAAN4iMumSbQjtRnJIH1AKRdbSfnI02WGh11r1xaVnZl1ohebKp6zpAS4mmJFqTagrIqUJ39kzulVI0sz2UzzfaVdsAFc5f80PnOCzSLqL5gnpsqv90dVJIqUWD3Bcc9TgYPPs3oGwyekkOsmjQ59o9yqdoF5GzrpaKkojhMNLxpfzEhBKpRkA2CzINpUzAAe3e90TGhQ_KbYCmtJYLfVGIu1kZkzQIAwE4A",
width:5312
}
]
}
]
const arr = []
data.forEach((entry)=>{
var restaurantName = {
"name" : entry.name,
"rating" : entry.rating,
"photo_reference" : entry.photo.map(x => x.photo_reference)
}
arr.push(restaurantName);
});
console.log(arr);
entry.photoes is not defined in your response.results object array.. did you mean to access it as entry.photo (inside your foreach function) ?

Resources