I made an http request and got back e.g the below json. I want to read the cars and the shoes objects into separate arrays at the time of the http request.
json
{
"id": 9,
"name": "Zlatan",
"cars": [
{
"type": "ford",
"year": "2019"
},
{
"type": "audi",
"year": "2017"
}
]
"shoes": [
{
"brand": "adidas",
"status": "old"
},
{
"brand": "timberland",
"status": "new"
}
]
}
comp.ts
cars = [];
shoes = [];
//......
getZlatan() {
return this.httpService.getInfo()
.subscribe( data => {
this.objZlatan = data; //this part holds the json obj above
this.cars ....//what to add here
this.shoes ....// here too.
} );
}
Or is there a cleaner way to load the arrays at http request?
Access the cars and shoes properties of the data with simple dot-notation. It might be ideal to check if the data returned is not null with if(condition here) and then perform your logic. If you have more objects and want to bring all cars and shoes under one array then you have to loop through.
getZlatan() {
return this.httpService.getInfo()
.subscribe(data => {
this.objZlatan = data;
this.cars = this.objZlatan.cars;
this.shoes = this.objZlatan.shoes;
});
}
Just use . and type names to access cars and shoes. Let me show an example:
return this.httpService.getInfo()
.subscribe( data => {
this.objZlatan = data; //this part holds the json obj above
this.cars = data.cars;
this.shoes =data.shoes;
} );
I try to implement a $or query in mongodb but got the error: $or needs an array.
Here is my request:
const filters = {
"$or": [
{
"positionStatus": {
"$in": [
"trainee"
]
}
},
{
"positionStatus": {
"$exists": false
}
},
{
"positionStatus": []
},
{
"positionStatus": [
""
]
}
]
}
I tested the type of the $or attribute of my object with Array.isArray and it returns true:
console.log('is array: ', Array.isArray(filters['$or']));
I use the version 2.2.33 of the Nodejs driver and Mongorito.
const items = yield Item
.sort(formatted_sort)
.find(filters)
.map(item => ...);
Edit
After investigating a bit more, it appears the problem is when using the expression with count:
const count = yield Sentence
.where(filters).count();
Thanks for your help!
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) ?
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>
I'm trying to return array from laravel controller. Here is my code,
$find = array("id"=>1,"id"=>2,"id"=>3);
$result = array("data"=>$find);
return $result;
My result is :
{
"data":
[
{"id":1},
{"id":2},
{"id":3}
]
}
The result is in JSON format.
But i want to get it in Array Format like :
{
"data":
[
["id":1],
["id":2],
["id":3]
]
}
It's impossible as ["id":1] is not a valid JSON.
You can always validate JSON here