Accessing Single Data from Nested Array JSON - arrays

I am trying to access a single data from a JSON file with the url 'https://123.abc.com/student'. I need to display an individual data from this URL. Could someone help me with this?
My Json data looks like this
{"students":[{"_id":"5dc516e51bdd2c0014e32e65","name":"John","email":"john#gmail.com","mobile":"9988776655","__v":0},{"_id":"5dc517b51bdd2c0014e32e69","name":"Mark","email":"mark#gmail.com","mobile":"9834783927","__v":0},{"_id":"5dca9231cfba5c0014d3d765","name":"Glen","email":"glen#gmail.com","mobile":"9996474563","__v":0},{"_id":"5dcb898b9327cb00142169bb","name":"vijay","email":"vijay#gmail.com","mobile":"9884699633","__v":0},{"_id":"5dcb89c09327cb00142169bc","name":"tests","email":"test#gmail.com","mobile":"9887456123","__v":0},{"_id":"5dcb8caa9327cb00142169bd","name":"greens","email":"greens#gmail.com","mobile":"9876543210","__v":0},{"_id":"5dcb95d69327cb00142169be","name":"Akash","email":"a4akash6#gmail.com","mobile":"9846042118","__v":0},{"_id":"5dcb961c9327cb00142169bf","name":"Akash K M","email":"Akash.Madhu#cognizant.com","mobile":"9846042118","__v":0},{"_id":"5dcce611ad69f500149660d5","name":"efavewbvwevb","email":"indrajitrakurajravi#gmail.com","mobile":"9042758563","__v":0},{"_id":"5dcce874ad69f500149660d6","name":"efavewbvwevb","email":"indrajitrakurajravi#gmail.com","mobile":"9042758563","__v":0},{"_id":"5dcce968ad69f500149660d7","name":"indrajit","email":"indrajitrakurajravi#gmail.com","mobile":"9042758563","__v":0}]}

Accessing single and array of data
let data = {"students":[{"_id":"5dc516e51bdd2c0014e32e65","name":"John","email":"john#gmail.com","mobile":"9988776655","__v":0},{"_id":"5dc517b51bdd2c0014e32e69","name":"Mark","email":"mark#gmail.com","mobile":"9834783927","__v":0},{"_id":"5dca9231cfba5c0014d3d765","name":"Glen","email":"glen#gmail.com","mobile":"9996474563","__v":0},{"_id":"5dcb898b9327cb00142169bb","name":"vijay","email":"vijay#gmail.com","mobile":"9884699633","__v":0},{"_id":"5dcb89c09327cb00142169bc","name":"tests","email":"test#gmail.com","mobile":"9887456123","__v":0},{"_id":"5dcb8caa9327cb00142169bd","name":"greens","email":"greens#gmail.com","mobile":"9876543210","__v":0},{"_id":"5dcb95d69327cb00142169be","name":"Akash","email":"a4akash6#gmail.com","mobile":"9846042118","__v":0},{"_id":"5dcb961c9327cb00142169bf","name":"Akash K M","email":"Akash.Madhu#cognizant.com","mobile":"9846042118","__v":0},{"_id":"5dcce611ad69f500149660d5","name":"efavewbvwevb","email":"indrajitrakurajravi#gmail.com","mobile":"9042758563","__v":0},{"_id":"5dcce874ad69f500149660d6","name":"efavewbvwevb","email":"indrajitrakurajravi#gmail.com","mobile":"9042758563","__v":0},{"_id":"5dcce968ad69f500149660d7","name":"indrajit","email":"indrajitrakurajravi#gmail.com","mobile":"9042758563","__v":0}]}
//access name
console.log('name--', data.students[0].name)
//single object
console.log(data.students[0])
//access array of objects
data.students.map(d=> console.log(d))

