Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7927589
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T19:23:09+00:00 2026-06-03T19:23:09+00:00

Using Underscorejs, would anyone know how to simply replace one attribute of an object?

  • 0

Using Underscorejs, would anyone know how to simply replace one attribute of an object?

For example, if I have this:

var obj = {
 "id" : 123,
 "date" : 142002020,
 "name" : "somename",
 "active" : 1
}

Now I want to set active to 0;

I could use _.collect and check for ‘active’ then update it and return to a new variable, but it seems like there’s a better way, I’m just not seeing it.

Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T19:23:11+00:00Added an answer on June 3, 2026 at 7:23 pm

    Why don’t you just obj['active'] = 0, you don’t need underscore for changing an property.

    EDIT

    If you need to find a nested key maybe you could use a recursive function.

    var obj = {
       'foo' : {
          'bar' : {
             'active' : 1
          }
       }
    }
    
    function replaceKey (obj, key, value) {
       var _obj = _(obj)
    
        _obj.each(function (v, k) {
          if (k === key) {
             obj[k] = value
          } else if (obj[k] === Object(obj[k])) {
             replaceKey(obj[k], key, value)
          }
       })
    
       return _obj;
    }
    
    console.log('should be active:', obj.foo.bar.active === 1)
    replaceKey(obj, 'active', 0)
    console.log('should not be active:', obj.foo.bar.active === 0)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

using (var file_stream = File.Create(users.xml)) { var serializer = new XmlSerializer(typeof(PasswordManager)); serializer.Serialize(file_stream, this); file_stream.Close();
Using Flex 3, I would like to take an image snapshot such as this:
Since the advent of AS3 I have been working like this: private var loggy:String;
Looks like other people have had this issue, but I would like to understand
Guys, I have a table called tblNames and one of my fields in this
I hope someone can help me with this Javascript. I have an Object called
When using EF4 to create a model, I would like to replace all underscores
Using a restful resource in Rails, I would like to be able to insert
Using C# for ASP.NET and MOSS development, we often have to embed JavaScript into
I have unfortunately wandered into a situation where I need regex using Ruby. Basically

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.