Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

how to access and assign new value in array/object in lodash

I am looking for simple easy way to assign new value through lodash. See the blow example. I only want to reassign value of first element of object of first element of arr array.

const arr = [
 {
   a: 'apple',
   b: 'banana',
   c: 'cat'
 },
 {
   a: 'apple-1',
   b: 'banana-2',
   c: 'cat-3'
 }
];

const newArr = arr;
const ele = ._first(newArr);// picked first element
ele['a'] = 'dog';

this is what I expect to see

console.log(newArr);
// [
 {
   a: 'dog', //<--- updated
   b: 'banana',
   c: 'cat'
 },
 {
   a: 'apple-1',
   b: 'banana-2',
   c: 'cat-3'
 }
];

thank you ahead!

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Why you cannot just use simple JS without lodash?

const arr = [
 {
   a: 'apple',
   b: 'banana',
   c: 'cat'
 },
 {
   a: 'apple-1',
   b: 'banana-2',
   c: 'cat-3'
 }
];

const newArr = arr;
newArr[0].a = 'dog'

console.log(arr)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading