How can I get data in json array by ID - javascript

I have a problem, may you help me!
I have a json array:
"category" : [
{
id: 1,
product: [{id : product_1, type : ball}]
},
{
id : 2,
product :[{id : product_2, type : pen}]
}
]
My problem is: if i have a link such as: http://test/category/1 then , How can I do to get product information by category with id = 1 ?
Thanks for any suggestion :)

This is a way to do it in one line and without using for:
function filterById(jsonObject, id) {return jsonObject.filter(function(jsonObject) {return (jsonObject['id'] == id);})[0];}
Then if you want to get, for example, the object with id=2, you just use:
var selectedObject = filterById(yourJson['category'], 2);
And then selectedObject will get this value, as per your example:
{
id : 2,
product :[{id : product_2, type : pen}]
}
There is a downside: Old browsers, such as IE8, don't support filter() so you need to add this polyfill code for old browser compatibility:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter#Polyfill

Assuming your data structure looks like this:
var data = {
"category" : [
{
id: 1,
product: [{id : product_1, type : ball}]
},
{
id : 2,
product :[{id : product_2, type : pen}]
}
]
}
Then, you can find the item in the category array with id == 1 by searching through the array like this:
function findId(data, idToLookFor) {
var categoryArray = data.category;
for (var i = 0; i < categoryArray.length; i++) {
if (categoryArray[i].id == idToLookFor) {
return(categoryArray[i].product);
}
}
}
var item = findId(data, 1);
// item.id, item.type

If you save your array of categories in categories JS variable, you can filter this array based on simple JavaScript (with no jQuery) like that:
var result = categories.filter(function(element){
if (element.id == 2){
return true;
} else {
return false;
}
});
and then in result[0].product you will have array of products that are within specific category.
See this jsfiddle for a proof.
PS. filter() method is not available in all browsers, you may need to apply some code that will make it available if needed and not found. Here is some article on Mozilla Developers Network on the details of filter() and the code you need to make it work on old browsers: MDN: filter().

