2

Assuming the following Array:

[
{id: 1234, name: "@Acme", sources:["Twitter"]},
{id: 5678, name: "@Enron", sources:["Facebook"]},
]

I want to promote sources[0] to a property value, either under sources itself or as a new key using lodash.

I've done the following:

myList = _.map(monitorList, _.partialRight(_.pick, ['id', 'name', 'sources']));
mySources = _.map(monitorList, 'sources');

I imagine I can iterate through each respective array now and map my index from mySources to the sources key in myList, however it seems like there should be a functional way using lodash to promote a nested array item to a property value.

Ideal final data structure:

[
{id: 1234, name: "@Acme", sources:"Twitter"},
{id: 5678, name: "@Enron", sources:"Facebook"},
]
2
  • Could you also show the intended output structure you want to achieve? Commented Jul 11, 2017 at 15:45
  • @guwere updated with more detail Commented Jul 11, 2017 at 17:33

3 Answers 3

2

With a functional ES6 approach:

const monitorList = [
    {id: 1234, name: "@Acme", sources:["Twitter"]},
    {id: 5678, name: "@Enron", sources:["Facebook"]},
];

var result = monitorList.map(o => Object.assign({}, o, { sources: o.sources[0] }));

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

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

Comments

1

You can follow a simple path and use forEach to replace sources property:

var items = [{id: 1234, name: "@Acme", sources:["Twitter"]},
             {id: 5678, name: "@Enron", sources:["Facebook"]}];

items.forEach((item) => item.sources = item.sources[0]);

console.log(items);

Another solution, using map, which is more functional (as it does not change items variable):

var items = [{id: 1234, name: "@Acme", sources:["Twitter"]},
             {id: 5678, name: "@Enron", sources:["Facebook"]}];

var newItems = items.map((item) => Object.assign({}, item, { sources: item.sources[0] }));

console.log(newItems);

7 Comments

Note that doing it this way mutates the original array. That could be ok but its not very functional.
@terpinmd. That's true. Edite my answer to include a more functional solution. Thanks!
I wanted an answer in lodash, but it's pretty similar and I didn't care about mutation of the original array in this case, so your solution put me on the right path: _.forEach(monitorList, monitor => monitor.sources = monitor.sources[0]);
Yeah, _.forEach ends up being equivalent to Array.prototype.forEach
@TylerMills, I'm a bit confused how to reconcile "there should be a functional way" with "I didn't care about mutation".
|
1

You could use map:

var array = [{id: 1234, name: "@Acme", sources:["Twitter"]},
              {id: 5678, name: "@Enron", sources:["Facebook"]}];


var items = array.map(item => {
  item.source = item.sources[0]; 
  return item;
});

You could change item.source to item.sources as well if you wanted to overwrite.

Another way using some losdash methods:

var items = array.map(item => {
  return _.assign({}, item, {sources: _.first(item.sources)});
});

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.