skip to Main Content

I would like to create a complex Javascript variable like this:

var e = {record: {role: {individual: {tag: value}}}};

where record, role, individual, and value are variables which come from an array.
At the end it should look like this:

var e = {1: {2: {1: {name: Joseph Quincy}
                    {age: 20}    

                 2: {name: Mary Poppins}
                    {age: 40}

                 3: {name: Sebastian Quincy}
                    {age: 60}

            {3: {1: ... and so forth

The array was derived from data as shown below, where the first number is record, second is role, and third is individual, the tag is name and age and the value is after the space:

name_1_2_1 Joseph Jones
name_1_2_2 Mary Poppins
name_1_2_3 Sebastian Quincy
age_1_2_1 20
age_1_2_2 40
age_1_2_3 60

The arrays look like for name_1_2_1 Joseph Jones:

key_value_ary = (name_1_2_1, Joseph James);
tag_rri_ary = (name, _1_2_1);
r_r_i_ary = (1, 2, 1);

Hence

value      = key_value_ary[1]; -> Joseph Jones
tag        = tag_rri_ary[0];   -> name
record     = r_r_i_ary[0];     -> 1
role       = r_r_i_ary[1];     -> 2
individual = r_r_i_ary[2];     -> 1

I have all these objects created but I don’t know how to write to the final variable "e".
I have the code below that figure out the different values. I write one line at a time so the record may exist already from a previous entry.

if (r_r_i_ary.length == 3) {
        var tag = tag_rri_ary[0];
        console.log("TAG", tag);
        var value = key_value_ary[1];
        console.log("VALUE", value);
        const a = {};
        a[tag] = value; // tag value
        console.log("AAA", a);
        const b = {};
        var individual = r_r_i_ary[2]; // individual
        b[individual] = a;
        console.log("BBB", b);
        const c = {};
        var role = r_r_i_ary[1]; // role
        c[role] = b;
        console.log("CCC", c);
        const d = {};
        var record = r_r_i_ary[0]; // record
        d[record] = c;
        console.log("DDD", d);
        e=????? 
      }

I obtained this from the console as one of the examples but I need to concatenate all together in one variable:

{
    "4": {
        "1": {
            "2": {
                "will_plac": "Atibaia, São Paulo, Brasil"
            }
        }
    }
}

How do I do that?

Thanks!

2

Answers


  1. // Will use named capturing groups to parse each line
    const lineRegex = new RegExp(
      "^(?<tag>\w+)" +        // Match and capture the tag in the beginning of the line (word characters)
      "_(?<record>\d+)" +     // Match and capture the record (digits)
      "_(?<role>\d+)" +       // Match and capture the role (digits)
      "_(?<individual>\d+)" + // Match and capture the individual (digits)
      "\s+" +                 // Match one or more whitespace characters
      "(?<value>.+)$"          // Match and capture the value (any characters until the end of the line)
    );
    
    function parse(input) {
      return input
        // get each line separately
        .split('n')
        // parse it with a regex
        .map((line) => line.match(lineRegex).groups)
        // reduce the parsed lines to the required shape
        .reduce((agg, parsed) => {
          const out = {...agg};
          if (typeof out[parsed.record] === 'undefined') {
            // Make sure there is a record to append to
            out[parsed.record] = {};
          }
          if (typeof out[parsed.record][parsed.role] === 'undefined') {
            // Make sure there is a role in the record to append to
            out[parsed.record][parsed.role] = {};
          }
          if (typeof out[parsed.record][parsed.role][parsed.individual] === 'undefined') {
            // Make sure there is an individual to assign values to
            out[parsed.record][parsed.role][parsed.individual] = {};
          }
          
          // Store the actual value for the tag
          out[parsed.record][parsed.role][parsed.individual][parsed.tag] = parsed.value;
          return out;
        }, {});
    }
    
    const data = `name_1_2_1 Joseph Jones
    name_1_2_2 Mary Poppins
    name_1_2_3 Sebastian Quincy
    age_1_2_1 20
    age_1_2_2 40
    age_1_2_3 60`;
    
    console.log(parse(data));
    Login or Signup to reply.
  2. Similar to Valery’s, but first I structure the data into a way that is useful:

    [
      {
        "route": [
          "1",
          "2",
          "3"
        ],
        "key": "age",
        "val": "60"
      }
    ]
    

    Using split and other basic array functions to join what’s left back together.

    Then I use a function with a while loop to work through the array of the route key before setting that value.

    const data = [
      `name_1_2_1 Joseph Jones`,
      `name_1_2_2 Mary Poppins`,
      `name_1_2_3 Sebastian Quincy`,
      `age_1_2_1 20`,
      `age_1_2_2 40`,
      `age_1_2_3 60`,
    ];
    
    const keyedData = data.map(line => {
      const bits = line.split(" ");
      
      const route = bits.shift(); // name_1_2_1
    
      const keyBits = route.split("_"); // [name, 1, 2, 1]
      const key = keyBits.shift(); // name
      
      return {
        route: keyBits,
        key,
        val: bits.join(' ') // the left over `Joseph Jones`
      }
    })
    
    
    function buildObject(data){
      const finalObject = {}
      
      data.forEach(item => {
        // Reset the target for this item to the `root` object
        let target = finalObject;
        while(item.route.length){
          let routeFragment = item.route.shift();
          // If the target doesn't have a property on this key, create one
          if(!target.hasOwnProperty(routeFragment)){
            target[routeFragment] = {};
          }
          // Now set the new target 
          target = target[routeFragment];
        }
        // Finally, update that value.
        target[item.key] = item.val;
      })
      
      return finalObject
    }
    
    const result = buildObject(keyedData);
    console.log(result);
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search