-2

In the function there is an empty JS object

var globalDataObject = [{}]; 

Then, there is a loop that goes over an array of users, stores the properties of each user in variables (ex. name, lastName, ...) and creates an object for each user:

//create an object containing the current name 
const currentObject = {
    'profile': {
        'name': nameVariable,
        'lastName': lastNameVariable
    }
};

What is the right way to add the data of currentObject to the globalDataObject once it's created? So that at the end the globalDataObject should be:

var globalDataObject = [
    'profile': {
        'name': 'John',
        'lastName': 'Smith'
    },
    'profile': {
        'name': 'Ann',
        'lastName': 'Lee'
    },
    'profile': {
        'name': 'Dan',
        'lastName': 'Brown'
    }
]; 

Important thing is that globalDataObject must be the JS object of the specified format (not the object containing multiple objects and not the array) since once it's created it is going to be converted into XML.

Ralf
  • 16,086
  • 4
  • 44
  • 68
a.tokayeva
  • 77
  • 1
  • 13
  • 2
    Your `globalDataObject` is not proper JavaScript; if you want an array of profiles then create one, if you want an object containing profiles you can't have each object keyed under `profile`. If you want an array of profiles then "adding a new one" is as simple as that: concatenate the new one. It's not clear to me what the issue is. – Dave Newton Aug 13 '18 at 12:55
  • 1
    I don't understand the problem. Why not just `.push()` each created object to the global array? Or even `globalDataObject = source_array.map(( name, lastName) => ({ "profile": { name, lastName }}));` – Shilly Aug 13 '18 at 12:55
  • There is no JSON in this question – Liam Aug 13 '18 at 12:56
  • Possible duplicate of [Access / process (nested) objects, arrays or JSON](https://stackoverflow.com/questions/11922383/access-process-nested-objects-arrays-or-json) – Liam Aug 13 '18 at 12:58
  • @DaveNewton an object containing profiles is exactly what I need, is there a way to get just the values (starting from 'profile') from the currentObject and add them to globalDataObject? – a.tokayeva Aug 13 '18 at 13:06
  • @at01 You may have missed where I said that `globalDataObject` is broken. (1) The first declaration is an array with a single empty object. (2) The second example is syntactically (array entries don't have keys) and logically invalid (the keys should be different or they'll just overwrite, and in some cases may not transpile at all). – Dave Newton Aug 13 '18 at 13:50

2 Answers2

2

You can create your global object like an array:

globalDataObject = []; 

And then just push in it:

globalDataObject.push(currentObject);
Daria Pydorenko
  • 1,754
  • 2
  • 18
  • 45
1

I dont understand the end goal of the question and why you dont just use .push() as suggested before. You havent accepted that answer so i assume its not the end goal.

globalDataObject must be the JS object of the specified format (not the object containing multiple object and not the array)

1) The format you gave is not valid JavaScript. 2) Why cant you have an array of objects or object with nexted objects and covert that into xml 3) Why do you want to convert json into xml in the first place.

I'll take a wild guess and assume you mis-typed the globalDataObject as array, and meant it to be an object with multiple 'profile' keys. Neither is valid javascript.

Since you can't have multiple keys with same name and expect them to have different values, I propose you to use unique "indexes" for each profile.( like an array...but an object).

// init the object
const userProfiles = {};

// then later add to it like this.
let profile1 = {name: "john", lastname: "smith"};
let profile2 = {name: "alice", lastname: "wonderland"};
userProfiles[1] = profile1;
userProfiles[2] = profile2;
// you can then torn it into an array of user profile objects like this
Object.keys(userProfiles).map((index) => {return userProfiles[index];})
Rainer Plumer
  • 3,693
  • 2
  • 24
  • 42