You can use this
let data = {"students":[{"_id":"5dc516e51bdd2c0014e32e65","name":"John","email":"john#gmail.com","mobile":"9988776655","__v":0},{"_id":"5dc517b51bdd2c0014e32e69","name":"Mark","email":"mark#gmail.com","mobile":"9834783927","__v":0},{"_id":"5dca9231cfba5c0014d3d765","name":"Glen","email":"glen#gmail.com","mobile":"9996474563","__v":0},{"_id":"5dcb898b9327cb00142169bb","name":"vijay","email":"vijay#gmail.com","mobile":"9884699633","__v":0},{"_id":"5dcb89c09327cb00142169bc","name":"tests","email":"test#gmail.com","mobile":"9887456123","__v":0},{"_id":"5dcb8caa9327cb00142169bd","name":"greens","email":"greens#gmail.com","mobile":"9876543210","__v":0},{"_id":"5dcb95d69327cb00142169be","name":"Akash","email":"a4akash6#gmail.com","mobile":"9846042118","__v":0},{"_id":"5dcb961c9327cb00142169bf","name":"Akash K M","email":"Akash.Madhu#cognizant.com","mobile":"9846042118","__v":0},{"_id":"5dcce611ad69f500149660d5","name":"efavewbvwevb","email":"indrajitrakurajravi#gmail.com","mobile":"9042758563","__v":0},{"_id":"5dcce874ad69f500149660d6","name":"efavewbvwevb","email":"indrajitrakurajravi#gmail.com","mobile":"9042758563","__v":0},{"_id":"5dcce968ad69f500149660d7","name":"indrajit","email":"indrajitrakurajravi#gmail.com","mobile":"9042758563","__v":0}]}
let result = data.students.forEach(val=>{
console.log(val)
// let data individually as
let name = val.name
let email = val.email
})

Related

Convert two dimensional associated array to Object using vue.js

I have an multi-dimensional associated array maplist that is such like below,
Now, I would like to convert it as an object such image is below,
The object should be arrange as such a way that array id will be object key and array name will be object value according to the maplist object image.
If I reduce array to a single object that is something like below,
let keyedOb = maplist.reduce((o,v) =>{ o[v.id] = v.name; return o},{})
Then, It will be shown as below image,
to reduce your array to a single object you can do something like
let keyedOb = maplist.reduce((o,v) =>{ o[v.id] = v.name; return o},{})

getting data from an object's array with vue.js

I am trying to access the following data in Vue.js
{"id":1,"name":"Westbrook","created_at":"2017-06-10T16:03:07.336Z","updated_at":"2017-06-10T16:03:07.336Z","stats":[{"id":1,"player_id":1,"points":2558,"assists":840,"rebounds":864,"created_at":"2017-06-10T16:03:07.373Z","updated_at":"2017-06-10T16:03:07.373Z"}]}
self.player = response.name works. now i need self.point
methods: {
fetchData: function() {
var self = this;
$.get("api/v1/players/1", function(response) {
console.log(response);
self.player = response.name;
self.point = response.stats.points
});
}
}
I have thus far tried response.stats["points"], response.stats[2], response.stats[ { points } ], response.stats[points]
The stats property in your json holds an array in which the first object has a property named points
So use response.stats[0].points
Keep in mind though that the stats is probably an array for a reason. It might hold more than one objects in the future, so always using the first element [0] might not be a valid approach.
I think it can help you
var json = JSON.parse(data);
json.stats[0].points
response = {"id":1,"name":"Westbrook","created_at":"2017-06-10T16:03:07.336Z","updated_at":"2017-06-10T16:03:07.336Z","stats":[{"id":1,"player_id":1,"points":2558,"assists":840,"rebounds":864,"created_at":"2017-06-10T16:03:07.373Z","updated_at":"2017-06-10T16:03:07.373Z"}]}
If you want to access the name
console.log(response.name) // Westbrook
If you want to access the stats data which contain list, simply target
let stats=response.stats[0] //By getting the first index in the list
Get the points in stats
console.log(stats.points) // 2588

array push of records obtained from mongo db