I think the OP wants to retrieve corresponding data from url.
So the first step is map the url to identifier
var urlParts = window.location.href.split(/\//)
//window.location.href = http://domain.com/category/1
// urlParts = ['http:', '', 'domain.com', 'category', '1']
var id = urlParts.pop();
var category = urlParts.pop();
Then you retrive the data from JSON string with JSON.parse:
var data = JSON.parse('{"category" : [{"id": 1, "product": [{"id" : "product_1", "type" : "ball"}] }, {"id" : 2, "product" :[{"id" : "product_2", "type" : "pen"}] } ]}');
//The data structure
// {"category" : [
// {"id": 1, "product" :
// [
// {"id" : "product_1", "type" : "ball"}
// ]},
// {"id" : 2, "product" :
// [
// {"id" : "product_2", "type" : "pen"}
// ]}
// ]}
At last, you can retrive the data similar #jfriend00's post with id and category
function findId(data, idToLookFor) {
var categoryArray = data.category;
for (var i = 0; i < categoryArray.length; i++) {
if (categoryArray[i].id == idToLookFor) {
return(categoryArray[i].product);
}
}
}
var item = findId(data.category, id);
// item.id, item.type

You might have found the answer, but if you haven't you can simply use the logical OR (||) operator.
In your case, you can use jQuery's ajax() method. To grab your product based on ids just use the below simple logic:
var prodID1 = 1, prodID2 = 2;
if(this.id == prodID || this.id == prodID2) {
//write your JSON data, etc.
//push to array, etc.
}

"category" : [
{
id: 1,
product: [{id : product_1, type : ball}]
},
{
id : 2,
product :[{id : product_2, type : pen}]
}
]
In this scenario I recommend, that you get the array from the Json response using the key. For example if your json response name is : "data" then do "data.category" which will give you an array to loop through.
Then
const allProducts = data.category;
allProducts .forEach((element) => {
//here id is your own id by which you want to compare against the json object
if (element.id == id) {
response.status(200).json(element)
//or you can return this element and capture it for any other operation you desire to do
}
});

Related

how to check for properties in an array

I am making a service call where I get back some data. For example:
var response = [
{id: 1, name: 'text1'},
{id: 2, name: 'text2'}
];
This array represents the most possible data I could get back. In other instances I may get back an empty array, an array with only one of those objects(either or of the objects inside the array).
I'd like to somehow loop through my array and check the id of each on=bject to set a flag for the front end to display other data. How do I properly do checks for the id's in the array for the scenarios I mentioned above?
Use Array.some()
var response = [{ id: 1, name: "text1" }, { id: 2, name: "text2" }];
var exists = (id, arr) => arr.some(e => e.id === id);
var id1Exists = exists(1, response);
var id2Exists = exists(2, response);
var id3Exists = exists(3, response);
console.log({ id1Exists, id2Exists, id3Exists });
var response = [
{id: 1, name: 'text1'},
{id: 2, name: 'text2'}
];
let flag
response.length > 0 && response.forEach(el => {
// JUST CHECKING IF ID IS PRESENT AND EQUAL TO 1
// YOU CAN CHANGE ACCORDINGLY
flag = el.id && el.id === 1 ? true : false
})
console.log(flag)
Here I write a function to find a certain object inside your array, I think this must help you:
public function getMyId($myArray, $id)
{
foreach($myArray as $key => $element){
$arrayFromObject = (Array) $element;
foreach($arrayFromObject as $keyObject => $valueObject){
if($valueObject['id'] == $id)
{
return $myArray[$key]->$keyObject;
}
}
}
return null;
}
For each element inside your array I convert object to array to verify if its id is equal to id u're looking for, than I return the array at certain position and certain value, or if you prefer u could just return the $valueObject it will be an array. I hope help u;

Firebase - Get data by key or chid values - javascript

I am trying to read the data from firebase database, and display the same in a webpage.
My database structure is as below -
If you see the image, i am able to read the "UserData" using the below code -
firebase.initializeApp(config);
var database = firebase.database();
var ref = database.ref('UserData');
ref.once('value', gotData1, errData);
function gotData1(data){
//console.log(data.val());
var usrData = data.val();
var keys = Object.keys(usrData);
//console.log(keys);
for (var i = 0; i< keys.length; i++){
var k = keys[i];
var id = usrData[k].AssignedID;
var name = usrData[k].Name;
$(document).ready(function() {
var $formrow = '<tr><td>'+id+'</td><td>'+name+'</td></tr>';
$('#userInfo').append($formrow);
});
}
}
In the highlighted part of the image, you can see keys with values 196214, 196215, 196216
Now, I need to fetch the values for "One, Count" by matching the key values with available AssignedID.
How can i achieve the same?
Update, JSON as text -
{
"app_url" : "https://app_name?ls=1&mt=8",
"UserData" : {
"HNpTPoCiAYMZEeVOs01ncfGBj6X2" : {
"Name" : "Arunima Vj"
"Email" : "asd#upp.com",
"AssignedID" : 196214
},
"VXU2tdGdzZX90PJa9mpEL3zAiZo2" : {
"Name" : "Lakshman Medicherla"
"Email" : "asd#upp.com",
"AssignedID" : 196215
},
"dFlwtqDNrja2RkOySVtW106IQP62" : {
"Name" : "Prashanth Sripathi"
"Email" : "asd#upp.com",
"AssignedID" : 196216
}
}
"teams" : {
"196214" : {
"1105" : {
"One" : 7619,
"count" : 24
},
"1379" : {
"Two" : 7145,
"count" : 21
}
},
"196215" : {
"1111" : {
"One" : 7779,
"count" : 20
},
"1508" : {
"Two" : 1176,
"count" : 21
}
},
"196216" : {
"1106" : {
"One" : 7845,
"count" : 22
},
"1509" : {
"Two" : 1156,
"count" : 26
}
}
}
}
Your data structure is quite nested, which makes the code more difficult to read. But this navigates the structure generically in the minimum code I could come up with:
var ref = firebase.database().ref("/42824688");
ref.child("UserData").once('value', gotUserData);
function gotUserData(snapshot){
snapshot.forEach(userSnapshot => {
var k = userSnapshot.key;
var id = userSnapshot.val().AssignedID;
var name = userSnapshot.val().Name;
ref.child("teams").child(id).once("value", teamsSnapshot => {
teamsSnapshot.forEach(teamSnapshot => {
var teamKey = teamSnapshot.key;
teamSnapshot.forEach(teamProp => {
var prop = teamProp.key;
var val = teamProp.val();
console.log(k+" "+name+" "+id+": "+teamKey+", "+prop+"="+val);
});
});
});
})
}
So for each user, this loads the teams data for that user and then loops over the teamsSnapshot to get each teamSnapshot and then loops over that to get each team property.
Working jsbin: http://jsbin.com/noziri/edit?html,js,console

Match 2 fields in different records Mongoose

Goodmorning
I am trying to match certain fields over a dataset. The dataset contains records like:
{
"token" : "17e0d95f2e2112443cfb09970ae9140e",
"answers" : {
"Yourname" : "Marco",
"youremail" : "marco#me.nl",
"recemail" : "sonja#me.nl"
}
},
{
"token" : "cf5ae65b05249dc6b2a0c99c4cf9688e",
"answers" : {
"yourname" : "Sonja",
"youremail" : "sonja#me.nl",
"recemail" : "marco#me.nl"
}
}
In this case, it should match the two because the recemail (recipients email) matches someone else's email address. And vice versa.
What I am trying to find is how to find these two records (in a larger dataset with more of these matches).
So, Marco's recemail should find Sonja's youremail and these two matches should then be made available to save into another collection, but that's not the important part here.
hope someone can help me with this query
Thanks!
The following code will return an array of 2-entry arrays. Each 2-entry array is made of the sender email and the recipient email. Each pair will appear only once. In other words, you'll get ["marco#me.nl", "sonja#me.nl"] but not ["sonja#me.nl", "marco#me.nl"].
var list = [{
"token" : "17e0d95f2e2112443cfb09970ae9140e",
"answers" : {
"Yourname" : "Marco",
"youremail": "marco#me.nl",
"recemail" : "sonja#me.nl" }
}, {
"token" : "cf5ae65b05249dc6b2a0c99c4cf9688e",
"answers" : {
"yourname" : "Sonja",
"youremail": "sonja#me.nl",
"recemail" : "marco#me.nl" }
}, {
"token" : "fe2c2740e083dfe02cc7e96520a0e079",
"answers" : {
"yourname" : "Joe",
"youremail": "joe#foo.com",
"recemail" : "mike#bar.com" }
}
];
var res = [], sz = list.length;
list.forEach(function(obj, pos) {
for(var i = pos + 1; i < sz; i++) {
if(
list[i].answers.youremail == obj.answers.recemail &&
list[i].answers.recemail == obj.answers.youremail
) {
res.push([ obj.answers.youremail, obj.answers.recemail ]);
}
}
});
console.log(res);

iterating tree in mongoDB

I have a collection of data like this(for example) :
{
name : "john" ,
_id : "0"
},
{
name : "Richard" ,
parent_id : "0" ,
_id : "1"
},
{
name : "Kevin" ,
parent_id : "0" ,
_id : "2"
},
{
name : "William" ,
parent_id : "1" ,
_id : "3"
},
{
name : "George" ,
parent_id : "3" ,
_id : "4"
}
I'm trying to write a function to receive an _id and return all children in any depth of this node, for example for _id = 0 I need something like this :
[
{
name : "Richard" ,
parent_id : "0" ,
depth : "1" ,
_id : "1"
},
{
name : "Kevin" ,
parent_id : "0" ,
depth : "1" ,
_id : "2"
},
{
name : "William" ,
parent_id : "1" ,
depth : "2" ,
_id : "3"
},
{
name : "George" ,
parent_id : "3" ,
depth : "3" ,
_id : "4"
}
]
I write several recursive functions to iterate on my mongodb docs but the main problem is I can't handle callbacks (asynchronous) and don't know when and how can I end the recursive function.
How can I do this with mongodb and node.js?
Any idea can be useful ,thanks.
There is a 2 famous algorithm that you can use in order to achive your goal
BFS(Breath First search) and DFS(Depth First Search).
For this problem BFS is better than DFS because you can trace your tree in O(logn)
You can use DFS too but you have to implement it in recursive way and running time will be O(n) and because your are coding in node js you have to implement it in a asynchronous and it could be a little hard to implement it.
In order to implement BFS algorithm you have to use asynchronous while loop because you have to have mongo query in your while loop and if you use normal javascript your BFS won't work because we are talking about node js not php!!!
So first this is asynchronous while loop that I use in my BFS code
function asyncLoop(iterations, func, callback ,foo) {
var done = false;
var loop = {
next: function() {
if (done) {
return;
}
if (iterations) {
func(loop);
} else {
done = true;
if(callback) callback(foo);
}
},
isEnd : function(){
return done ;
} ,
refresh : function(it){
iterations = it ;
},
break: function() {
done = true;
callback();
}
};
loop.next();
return loop;
}
And this is BFS algorithm node js code:
function bfs (_id ,callback){
_id = String(_id);
var q = [] ,res = [] ;
db.tasks.findOne({ _id : _id }).lean().exec(function(err,root){
root.depth = 0 ;
q.push(root);
asyncLoop(q.length ,function(loop){
res.push(q[0]);
db.tasks.find({ _parent : q[0]._id }).lean().exec(function(err,new_nodes){
if(err) console.log(err);
else {
var d = q[0].depth ;
q.shift();
loop.refresh(new_nodes.length + q.length);
if(new_nodes.length > 0){
new_nodes.forEach(function(new_node){
new_node.depth = d+1 ;
q.push(new_node);
});
}
loop.next();
}
});
},function(){ callback(res) });
});
}
Note:I also save depth of each query so you can have depth of each query and know where this query is in tree.

how to return objects in array as string with comma ?

var list = [ {
id : 1234,
shop : 'shop1'
}, {
id : 4312,
shop : 'shop2'
} ];
I want that only id attributes in object array return as "1234,4312". How can I do?
Even easier:
var list = [ { id : 1234, shop : 'shop1' }, { id : 4312, shop : 'shop2' } ];
ids = list.map(function(obj){
return obj.id
})
If you specifically need a string, add a .toString() to the end of the map call:
ids = list.map(function(obj){
return obj.id
}).toString()
You have to loop through the array and create a new array. It's actually not that hard:
var list = [ { id : 1234, shop : 'shop1' }, { id : 4312, shop : 'shop2' } ];
var ids = [];
list.forEach(function(obj, index){
ids.push(obj.id);
});
if you want that as a comma delimited string you can simply call ids.toString(); it's the default behavior.

Categories

Resources