1

I have an object that I loop with a for in loop that loops through the object to find the key and value.

house: {
  john: {
       love_off: "mary"
  }
}

My for in loop returns:

 for ( var key in house.john) {
     house.john[key.replace("_off","") = house.john[key];
 }

It returns a copy of the original array long with:

house: {
      john: {
           love: "mary"
           love_off: "mary"
      }
    }

How do I just get "love": "mary" from the object and add it to array that needs to be returned?

3
  • You need to replace the love_off pair with the new love pair? Commented Apr 13, 2017 at 17:34
  • No array appears at javascript at Question. Is resulting object expected result? Are you trying to get the new property and value? Commented Apr 13, 2017 at 17:36
  • I am just trying to get the new property and value. I misspoke. I apologize Commented Apr 13, 2017 at 18:26

4 Answers 4

1

You have to use delete statement in order to delete the old property.

The delete operator removes a property from an object.

var object={
  "house": {
     "john": {
       "love_off": "mary"
     }
  }
}
 for ( var key in object.house.john) {         
     object.house.john[key.replace("_off","")] = object.house.john[key];
     if(object.house.john.hasOwnProperty(key))
         delete object.house.john[key];
 }
console.log(object);

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

6 Comments

might need to check if the property exists first
suggestion...use hasOwnProperty() ... value of property could be falsy but still exist. And would check before modifications
@charlietfl, yes, you're right. hasOwnProperty is better choice in this situation.
@charlietfl "suggestion...use hasOwnProperty()" Interesting. If recollect correctly, have received two different comments recently suggesting to use or not use in operator. Is the a Question that you are aware of that addresses difference between in operator and .hasOwnProperty()?
@guest271314 simply to check if key is enumerable
|
1

If interpret Question correctly, you can use JSON.stringify(), JSON.parse() with .replace()

data = JSON.parse(JSON.stringify(data).replace(/_off(?=":)/, ""));

2 Comments

Good answer!! But check that could be multiple properties with "_off" and with the method String.prototype.replace() only the first occurrence will be replaced
@YosvelQuintero The RegExp can be adjusted. Did not consider objects other than at OP before posting Answer
0

try this

var myArray= [] 
for ( var key in object.house.john) {         
     object.house.john[key.replace("_off","")] = object.house.john[key];
     delete object.house.john[key];
     myArray.push(object.house.john);
 }

Comments

0

You can loop over all Object keys with Object.keys() using Array.prototype.forEach().

Than make the replacement and the most important is to delete the old key property.

Code example:

var obj = {house: {john: {love_off: "mary"}}};

Object.keys(obj.house.john).forEach(function (k) {
  obj.house.john[k.replace('_off', '')] = obj.house.john[k];
  delete obj.house.john[k];
});

console.log(obj);

Comments

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.