In MEAN stack, I am trying to store the records obtained from mongo db in an array, but I am not able to store the record in an array.
This is my code, I am trying to push the records obtained from projectimage to fulldetails[] array, but it failed. Suggest me the possible solution to store the mongo db records to array
var express = require("express"),
router = express.Router(),
project = require("../../models/project.js"),
projectimage = require("../../models/projectimages.js"),
var details=data;
var fulldetails=[];
for (var i = 0; i < details.length; i++) {
var prjct_id=details[i]._id;
console.log('below'+i);
fulldetails.push(details[i]);
projectimage.findOne({projectId: prjct_id}, function(err, data){
fulldetails.concat(data);
});
console.log(fulldetails);
return false;
}
I think what do you want is to get an array from your Mongo collection, correct me if I am wrong.
I am also assuming that Mongo query runs successfully, and returns the records correctly. then you can use toArray function to get array in callback.
// let's say Furniture is your collection
let furniture = Furniture.find({});
let details = [];
furniture.toArray((err, array) => {
if (err) return;
details = array; // now details has your collections' documents
});
For more refer this
Let me know if this is not what you were looking for.
I think you're trying to get a flat array of full details. If my assumption is correct, you could try my solution here:
var fulldetails=[];
for (var i = 0; i < details.length; i++) {
var prjct_id=details[i]._id;
projectimage.find({projectId: prjct_id}).toArray(function(err, data){ //convert data to array first
console.log(data);
fulldetails.concat(data); // concat data to fulldetails to get a flat array
});
}

Store different item types in dictionary

I want to store a dictionary as:
[String : AnyItem]
because the format will be
["codename" : "title", "image" : UIImage("imageName")]
but I am having a difficult type accomplishing this. The ultimate goal is to have an array full of these types of dictionaries.
If you want a dictionary value that can hold anything, you need Any (rather than AnyType.
But this will be a real pain – whenever you want to get the value out, you’ll have to covert the types back every time, and if you mess up doing that, you’ll get all sorts of errors.
Instead, consider using a tuple – a pair of the two types you want. You can name the elements to make them easier to access:
var images: [(codename: String, image: UIImage)] = []
if let url = NSURL(string: "http://www.gravatar.com/avatar/24d29e1ff91b68642bbef96b43b5e119?s=128&d=identicon&r=PG&f=1"),
let data = NSData(contentsOfURL: url),
let image = UIImage(data: data) {
images.append(codename: "AlexAvatar", image: image)
}
images[0].codename // name of the first image in the array
images[0].image // the first image in the array
Alternatively, if you are going to want to address the images by name, why not use the “codename” field as the dictionary key?
var images: [String:UIImage)] = []
if let url = NSURL(string: "http://www.gravatar.com/avatar/24d29e1ff91b68642bbef96b43b5e119?s=128&d=identicon&r=PG&f=1"),
let data = NSData(contentsOfURL: url),
let image = UIImage(data: data) {
images["AlexAvatar"] = image
}
for (codename, image) in images {
// iterate over the codename/image pairs
}
If your actual data requirements are more complicated, consider a struct instead of a tuple.

Bug when sending array in node.js and socket.io

I use socket.io version 0.8.4
I have boiled down my problem to the following. I have data looking like this:
data.prop1 = [];
data.prop1.push("man");
data.prop2 = [];
data.prop2["hey"] = "man";
I send the data from the server to the client this way:
socket.emit("data", data);
On the client side I receive the data this way:
socket.on("data", function(data){ console.log(data); });
The weird thing is:
data.prop1 = [];
data.prop1.push("man"); // This data exists in the client side data object
data.prop2 = [];
data.prop2["hey"] = "man"; // This data does not exist.
data.prop2 is just an empty array on the client side.
Is there a known bug in json serializing arrays on the form in prop2?
Thankyou in advance
EDIT:
Problem solved using this workaround:
data.prop1 = [];
data.prop1.push("man");
data.prop2 = {}; // <= Object instead of array
data.prop2["hey"] = "man";
ECMA-262 about JSON.stringify:
The representation of arrays includes only the elements between zero and array.length – 1 inclusive. Named properties are excluded from the stringification.
Arrays are supposed to have numerical property names. So when the data.prop2 is transformed to JSON (which socket.io sends the data in, I imagine), it doesn't get the 'hey' property. If you want to use non-numerical property names, you should use objects instead of arrays:
data.prop1 = [];
data.prop1.push("man");
data.prop2 = {}; // Notice we're creating an object, not an array.
data.prop2["hey"] = "man"; // Alternatively: data.prop2.hey = "man"
Unfortunately, Javascript doesn't really work like that.
Check out this article, about half way down. It explains the problem where you try to set data.prop2["hey"] = "man";

Resources