0

i have a property like this "data.property" = "16165456".. i try to create a object like this

data {
   property : "16"
}

i use split and and loop but not work


    this.returnKey(model, this.to['pro1'], this.model[this.to['pro2']])
returnKey(model: any, name: string, value: string) {

        let nameParts = name.split("."),
            currentObject = model;
        for (let i in nameParts) {
            let part = nameParts[i]
            currentObject[part] = currentObject[part]

            this.currentObject = currentObject[part];
        }

        let part2 = nameParts[1];
        currentObject = value;
    }
4
  • 1
    There is no problem statement here. Please create a runnable snippet (using toolbar in editor) that runs this code with sample input, and illustrates the problem you experience. Provide what is expected, and what you get instead. Commented Mar 3, 2022 at 9:40
  • sorry man but i dont know what do you mean about runnable snippet ..the code not work because return data = '16' Commented Mar 3, 2022 at 9:53
  • do you help me man or tell me about the view of stack overflow ? Commented Mar 3, 2022 at 9:54
  • You can create a snippet by using the <> button in the question editor. Commented Mar 3, 2022 at 9:56

1 Answer 1

1

A few issues:

  • currentObject[part] = currentObject[part] is a statement that accomplishes nothing. You'd want to indicate what to assign when that property does not yet exist, so do:

    currentObject[part] = currentObject[part] ?? {};
    
  • this.currentObject is a property, and is not the variable with the same name. this is not appropriate here. Just assign to currentObject.

  • The variable part2 gets a value, but it is not used. So this doesn't accomplish anything.

  • The final assignment currentObject = value will not affect the model object, only that local variable. So also this statement has no lasting effect.

  • The for loop iterates one time too many. You'd need to stop one step earlier so you can still assign the value to the property of the parent object that needs to get it.

Here is a correction to all those issues:

function returnKey(model, name, value) {
    let nameParts = name.split("."),
        key = nameParts.pop(), // extract the last property
        currentObject = model;
    for (let i in nameParts) { // Now we will iterate one time less
        let part = nameParts[i];
        // Initialise the property with `{}` when it is new:
        currentObject[part] = currentObject[part] ?? {};
        currentObject = currentObject[part];
    }
    // Assign to the deepest key, so the model will get it:
    currentObject[key] = value;
}

// Demo:
let name = "data.numeroApplication";
let value = "16165456";
let model = {};
returnKey(model, name, value);
console.log(model); // Model has mutated

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

12 Comments

or TS2538: Type 'undefined' cannot be used as an index type. 75 currentObject[key] = value // error key
That is a typescript error. Just declare the type of key if in typescript. Can you debug and tell me what the value of key is? If it is undefined then what is the argument you have passed for name? Can you debug and tell me what the value is of name when you get this error?
if i declare like key = '' ..i get a error.. i cant debug ..because i use an angular application ..
Sorry, I don't understand. You should initialise key to get value from nameParts.pop(). Not ''. Please add console.log(name) inside the function and tell me the value. I cannot reproduce your problem, so you are using some unexpected parameter value.
data.property is the value
|

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.