0

I am trying to take JSON data information from a separate file and insert that info into a newly created object. I know there are a lot of similar questions, but how do you do this with Node JS? I've tried just about all the options from the following link with no success

Add new element to an existing object

Ex:

"EXTERNAL-DATA" : [
  {
    "clients" {
        {
            "firstName" : "Thomas",
            "lastName" : "Johnson",
            "profession" : "Carpenter",
            "age": 27,
            "education": {
               "Diploma": true
            }
        },
        {
            "firstName" : "Jane",
            "lastName" : "Simpson",
            "profession" : "Teacher",
            "age": 32,
            "education": {
                "Masters Degree": true,
                "College Degree": true,
                "Diploma": true
            }
        }
    }
  }
]

If I only want to capture, say firstName, lastName, and education and place each client in a new, empty object collectedData as their own individual objects, how would I go about doing that? Assuming the EXTERNAL DATA is held inside a variable called data

  1. I've tried the spread operator collectedData = {... collectedData[i], ...data[i]}, but it doesn't add each as new objects
  2. I've tried using new Object(); each time I loop through, but I'm not sure how the left-hand side is supposed to look when there are thousands of objects from the external JSON file.
  3. I've tried Object.assign(), but with no luck

I know I'm overthinking it, but does anybody have an idea even for what direction to go in? I feel burned out.

Would eventually look like this:

collectedData = {
  {
    "firstName": "Thomas",
    "lastName": "Johnson",
    "education": {
        "Diploma": true
     }
  },
  {
    "firstName": "Jane",
    "lastName": "Simpson",
    "education": {
        "Masters Degree": true,
        "College Degree": true,
        "Diploma": true
    }
  }
}
0

1 Answer 1

1

You can use Array.map() to go through the clients array (which, looks like a typo in your code... I'm assuming it's an array) and get the object the way you want it.

const collectedData = clients.map((client) => {
  return {
    firstName: client.firstName,
    lastName: client.lastName,
    education: client.education
  };
});

This basically loops through each client and creates a new object for each one with only the properties you want. Then, a whole new array is returned and assigned to collectedData.

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

4 Comments

does .map only work with arrays? If you have several objects inside an array, can you still use .map?
@Mitchell The map is for the array... it doesn't matter what's in that array.
Can you pass a function as an argument, like instead of clients.map((client) => we could do something like clients.map((getClients()) => ?
@Mitchell Yes, what you see there is simply an anonymous function. You can pass in any other function.

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.