javascript step inside object to populate with variable

I try to dynamically step in depth inside an object. The object children is unknown and should instead be used from a string.

I hope you can see below what I try to do. I've comment out the part where I can't figure out a solution.

var global_object = {};

function populate(id, text) {
  let parts = id.split('/');

  for(let key in parts) {
    let part = parts[key];
    // global_object['this']['is']['a']['long']['url'] = text;
    console.log(part);
  }
}

populate('this/is/a/long/url', 'My text');

you can do something like this, store a var obj and put i inside a new key each turn. Then for the text, use another var to "remember" the last one.

var global_object = {};

function populate(id, text) {
  let parts = id.split('/');
  let obj = global_object, last_obj, part;

  for(let key in parts) {
    part = parts[key];
    last_obj = obj;      
    obj[part] = {};
    obj = obj[part]
    console.log(part);
  }
  last_obj[part] = text;
}

populate('this/is/a/long/url', 'My text');
console.log(global_object);

Iterating Key/Value Pairs. A simple JavaScript “for” loop can be used to iterate through your new dictionary. for(varkeyindict){varvalue=dict[key];// do something with "key" and "value" variables} By using the “for(var key in dict)” method of iterating on the object you are able to easily access all the key/value pairs in contains. This method will iterate with each “key” value being the Index on the Object that can be used to access the associated Value.

An alternative recursive solution self contained with no external variables

const populate = (id, text, result) => {
  const arr = id.split('/');
  let key = arr.shift();
  if(key) { 
    result[key] = {};
    if(!arr.length) { result[key] = text; }
    populate(arr.join("/"), text, result[key])
  }
  return result;
}

console.log(populate('this/is/a/long/url', 'My text', {}));

In JavaScript, arrays and objects are reference types. This means that when a variable is assigned an array or object, what gets assigned to the variable is a reference to the location in memory where the array or object was stored. Arrays, just like every other object in JavaScript, are reference types.

Something like this should work. obj is the current object to be populated and it is updated with each iteration of the for loop.

let obj = global_object;
function populate(id, text) {
    let parts = id.split('/');

    for (let key in parts) {
        let part = parts[key];
        obj[part] = {};
        obj = obj[part];

    }
    obj = text;
}

Variables exist and are global only within the logic app instance that creates them. Also, they persist across any loop iterations inside a logic app instance. When you reference a variable, use the variable's name as the token, not the action's name, which is the usual way to reference an action's outputs.

Or reduce it

var global_object = {};

function populate(id, text) {
    global_object = id.split("/").reduceRight((acc, part) => {
        var nested = JSON.parse(JSON.stringify(acc));

        acc = {};
        acc[part] = nested;

        return acc;
    }, text);
}

populate("this/is/a/long/url", "My text");

A list. Objects, as generic blobs of values, can be used to build all sorts of data structures. A common data structure is the list (not to be confused with array). A list is a nested set of objects, with the first object holding a reference to the second, the second to the third, and so on.

An object that contains two properties describing the global variable: value: its data type, which can be any data type accepted within WebAssembly modules — i32, i64, f32, or f64. mutable: a boolean defining whether the value is mutable or not. A value containing the variable's actual value.

The flow variable can be one of the pre-defined flow variables (as listed in the Flow variables reference), or a custom flow variable that you created. The value of <Ref> is always interpreted as a flow variable; you cannot specify a literal string as the value.

In order to populate the page, we're first going to need a template for each of our activities. I've already designed this section using the Page Builder tools, but you can pause to grab a few components that'll match these fields. And our next step is the binding.

Comments
  • Possible duplicate of Javascript: How to create an object from a dot separated string?
  • the answer to your problem is similar if not the same here: by @Tony although what you are doing is considered bad practice. stackoverflow.com/questions/20424226/…
  • @ElieNassif Thanks! Why is it considered bad practice and what is the alternative?
  • @JensTörnell if you rename one of the attributes in another piece of code, the current code will not be functional. Unless you perform a lot of control over it. Not sure what the context of your project is.
  • @ElieNassif I try to generate some kind of file/folder API for fetching files and folders from PHP with ajax. The idea is that when I send an url or id as I call it, it should populate this nested object. Then later I can use that object to render a filetree as html elements from js.