2

I am trying to learn/use Node and Javascript by re-coding a PHP/MYSQL application I made that is not very efficient - as I built it while I was trying to earn those technologies as well.

I am trying to re-use a some of the information that is created or stored in JSON format.

I have in one file a set of 52 questions that looks like such...

//questions.json
{
  "School" : {
    "Question 1": "The facilities of the school adequately provide for a positive learning/working experience.",
    "Question 2": "School provides adequate access to teaching materials without any extra expense by the teacher (books, lab supplies, art supplies, materials, copying, etc.",
    "Question 3": "School is well funded to provide students and faculty with adequate materials to support their course offering.",
    "Question 4": "Parent community is actively involved in supporting the school's mission and their child's education endeavors.",
    "Question 5": "Classroom student to teacher ratios are kept low."
  },
  "Students" : {
    "Question 6": "Students are generally of high aptitude.",
    "Question 7": "Student interactions with faculty are primarily characterized by respectability.",
    "Question 8": "Students are of diverse ethnicity."
  },
  "Administration" : {
    "Question 9": "Administration positively supports faculty in a professional manner.",
    "Question 10": "Administration actively encourages a dialogue concerning school related issues.",
    "Question 11": "Administration is actively engaged in developing/supporting/maintaining a clear school vision.",
    "Question 12": "Administration makes sure that the financial stability and integrity of the school is well maintained.",
    "Question 13": "Administration/Ownership uses school funds to support the well being of the school."
  },...

I also have a Knex script that does a query on the data and returns an array like below....

[ { Q1: 8.44,
    Q2: 9.13,
    Q3: 8.81,
    Q4: 8.38,
    Q5: 7.63,
    Q6: 8.06,
    Q7: 8.56,
    Q8: 8.5,
    Q9: 7.63,
    Q10: 8.13,
    Q11: 8.5,
    Q12: 8.88,
    Q13: 8.75,
    Q14: 7.38,
    Q15: 8.13,
    Q16: 8.56,...

I would like to try to combine these two arrays to look like...

{
  "School" : {
      {"Question 1" : {
          "Question" : "The facilities of the school adequately provide for a positive learning/working experience.",
          "Average" : 8.44
            },
      {"Question 2" : {
          "Question" : ""School provides adequate access to teaching materials without any extra expense by the teacher (books, lab supplies, art supplies, materials, copying, etc."",
          "Average" : 9.13
            }

If need be I can remove the category divisions within the .json file if that make combining easier, but in the end applications I display the data in sections, so I will eventually filter it. I just thought maybe dividing it in the json file would make it easier.

I am able to concat the arrays, and I have looked at .map but I am not sure how I would apply it to this. It looks like I would need a nested loop?

2 Answers 2

1

Using a double forEach loop:

const questions = {
  "School" : {
    "Question 1": "The facilities of the school adequately provide for a positive learning/working experience.",
    "Question 2": "School provides adequate access to teaching materials without any extra expense by the teacher (books, lab supplies, art supplies, materials, copying, etc.",
    "Question 3": "School is well funded to provide students and faculty with adequate materials to support their course offering.",
    "Question 4": "Parent community is actively involved in supporting the school's mission and their child's education endeavors.",
    "Question 5": "Classroom student to teacher ratios are kept low."
  },
  "Students" : {
    "Question 6": "Students are generally of high aptitude.",
    "Question 7": "Student interactions with faculty are primarily characterized by respectability.",
    "Question 8": "Students are of diverse ethnicity."
  }
};

const scores = {
  Q1: 8.44,
  Q2: 9.13,
  Q3: 8.81,
  Q4: 8.38,
  Q5: 7.63,
  Q6: 8.06,
  Q7: 8.56,
  Q8: 8.5
};

// Placeholder for your result
const result = {};

// Iterate through the question categories:
const categories = Object.keys(questions);

categories.forEach(cat => {
  // Add category to result
  result[cat] = {};
  
  // Get the question keys for that category
  const questionKeys = Object.keys(questions[cat]);
  
  questionKeys.forEach(q => {
    // Get the score key. i.e: Question 1 -> Q1
    const scoreKey = q.replace('Question ', 'Q');
    
    // Add question to result
    result[cat][q] = {
      Question: questions[cat][q],
      Average: scores[scoreKey]
    };
  });
});

console.log(result);

Sign up to request clarification or add additional context in comments.

Comments

1

What I do below is basically:

  1. I take the question number out of "Question 1"
  2. Combine it with "Q" to get "Q1"
  3. And then the plain old key lookup in the object you have based on this new string.

All this, in loops below

var newObj = {};
// replace /*questions.json*/ with your actual object.
for (var category in /*questions.json*/){
    var questionsObj = /*questions.json*/[category];
    newObj[category] = {}
    for (var question in questionsObj) {
        newObj[category][question] = {};
        newObj[category][question].question = questionsObj[question];

        var qNumber = question.split("Question ")[1];
        newObj[category][question].Average = dataArray[0]["Q"+qNumber];
    }
}

1 Comment

Thank you for your help! Both of your responses helped immensely, but I was able to modify the other response to work with what I needed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.