Hi I'm new to React and nodejs. I get from the user his preferences for certain categories in the json code for example:
{
"sport" : {
"tennis": "5",
"running": "4",
"swimming": "5"
},
"study" : {
"history" : "0"
}
}
I want for each preference to create its own field in the "userPreferences" object.
This is the code I wrote down but I can not see what the error is here
exports.reduceUserPreferences = (data) => {
let userPreferences = {};
data.forEach(category => {
category.forEach(preference => {
category_preference_name = category.string + "_" + preference.string;
if (!isEmpty(preference.trim())) userPreferences.category_preference_name = preference;
});
});
//if (!isEmpty(data.tennis.trim())) userPreferences.sport_tennis = data.tennis;
//if (!isEmpty(data.swimming.trim())) userPreferences.sport_swimming = data.swimming;
//if (!isEmpty(data.running.trim())) userPreferences.sport_running = data.running;
//if (!isEmpty(data.history.trim())) userPreferences.study_history = data.history;
return userPreferences;
};
I want the "" object to have fields of all preferences along with the category to which they belong.
I can not understand what I am doing wrong, I linger on this code for several hours.
add example
I have another function similar to this function, the input is similar and the output is similar.
For example input:
{
"bio": "hello there",
"website": "",
"location": "los angles"
}
Example function:
exports.reduceUserDetails = (data) => {
let userDetails = {};
if (!isEmpty(data.bio.trim())) userDetails.bio = data.bio;
if (!isEmpty(data.website.trim())) {
// https://website.com
if (data.website.trim().substring(0, 4) !== 'http') {
userDetails.website = `http://${data.website.trim()}`;
} else userDetails.website = data.website;
}
if (!isEmpty(data.location.trim())) userDetails.location = data.location;
return userDetails;
};
The output will be:
An object with the attribute of all preferences along with their value.
I was looking for examples with a nested loop, I could not find.
There are a couple of things you need to fix in your code.
First, when using a variable name as the key to extract from an object, user obj[varName], not obj.varName.
(read more here: Dynamically access object property using variable)
Also, you're trying to loop an Object, not an array. To loop through the keys, use Object.keys()
Combining these two things you get the desired result, as you can see in this snippet. Also, for future questions, I highly recommend you make a snippet yourself.
const jsonOriginal = {
"sport" : {
"tennis": "5",
"running": "4",
"swimming": "5"
},
"study" : {
"history" : "0"
}
}
const reduceUserPreferences = (data) => {
let userPreferences = {};
Object.keys(data).forEach(category => {
Object.keys(data[category]).forEach(preference => {
category_preference_name = category + "_" + preference;
const preferenceValue = data[category][preference].trim();
if (preferenceValue !== '') userPreferences[category_preference_name] = preferenceValue;
});
});
return userPreferences;
};
console.log(reduceUserPreferences(jsonOriginal))
Related
So I am pretty new when it comes to Javascript and it is as simple as read a json list with a value of:
{
"URL": [{
"https://testing.com/en/p/-12332423/": "999"
}, {
"https://testing.com/en/p/-123456/": "123"
},
{
"https://testing.com/en/p/-456436346/": "422"
}
]
}
What I would like to do is to have both the URL and the amount of numbers etc
"https://testing.com/en/p/-12332423/" and "999"
and I would like to for loop so it runs each "site" one by one so the first loop should be
"https://testing.com/en/p/-12332423/" and "999"
second loop should be:
"https://testing.com/en/p/-123456/" and "123"
and so on depending on whats inside the json basically.
So my question is how am I able to loop it so I can use those values for each loop?
As Adam Orlov pointed out in the coment, Object.entries() can be very useful here.
const URLobj = {
"URL": [{
"https://testing.com/en/p/-12332423/": "999"
}, {
"https://testing.com/en/p/-123456/": "123"
},
{
"https://testing.com/en/p/-456436346/": "422"
}
]
};
URLobj.URL.forEach(ob => {
console.log('ob', ob);
const entries = Object.entries(ob)[0]; // 0 just means the first key-value pair, but because each object has only one we can just use the first one
const url = entries[0];
const number = entries[1];
console.log('url', url);
console.log('number', number);
})
You mean something like this using Object.entries
const data = {
"URL": [
{"https://testing.com/en/p/-12332423/": "999"},
{"https://testing.com/en/p/-123456/": "123"},
{"https://testing.com/en/p/-456436346/": "422"}
]
}
data.URL.forEach(obj => { // loop
const [url, num] = Object.entries(obj)[0]; // grab the key and value from each entry - note the [0]
console.log("Url",url,"Number", num); // do something with them
})
let's call your object o1 for simplicity. So you can really go to town with this link - https://zellwk.com/blog/looping-through-js-objects/
or you can just use this code :
for(var i = 0; i < o1.URL.length; i++) {
//each entry
var site = Object.keys(URL[i]) [0];
var value = Object.values(URL[i]) [0];
// ... do whatever
}
don't forget each member of the array is an object (key : value) in its own right
You can extract the keys and their values into another object array using map
Then use the for loop on the newly created array. You can use this method on any object to separate their keys and values into another object array.
const data = {
"URL": [{
"https://testing.com/en/p/-12332423/": "999"
}, {
"https://testing.com/en/p/-123456/": "123"
},
{
"https://testing.com/en/p/-456436346/": "422"
}
]
}
var extracted = data.URL.map(e => ({
url: Object.keys(e)[0],
number: Object.values(e)[0]
}))
extracted.forEach((e) => console.log(e))
The Problem:
I have this function. Which removes all KeyValue Pairs that have an Empty String as value from a Payload.
The problem is, that I want to apply it in an Object that is Nested. Not the whole Payload Object. Example:
configuration: {
conf1: "",
conf2: "Something",
conf3: ""
},
resourceName: "Name"
In this case I want to apply my UtilityFunction, in the configurationObject. Which would result in this:
configuration: {
conf2: "Something",
},
resourceName: "Name"
So, I used a few Methods. Object.assign, rest, in order to supply an object with all the outside parameters, but also, the output of the utility applied to just the configuration object.
I tried:
Object.assign(formValues, removeEmptyKeysUtil(formValues.configuration));
// Results in printing the values in the main object.
Also:
{ formValues, ...removeEmptyKeysUtil(formValues.configuration) };
// which does not do anything
Can you please help me, and explain what am I doing wrong?
The stripEmptyStrings function takes the original object and the target key.
this function can also handle if the target property of the object is "" and will delete that property regardless of if it is an Object or not.
const stripEmptyStrings = (object, target) => {
const _target = object[target];
if (_target === "") {
delete object[target]
return object;
}
else if (typeof _target === "object") {
Object.keys(_target).forEach((k) => {
if (_target[k] === "") delete _target[k];
})
}
return {
...object,
[target]: _target,
}
}
const obj1 = {
configuration: {
conf1: "",
conf2: "Something",
conf3: ""
},
resourceName: "Name",
}
const result1 = stripEmptyStrings(obj1, "configuration");
console.log(result1)
const obj2 = {
removeMe: "",
resourceName: "Name2",
}
const result2 = stripEmptyStrings(obj2, "removeMe");
console.log(result2)
Here is my code :
Search.prototype.makeQuery = function (data) {
let result = {};
if (data.orderId) {
result["order_id"] = data.orderId;
}
if (data.userMobileNumber) {
result["user.Number"] = {$regex : data.userMobileNumber}
}
if (data.userFullName) {
result["user.Name"] = {$regex: data.userFullName}
}
return result;
};
All I want is finding better way to optimize my code and reduce if condition in my code. Is there any suggestion ?
You can avoid the typing of if when you wrap it into a function and the typing of data with destructuring.
The advantage of wrapping the if in this case into a function is that you can simply test it, it is reusable and easy to read
Code
Search.prototype.makeQuery = function (data) {
let result = {}
let {orderId, userMobileNumber, userFullName} = data
setObjectValue(orderId, result, "order_id", orderId)
setObjectValue(userMobileNumber, result, "user.Number", {$regex : userMobileNumber})
setObjectValue(userFullName, result, "user.Name", {$regex: userFullName})
return result;
}
function setObjectValue(condition, object, key, value) {
if(condition) {
object[key] = value
}
}
Working Example
function makeQuery (data) {
let result = {}
let {orderId, userMobileNumber, userFullName} = data
setObjectValue(orderId, result, "order_id", orderId)
setObjectValue(userMobileNumber, result, "user.Number", {$regex : userMobileNumber})
setObjectValue(userFullName, result, "user.Name", {$regex: userFullName})
return result;
}
function setObjectValue(condition, object, key, value) {
if(condition) {
object[key] = value
}
}
let data = {
orderId: 1,
userMobileNumber: "016875447895",
userFullName: "John Doe"
}
let query = makeQuery(data)
console.log(query)
A simpler way:
Search.prototype.makeQuery = function (data) {
let result = {};
data.orderId && (result["order_id"] = data.orderId);
data.userMobileNumber && (result["user.Number"] = {$regex : data.userMobileNumber});
data.userFullName && (result["user.Name"] = {$regex: data.userFullName});
return result;
};
Let's imagine you have many fields or you want to modify them, you would create a map. Right now, your code works and my solution is overkill but it may be useful in the future:
const interestingData = new Map()
//I tried to imitate your values.
// Use whatever function you want here as a callback.
// value is the value you request, the callback must return the value you want to set.
interestingData.set("order_id", value => value)
interestingData.set("user.Number", value => ({ $regevalue: value }))
interestingData.set("user.Name", value => ({ $regevalue: value }))
//Tgis is a Factory in case you need several search.
const makeSearch = fields => data => {
let result = {}
fields.forEach((callBack, field) => {
if (data[field])
result[field] = callBack(data[field])
})
return result
}
//Creating a searching function
const myResearch = makeSearch(interestingData)
//Fake examples
const data1 = {
order_id: 'ertyui',
"user.Number": "ertyuio",
"user.Name": "ertyuio",
azerr: 123456
}
const data2 = {
order_id: 'ertyui',
"user.Number": "ertyuio",
}
console.log(myResearch(data1))
console.log(myResearch(data2))
It is not simpler but it is more extensible, and when you have many parameters, it is going to be much faster on a big scale. It is also reusable. Hope that helps!
Not sure if you'd consider this as code optimization, but you can get rid of the if statements using Object.assign:
Search.prototype.makeQuery = function (data) {
return Object.assign({},
data.orderId && { order_id: data.orderId },
data.userMobileNumber && {
'user.Number': { $regex : data.userMobileNumber },
},
data.userFullName && {
'user.Name': { $regex : data.userFullName },
},
)
};
If you can use newer JS features (with a transpiler or otherwise), you could use Object rest/spread for a slightly more concise syntax:
Search.prototype.makeQuery = (data) => ({
...data.orderId && { order_id: data.orderId },
...data.userMobileNumber && {
'user.Number': { $regex : data.userMobileNumber },
},
...data.userFullName && {
user.Name': { $regex : data.userFullName },
},
});
Edit 1: note that all these are pure functions, no mutations are taking place whatsoever
My JSON is like:
{
"boundaries": [
{
"boundary": {
"boundaryId": "45083021141",
"boundaryType": "USA_POSTCODE",
"boundaryRef": "B1"
}
}
],
"themes": [
{
"TheftCrimeTheme": {
"boundaryRef": "B1",
"individualValueVariable": [
{
"name": "2013 Theft Crime",
"description": "Theft Crime for 2013",
"count": 2080
}
]
}
},
{
"BurglaryCrimeTheme": {
"boundaryRef": "B1",
"individualValueVariable": [
{
"name": "2013 Burglary Crime",
"description": "Burglary Crime for 2013",
"count": 302
}
]
}
}
]
}
I want to get value of count to display in graph. As you can see in the above json, inside themes there are two keys i.e TheftCrimeTheme and BurglaryCrimeTheme. I want to get value of count in everycrimetheme. For this I did the below code:
$http.get("http://152.144.218.70:8080/USACrime/api/crimeAPI?city="+$scope.strCity+"&crimeType="+$scope.type1+"&years="+$scope.type+"&month="+$scope.type2).success(function (result) {
for(var i=0;i<result.themes.length;i++){
var crime={};
console.log("did",result.themes[i]);
var test2 = result.themes[i];
console.log("test2",test2);
var test = test2[Object.keys(test2)];
console.log("test",test);
crime.name = Object.keys(result.themes[i]);
console.log("CrimeName",crime.name);
crime.data = [];
var test1 = test.individualValueVariable[0].count;
console.log("test1",test1);
crime.data.push(test1);
crime_data.push(crime);
}
});
My agenda is to plot graph showing count of every year.To achieve this firstly I have to get multiple keys like TheftCrimeTheme, BurglaryCrimeTheme etc. Then after that i can access value of count in individualValueVariable.
when I used Object.keys() method , I got an error "undefined" when i console values of nameR. Please suggest me how should i do this?
This function recieves info (as the whole json), and theme as the theme which you want to get the count (ie.: "BurglaryCrimeTheme").
getThemeCrimesCount = (info, theme) => {
const record = info.themes.find(obj => Object.keys(obj)[0] == theme)[theme];
return record.individualValueVariable.reduce((a, b) => a += b.count, 0);
}
getThemeCrimesCount(info, "BurglaryCrimeTheme"); // 302
getThemeCrimesCount(info, "TheftCrimeTheme"); // 2080
Formatting this to separate the elements for clarity.
// Builds and returns URL with query string attached.
const buildURL = (uri, params) => {
let queryParams = Object.keys(params).map(function(k) {
return encodeURIComponent(k) + '=' + encodeURIComponent(params[k])
}).join('&');
return uri + '?' + queryParams;
};
// Parses returned JSON object.
const parseStatistics = (result) => {
// My assumption here is that you are receiving a JSON string, which
// would need to be parsed into an object before being used.
let result = JSON.parse(result);
// Set base object
let namR = {};
// Iterate through themes array.
result.themes.forEach(function(element) {
// Find the object name for current theme.
let type = Object.keys(element)[0];
// Find count for current theme.
let count = element[type].individualValueVariable.count;
// Set count.
namR[type] = count;
});
// Log object
console.log(namR);
};
// Set up url info.
let params = {
city: $scope.strCity,
crimeType: $scope.type1,
years: $scope.type,
month: $scope.type2
};
let baseURL = "http://152.144.218.70:8080/USACrime/api/crimeAPI";
// Execute request.
$http.get(buildURL(baseURL, params)).success(parseStatistics(response));
I have an array. Pretty simple, like: [32652,24164] both of which numbers are stored productIDs
I have an object storing data associated with each productID
var products = {
"products": [{
"productID": "32652",
"name": "Playstation 4",
"price": "109.99"
}, {
"productID": "24164",
"name": "Xbox",
"price": "129.99"
}]
};
I'm trying to find the associated name and price in the object along with the productID by using .filter
I have the following function:
function findProductData(productID) {
var foundProductArray = [];
var foundProduct = products.products.filter(x => x.productID === productID)[0];
console.log(foundProduct); // THIS IS COMING BACK UNDEFINED
if (foundProduct) {
// This function is never running as the filter is coming back UNDEFINED
console.log('Something found');
var foundName = foundProduct.name;
var foundPrice = foundProduct.price;
foundProductArray.push(foundName, foundPrice);
return foundProductArray;
} else {
console.log('Nothing found');
}
}
This is the particular line of code that is not working. This should return a result, but it's coming back undefined
var foundProduct = products.products.filter(x => x.productID === productID)[0];
So the function is never running and never finding the associated name and price.
I've put together a JS Fiddle
You missed a parseInt. You are trying to strictly compare a string and an int
.filter(x => x.productID === productID)[0]
Change that to this
.filter(x => parseInt(x.productID) === productID)[0]