I am trying to build this function that sets up my object for me
var schema = function(tableName, data) {
return dataSet = {
tableName: {
1: data
}
};
};
var dataSet = schema("messages", data);
But when I execute this it returns tableName as a string, not using the variable that I pass through the function?
Is it possible to use the variable that I pass into my function as a name so that I get it returned like this:
{
"message": {
"1": {
"username": "Simon",
"message": "First message"
}
}
}
Instead of this:
{
"tableName": {
"1": {
"username": "Simon",
"message": "First message"
}
}
}
Dot notation is not evaluated, but the square bracket syntax is:
var schema = function(tableName, data) {
var dataSet = {};
dataSet[tableName] = {
1: data
};
return dataSet;
};
Related
const { screen } = data;
// screen = 'News', string
const { params } = data;
// params = object
// Object {
"body": "test",
"created_at": "2020-05-25 21:25:37",
"status": 1,
"title": "test",
"updated_at": "2020-05-25 21:25:37",
}
if (notification.origin === 'selected') {
if (screen) {
NavigationService.navigate(screen, { params });
}
}
function navigate(routeName, object) {
console.log('routeName =' + routeName);
// routeName =News
console.log(object);
// Object {
"params": null,
}
}
problem
I can't get object in function navigate.
I don't still understand it..
I would appreciate it if you could give me any advice.
You should not add the brackets around param when calling navigate. Call it like so:
NavigationService.navigate(screen, params);
Instead of:
NavigationService.navigate(screen, { params });
I'm querying from firebase realtime database, storing each result in an array, and returning this array. While the data is correct, I am not returning it in a type I want it to be in.
The code for this function is
exports.getprojects = functions.https.onRequest((req, res) => {
const uid = req.body.uid;
var projectref = fref.child('projects');
var projarr = [];
// Make the promise object for query
const projpromise = projectref.orderByChild('key').equalTo(uid).once('value');
// Another Promise for pushing to array
const pushpromise = projpromise.then(snap => {
var proj_item = snap.val();
// push to array
projarr.push(proj_item);
return proj_item;
}).catch(reason => {
console.log(reason)
return res.json(400);
})
// Respond with array
return pushpromise.then((proj_item) => {
return res.json(200, projarr);
}).catch(reason => {
console.log(reason)
return res.json(400);
})
});
This function queries all the right data, but returns in format like this :
[
{
"project_id": {
"project_title": "Test Project1",
"project_type": "Long Term"
},
...
]
What I need is for this function to return in format like below :
{
{
"project_id": {
"project_title": "Test Project1",
"project_type": "Long Term"
},
...
}
How can I achieve this? Any help is appreciated. Thank you!
To turn your array into a JSON object with just the key-value pairs, do something like this:
var array = [
{"project_id": {
"project_title": "Test Project1",
"project_type": "Long Term"
}},
{"project_id2": {
"project_title": "Test Project2",
"project_type": "Medium Term"
}},
{"project_id3": {
"project_title": "Test Project3",
"project_type": "Short Term"
}}
];
var result = {};
array.forEach(function(object) {
var key = Object.keys(object)[0];
result[key] = object[key];
})
console.log(result);
How can i find data that is related to the already known data?
( I'm a newb. )
For example here is my json :
[
{ "id": "1", "log": "1","pass": "1111" },
{ "id": 2, "log": "2","pass": "2222" },
{ "id": 3, "log": "3","pass": "3333" }
]
Now i know that "log" is 1 and i want to find out the data "pass" that is related to it.
i've tried to do it so :
The POST request comes with log and pass data , i search the .json file for the same log value and if there is the same data then i search for related pass
fs.readFile("file.json", "utf8", function (err, data) {
var jsonFileArr = [];
jsonFileArr = JSON.parse(data); // Parse .json objekts
var log = loginData.log; // The 'log' data that comes with POST request
/* Search through .json file for the same data*/
var gibtLog = jsonFileArr.some(function (obj) {
return obj.log == log;
});
if (gotLog) { // If there is the same 'log'
var pass = loginData.pass; // The 'pass' data that comes with POST request
var gotPass = jsonFileArr.some(function (obj) {
// How to change this part ?
return obj.pass == pass;
});
}
else
console.log("error");
});
The problem is that when i use
var gotPass = jsonFileArr.some(function (obj) {
return obj.pass == pass;
});
it searches through the whole .json file and not through only one objekt.
Your main problem is that .some() returns a boolean, whether any of the elements match your predicate or not, but not the element itself.
You want .find() (which will find and return the first element matching the predicate):
const myItem = myArray.find(item => item.log === "1"); // the first matching item
console.log(myItem.pass); // "1111"
Note that it is possible for .find() to not find anything, in which case it returns undefined.
The .some() method returns a boolean that just tells you whether there is at least one item in the array that matches the criteria, it doesn't return the matching item(s). Try .filter() instead:
var jsonFileArr = JSON.parse(data);
var log = loginData.log;
var matchingItems = jsonFileArr.filter(function (obj) {
return obj.log == log;
});
if (matchingItems.length > 0) { // Was at least 1 found?
var pass = matchingItems[0].pass; // The 'pass' data that comes with the first match
} else
console.log("error"); // no matches
Using ES6 Array#find is probably the easiest, but you could also do (among other things)
const x = [{
"id": "1",
"log": "1",
"pass": "1111"
}, {
"id": 2,
"log": "2",
"pass": "2222"
}, {
"id": 3,
"log": "3",
"pass": "3333"
}];
let myItem;
for (let item of x) {
if (item.log === '1') {
myItem = item;
break;
}
}
console.log(myItem);
I have an array of objects
object will be in below format
var newUserDetail={"Age":"21","name":"Vicky","UserId":"198303"};
Now I am trying to compare UserId and replace the values
//usersList contains array of newUserDetail kind of objects
jQuery(usersList).each(function(){
if(this.UserId==newUserDetail.UserId){
this=newUserDetail;
}
});
But it throws an error
Invalid left-hand side in assignment
Set the array entry:
jQuery(usersList).each(function (i) {
if (this.UserId == newUserDetail.UserId) {
usersList[i] = newUserDetail;
return false; //if you want to break the loop
}
});
Try this
$(document).ready(function() {
var newUserDetail = {
"Age": "21",
"name": "Vicky",
"UserId": "198303"
};
var tm = {
"Age": "21",
"name": "Vicky",
"UserId": "198303"
};
$.each(newUserDetail, function(k, i) {
if (i == tm.UserId) {
alert("User ID match");
}
})
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
How can I change the name of a node in my JSON?
My code:
childType = view
childName = view0
child=[];
child[childTipy]= {
childType:{
"tipo": childTipy,
"nome":childName,
}
};
childList.push(child[childTipy]);
minhasWindows = {"window": {
"nome": "Win2",
"title": "Win",
"childrens": childList
}
};
The resulting JSON:
{
"windows" : [
{
"window" : {
"nome" : "Win2",
"title" : "Win",
"childrens" : [
{
"childType" : {
"tipo" : "view",
"nome" : "view0"
}
}
]
}
}
]
}
I want the childType node to be the value of my var childType = "view". How can I change this?
PS: I have multiple childType values.
If you want the property to come from a variable, use array syntax to assign the property.
var childType = "view"; // String needs to be quoted
var childName = "view0"; // String needs to be quoted
var child = {}; // This must be an object, not array
child[childType] = {
tipo: childType, // Don't need to quote keys in object literals
nome: childName,
};
childList.push(child);
You also had too many levels in your child object.
Change this
childType:{
to
view:{
Replace
child=[];
child[childTipy]= {
childType:{
"tipo": childTipy,
"nome":childName,
}
};
with
child=[];
child[childTipy]= {};
child[childTipy][childType] = {
"tipo": childTipy,
"nome":childName,
};
If childType appears in the keys only:
var someJson = '{"windows":[{"window":{"nome":"Win2","title":"Win","childrens":[{"childType":{"tipo":"view","nome":"view0"}}]}}]}';
var newJson = $.parseJSON(someJson.replace('childType', 'view'));
Though I don't see any actual need of changing a key in place.