My goal is to locate an item in a list and transfer some of its attributes to another item. The code I currently have feels messy and inefficient.
this.myResult = {value1: null, value2: null};
this.myArray = [
{ a: 'test1', b: 1, c: {subObject1}},
{ a: 'test2', b: 2, c: {subObject2}}
];
let result = this.myArray.find(myObject => myObject.b === 2);
this.myResult = {
value1: result.a,
value2: result.b
}
Is there a more elegant way to utilize the result of the Array.find()
function in another lambda function and update myResult
without the need for a temporary variable? I'm exploring options similar to the following code snippet, and willing to consider the use of libraries like lodash.
this.myResult = {value1: null, value2: null};
this.myArray = [
{ a: 'test1', b: 1, c: {subObject1}},
{ a: 'test2', b: 2, c: {subObject2}}
this.myArray
.find(myObject => myObject.b === 2)
.then(result => {
this.myResult = {
value1: result.a,
value2: result.b
}
});
EDIT:
After discussions in the comments, I am interested in a solution involving something akin to a .map
function following the .find
this.myResult = {value1: null, value2: null};
this.myArray = [
{ a: 'test1', b: 1, c: {subObject1}},
{ a: 'test2', b: 2, c: {subObject2}}
this.myArray
.find(myObject => myObject.b === 2)
.map(result => {
this.myResult = {
value1: result.a,
value2: result.b
}
});