0

I have an object and I am iterating through it's properties in order to change them (I want to replace 'a.1'-kind to 'a[1]'-kind):

.fail(function (data) {

var errors = data.responseJSON;
console.log("Object before: ", errors);


console.log("Changed properties:")
for (var property in errors) {
    if (errors.hasOwnProperty(property)) {



            if (property.includes('.')) {
                property = property.replace(/\./, "[");
                property = property.replace(/$/, "]");
                console.log(property);

            }

    }
}

console.log("Object after: ", errors);

The properties change during iteration, but object's properties don't change for real:

enter image description here

How do I changed object's properties not only while iterating through them, but "forever"?:)

Appreciate any help:)

4
  • 3
    Please post actual code and not screenshots. Commented Jun 13, 2017 at 19:06
  • Please provide real code through the <> button... Commented Jun 13, 2017 at 19:07
  • what would it change?) Commented Jun 13, 2017 at 19:07
  • 1
    You may read about passing references vs. pass by value, and call by sharing Commented Jun 13, 2017 at 19:08

3 Answers 3

2

You may delete and reassign:

if (property.includes('.')) {
    errors[property.replace(/\./, "[").replace(/$/, "]")]=errors[property];
    delete errors[property];
}

You may ask why does

property=property.replace(..);

Not work? Well property is not related to object in any way. Its just a string...

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

9 Comments

property.replace(/\.(.+)$/, "[$1]") is slightly more succinct.
@Jonas w, thank you. but can you explain, why 1) errors[property.replace(/\./, "[").replace(/$/, "]")]=errors[property]; and not: errors[property] =errors[property.replace(/\./, "[").replace(/$/, "]")];
@Jonas w, and why do we need to: delete errors[property]; ?
@sergej formin you want to copy errors["a[1]"]=errors["a.1"]; and then delete errors["a.1"] ... And = means copy not move
@Jonas w, thank you:) so when we errors["a[1]"]=errors["a.1"] we actually add a new "property:value" pair to the object, and then we have to delete old one... I didn't know that just by declaring a new "property:value" pair inside "for... in" loop you are adding this pair to the object.... Thanks again!
|
0

Like the post from @Jonas w above, you can do a delete and reassign the value.

Another example here (does not include your string replacement/regex logic, but shows how you can update/alter the keys:

let logger = document.querySelector('pre');

let obj = {
  foo: 'foo-value',
  bar: 'foo-value',
  baz: 'foo-value',
  qux: 'foo-value'
};

logger.innerHTML = `Original: ${JSON.stringify(obj, null, 2)}\n`;

Object.keys(obj).forEach((oldKey) => {
  let newKey = oldKey + '-new';
  let originalVal = obj[oldKey];
  obj[newKey] = originalVal;
  delete obj[oldKey];
});

logger.innerHTML += `Updated: ${JSON.stringify(obj, null, 2)}\n`;
<pre></pre>

Comments

0

A functional approach:

function replaceProperties(obj) {

    var newObj = Object.getOwnPropertyNames(obj).reduce(function (newObj, prop) {

        var newProp = prop;

        if (prop.includes('.')) {
            newProp = prop.replace(/\./, "[").replace(/$/, "]");
        }

        newObj[newProp] = obj[prop];

        return newObj;

    }, {});

    return newObj;

}

var newObj = replaceProperties(errors);

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.