-2

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.

hch
  • 717
  • 1
  • 6
  • 18
  • your error is here: `if (!isEmpty(preference.trim())) userPreferences.category_preference_name = preference;` should be using bracket notation like so: `if (!isEmpty(preference.trim())) userPreferences[category_preference_name] = preference;` you can't access a dynamic property using dot syntax, you have to use bracket syntax – r3wt Dec 10 '20 at 15:43
  • Does this answer your question? [Dynamically access object property using variable](https://stackoverflow.com/questions/4244896/dynamically-access-object-property-using-variable) – r3wt Dec 10 '20 at 15:45
  • thank you for helping, i add an example – hch Dec 10 '20 at 15:46

1 Answers1

1

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))
Michael Beeson
  • 2,840
  • 2
  • 17
  • 25
  • thank you for help and for your advice, you right, in the nest time i'll use snippet – hch Dec 10 '20 at 15:57