3

In my array I am using .find to find an object and set its property to some value. I want to set the name only if there is a value. i.e; check for undefined Below is my array, am using .find to find the object and set its name. I want to set only if the object with id === checkid is found.

 this.myarray.find(colDef => colDef.id === checkid).name = newname;.

4 Answers 4

3

You can do this in one statement with Object.assign like this:

let myArray = [{id: 1, name: 'Boris'}, {id: 2}]

// id 3 does not exist no changes would be made to myArray
Object.assign(myArray.find(x => x.id == 3) || {}, {name: 'Foo'})

// id 1 is there so name would be changed to 'john'
Object.assign(myArray.find(x => x.id == 1) || {}, {name: 'john'})

console.log(myArray)

The idea is to provide Object.assign with an object literal if Array.find does not find an object with the provided id.

The other benefit of this is that you now pass an object for the update so you can easily update more than one property at a time:

let myArray = [{id: 1, name: 'Boris'}, {id: 2}]

// id 1 is there so name would be changed to 'john'
Object.assign(myArray.find(x => x.id == 1) || {}, {name: 'john', state: 'FL'})

console.log(myArray)

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

3 Comments

this is really nice, using Object.assign. Thanks!
Glad you liked it. If this answered your question please mark it as the accepted answer so others can find it easier. Thanks.
I came for an undefined and went home with an Object.assignment. This is beautiful.
1

You need to run find as it's own statement and check if the result is defined. If so, assign the name, otherwise don't:

let myarray = [
    {id: 100},
    {id: 10},
    {id: 5}
]

let found = myarray.find(colDef => colDef.id === 10)

if (found !== undefined){
    found.name = "some name"
} 
console.log(found)

// no id 11 found will be undefined
found = myarray.find(colDef => colDef.id === 11)
console.log(found)

As a single line:

 this.myarray.find(colDef => colDef.id === checkid).name = newname;

there's no chance to check if find() returned undefined and trying to assign a property to undefined will be an error.

1 Comment

I kind of wanted to see if we can do in one statement along with find. Anyways thanks!
0

Here is both ways of approaching.

If you dont care, use fireAndForget style.

If you really want to know, a more verbose check.

const arr = [{id:1}];

const fireAndForget = (arr, id, newName) => (arr.find(i => i.id === id)||{}).name = newName;

const checkAndNotify = (arr, id, newName) => {
  const p = arr.find(i => i.id === id);
  if(!p) return false;
  p.name = newName;
  return true;
};

console.log(checkAndNotify(arr, 2, 'test'));
console.log(checkAndNotify(arr, 1, 'test'));
console.log(arr);

console.log(fireAndForget(arr, 2, 'test2'));
console.log(fireAndForget(arr, 1, 'test2'));
console.log(arr);

Comments

0

/Check if obj isn't null of undefined, and if it's an object. If if is a valid object, then assign the value for '.name'

const obj = this.myarray.find(colDef => colDef.id === checkid);

if (obj && typeof obj === "object") {
  obj.name = newName;
}

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.