How do you pass object in React Native? - javascript

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 });

Related

Remove parent from object

I am trying to remove the parent of an object like this:
const object = {
"id": {
"key":"value"
}
}
I wanna get:
object = {
"key":"value"
}
Anyone knows how to achieve this?
You have to use let or another variable
let object = {
"id": {
"key":"value"
}
}
// ...
object = object.id
// or
const anotherVariable = object.id

Why does vue.js alter my JSON data?

I want to return data from my API and display it in a vue.js project.
The data is:
{
"pages":[
{
"id":3,
"slug":"over-mij",
"updated_at":"2017-12-25T11:16:21+01:00",
"created_at":"2017-12-25T10:56:21+01:00",
"title":"Over mij",
"content":""
},
{
"id":6,
"slug":"mijn-cv",
"updated_at":"2017-12-25T11:07:29+01:00",
"created_at":"2017-12-25T11:07:29+01:00",
"title":"Mijn cv",
"content":null
}
],
"current_user":null
}
Then I'm using vue-resource to load the data from my API page in a seperate module of vuex:
const actions = {
initPages({commit}) {
Vue.resource('pages').get({}).then(function(response) {
if (response) {
response.json().then(data => {
state.pages = data;
});
}
});
}
}
The problem however is when I check what's inside the state.pages, this contains:
{
"pages": {
"pages": [
{
"id": 3,
"slug": "over-mij",
"updated_at": "2017-12-25T11:16:21+01:00",
"created_at": "2017-12-25T10:56:21+01:00",
"title": "Over mij",
"content": ""
},
{
"id": 6,
"slug": "mijn-cv",
"updated_at": "2017-12-25T11:07:29+01:00",
"created_at": "2017-12-25T11:07:29+01:00",
"title": "Mijn cv",
"content": null
}
],
"current_user": null
}
}
For some weird reason my JSON response is altered, I've already tried setting state.pages to data.pages and this just eliminates the first pages object but not the second one.
When I set my state.pages to data.pages[0] it does work, does anyone know why this is happening?
Even when I use mutations, I still have the same issue:
module of store.js:
import Vue from 'vue';
const state = {
pages: []
}
const mutations = {
'setPages' (state, pages) {
state.pages = pages;
}
}
const getters = {
pages(state) {
return state.pages;
},
page(state) {
return keyword => state.pages.find(function(page) {
return page.slug === keyword;
});
}
}
const actions = {
initPages({commit}) {
Vue.resource('pages').get({}).then(function(response) {
if (response) {
response.json().then(data => {
commit('setPages', data.pages);
});
}
});
}
}
export default {
state,
getters,
mutations,
actions
}

How to verify the particular value of the DB node of firebase database

I have a JSON structure as
{
"engagedUsers": {
"qwe": {
"agent_availability": true,
"conv_id": "parthengineer_qwe",
"emailId": "qwe#gmail.com",
"login_time": 1512644440,
"name": "qwe",
"uid": "adasfklsfjdskldgsgjgjkl"
},
"tt": {
"agent_availability": true,
"conv_id": "genesis_tt",
"emailId": "tt#gmail.com",
"login_time": 1512644440,
"name": "tt",
"uid": "adasfklsfjdskldgsgjgjkl"
}
}
}
I want to verify if the conv_id child is equal to parthengineer_qwe. I tried the below code but cannot got through it:
this.engagedRef = firebase.database().ref('engagedUsers');
this.engagedRef.orderByValue().once('value', function (snapshot) {
snapshot.forEach(function (data) {
// console.log("snapShot:" + data.child('conv_id').val());
if ((data.child('conv_id').val() == 'parthengineerqwe') && (existEngageduser !== 1)) {
existEngageduser = 1
// console.log("under haschild:" + existEngageduser);
}
if (existEngageduser == 1) {
isEngaged = true
// sessionStorage.isEngaged = isEngaged;
}
// console.log("isEngaged:" + isEngaged);
})
return isEngaged;
});
Please suggest a better way to achieve this as I know I am not going right way, I expect isEngaged variable as true
I'm still not sure what you want but why not have an external function to do it?
snapshot.forEach(function (data) {
if (isEngaged(data.val())) {
console.log(data.key, ' is engaged');
}
}
function isEngaged(userData) {
return (userData.conv_id === 'parthengineer_qwe') || false;
}

Get the closest match in a string

I have a json file with some domains which I try to match in a foreach object function.
JSON-File:
"calendar-google": {
"domainMatch": "calendar.google",
"icon": "assets/icons/google-calendar.png"
},
"inbox-google": {
"domainMatch": "inbox.google",
"icon": "assets/icons/google-gmail.png"
},
"github": {
"domainMatch": "github",
"icon": "assets/icons/github.png"
},
"google": {
"domainMatch": "google",
"icon": "assets/icons/google-google.png"
},
"trello": {
"domainMatch": "trello",
"icon": "assets/icons/trello.png"
},
"tweetdeck": {
"domainMatch": "tweetdeck.twitter",
"icon": "assets/icons/twitter.png"
},
"twitter": {
"domainMatch": "twitter",
"icon": "assets/icons/twitter.png"
},
"youtube": {
"domainMatch": "youtube",
"icon": "assets/icons/youtube.png"
}
Now I want to check if my url in my local storage does match with one of these "domainMatch" properties.
JavaScript:
$.getJSON("domainList.json", function (data) {
Object.getOwnPropertyNames(data).forEach(function(val, idx, array) {
var domainMatch = data[val].domainMatch
if(localStorage.getItem("favouriteSite")) {
var sites = JSON.parse(localStorage.getItem("favouriteSite"));
// Lets think firstlink is "calender.google.com"
var firstLink = sites.site1.link;
if(firstLink.includes(domainMatch)){
// 2 Results but I want only that
// result which is near!!
}
}
});
You see in the comment, that I not want the match "google" and "calender.google". I want only the nearest match from both (calender.google in this case).
How can I get the nearest match of a string?
When I did not write something detailed enough, then please write it but I think you should understand what I mean.
Greetings
Julian
Use Array.prototype.some() function to return the result immediately on the first match:
$.getJSON("domainList.json", function (data) {
var matched = Object.keys(data).some(function(k) {
var domainMatch = data[k].domainMatch;
if (localStorage.getItem("favouriteSite")) {
var sites = JSON.parse(localStorage.getItem("favouriteSite"));
// Lets think firstlink is "calender.google.com"
var firstLink = sites.site1.link;
return firstLink.includes(domainMatch);
}
};
});
Instead of forEach, use find to get the first match or null if nothing is matched like this:
$.getJSON("domainList.json", function(data) {
var sites = JSON.parse(localStorage.getItem("favouriteSite")); // this should be out here (so you won't fetch it evertime)
var firstLink = sites.site1.link;
var val = Object.keys(data).find(function(key) { // find will stop at the first found item (Object.keys) is better since it's just a plain object)
var domainMatch = data[key].domainMatch;
return firstLink.includes(domainMatch);
});
console.log(val); // val will be the first matched key ('github', 'google', ...) or null if nothing is matched
/***************** EDIT: **********************/
if(val) { // if we found something
var obj = data[val]; // get the object (because val is the key)
var icon = obj.icon; // now obj is an object from the array data (you can access its icon property like this)
}
});

How can I use a string to access an object property?

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;
};

Categories

Resources