0

I have array with objects users:

let state = {
  users: [
    {id: 1, name: 'Igor',  selected: false},
    {id: 2, name: 'Anton', selected: false},
    {id: 3, name: 'Vasya', selected: false},
    {id: 4, name: 'Pete',  selected: false},
    {id: 5, name: 'Dan',   selected: false}
  ], ....

And a function that works with this object:

export function selectUser(userId) {
  if (state.users.find(item => item.id == userId).selected == false) {
    /* some code */

    //[needed] Code to update the user property selected to true

  } else {
    /* some other code */

    //[needed] Code to update the user property selected to true
  }
  emitChange();
}

How to do that, without overwriting the whole array, strangely can't think of anything

6
  • 3
    Please label with the language being used. It looks like JavaScript, but then again Pepsi looks like Coke but I would never want to drink Pepsi. Commented Dec 28, 2015 at 8:32
  • 1
    "How to do that", How to do what? Commented Dec 28, 2015 at 8:35
  • when you say change you mean you want to replace or modify existing object ? Commented Dec 28, 2015 at 8:49
  • @TimBiegeleisen sorry for that Commented Dec 28, 2015 at 8:51
  • @ikffs Your code makes no sense as the true case is the same as the false case according to the comments. Please rework your question. Commented Dec 28, 2015 at 8:51

3 Answers 3

1

if you want to toggle property then

export function selectUser(userId) {
  var user = state.users.find(user => user.id == userId);
  var isSelected = user.selected; 
  // state.users.forEach(user => user.selected = false ); deselect others 
  user.selected = !isSelected;
  emitChange();
}
Sign up to request clarification or add additional context in comments.

Comments

1

The find() method returns the object you want to update. So call that first and assign it to a variable, then test it exists and if so then work with it.

export function selectUser(userId) {
  var user = state.users.find(item => item.id == userId);

  if (user != 'undefined'){
    if (user.selected == false) {
      /* some code */

      user.name = 'New Name';
      // etc

    } else {
      /* some other code */
    }
  }

  emitChange();
}

Comments

1

According to the comments in the true and false case this should work:

export function selectUser(userId) {
  var user = state.users.find(item => item.id == userId);
  if (user && user.selected == false) {
    user.selected = true;
  } else {
    /* some other code */
  }
  emitChange();
}

3 Comments

find() returns new object, so you are modifying "user" in your code not original object
@ikffs Where did you get that information? According to the specs Array.find() returns the object itself, not a copy or something like that. I've also tested it and it works.
jeez buddy, you are a lifesaver, sorry for long response, I don't know why but I really thought that find returns some other object, silly me! thanks

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.