2

i have an large array of objects full of values and i have an empty array of objects as well, and i am willing to get every value from full array of objects (btw full array of objects has more keys than empty one) and push it to empty one like so:

/*empty one (array1):*/ [{foo: '', bar: '', thumbnail: ''}];

/*full one (array2):*/ [{foo_: 'blah', bar: 'blah',
                       thumbnail: '/photo', id: 'ad12dxa1', something: 'thing'},{foo_: 'blah1', bar: 'blah1',
                       thumbnail: '/photo1', id: 'ad12dxa12', something: 'thing1'}];

and i want to get only foo_, bar and thumbnail from array2 and insert them as foo, bar & thumbnail into array1 like: foo = foo_ bar=bar, thumbnail= thumbnail

if answer will include both for loop and array.every method explanation it would be better.

Thank you!

1 Answer 1

3

You can simply create a new array using .map() with some Object Destructuring:

const data = [
  {foo_: 'blah', bar: 'blah', thumbnail: '/photo', id: 'ad12dxa1', something: 'thing'},
  {foo_: 'blah1', bar: 'blah1', thumbnail: '/photo1', id: 'ad12dxa12', something: 'thing1'}
];
                                            
const result = data.map(({foo_:foo, bar, thumbnail}) => ({foo, bar, thumbnail}));

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

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

10 Comments

Your answer had a "run code snippet" button, which worked, until you started editing it. I'd recommend going back to the first version.
There it is again. Don't touch anything now.
this code is little obscure, can you add explanation what does foo_:foo and => means because you modified my code and your code has not any foo key and also i please use my code as an example
@iLiA I've added the links to the docs containing necessary information with simple examples. => represents the Arrow Functions
thank you @MohammadUsman, can you also provide .every and for loop anwer?
|

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.