最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - LoDash - How to push one collections values into another via common key - Stack Overflow

programmeradmin4浏览0评论

I have two collections.

var a = 
[
 {unique_id: "001", state: "CO"},
 {unique_id: "001", state: "TX"},
 {unique_id: "001", state: "CC"},
 {unique_id: "002", state: "CC"},
 {unique_id: "002", state: "NY"}
]

And

var b = 
[
 {unique_id: "001", states:[]},
 {unique_id: "002", states:[]}
]

And I want to get:

var b = 
[
 {unique_id: "001", states:["CO","TX","CC"]},
 {unique_id: "002", states:["CC","NY"]}
]

I should mention that the "b" array has to stay in the same order it's in AND some of the unique_id's don't have a value.

I've been trying to use LoDash / - so if anyone can solve this with LoDash that would be awesome!

I have two collections.

var a = 
[
 {unique_id: "001", state: "CO"},
 {unique_id: "001", state: "TX"},
 {unique_id: "001", state: "CC"},
 {unique_id: "002", state: "CC"},
 {unique_id: "002", state: "NY"}
]

And

var b = 
[
 {unique_id: "001", states:[]},
 {unique_id: "002", states:[]}
]

And I want to get:

var b = 
[
 {unique_id: "001", states:["CO","TX","CC"]},
 {unique_id: "002", states:["CC","NY"]}
]

I should mention that the "b" array has to stay in the same order it's in AND some of the unique_id's don't have a value.

I've been trying to use LoDash https://lodash./ - so if anyone can solve this with LoDash that would be awesome!

Share Improve this question edited Jun 19, 2015 at 20:49 webmaster_sean asked Jun 19, 2015 at 19:40 webmaster_seanwebmaster_sean 9724 gold badges13 silver badges23 bronze badges 2
  • What have you tried to do? I'm not going to just tell you, though someone may! – Robert Moskal Commented Jun 19, 2015 at 19:50
  • I've tried _.forEach and _.forIn with Lodash, but can't seem to figure out how to match pushing the values into "b" by unique_id... – webmaster_sean Commented Jun 19, 2015 at 19:57
Add a ment  | 

2 Answers 2

Reset to default 3

The time plexity of this solution is suboptimal ( O(n^2) ), but it might help you think of ways to match pushing the values into "b":

_.forEach(a, function(element1){
  _.forEach(b, function(element2){
    if (element2.unique_id === element1.unique_id) {
     element2.states.push(element1.state);
    }
  });
});

Perhaps a better solution might be to index your objects in b by their unique id using lodash's _.indexBy method. For example you can index your objects in b as follows:

var c = _.indexBy(b, 'unique_id')

Which will result in:

{001:{unique_id: "001", states:[]}, 002: {unique_id: "002", states:[]}}

Since the objects in our c array are pointing to the same objects in memory as those in our b array, we can directly mutate our objects in c and b will reference those updated objects in memory. So:

_.forEach(a, function(element1){
  if (element1.unique_id in c) {
    c[element1.unique_id].states.push(element1.state);
  }
})

Now if we take a look at our b array, we'll see that the value is:

var b = 
[
 {unique_id: "001", states:["CO","TX","CC"]},
 {unique_id: "002", states:["CC","NY"]}
]

The time plexity of this solution should be close to O(n), which is much better than using the nested _.forEach approach. Code snippet:

var a = 
[
 {unique_id: "001", state: "CO"},
 {unique_id: "001", state: "TX"},
 {unique_id: "001", state: "CC"},
 {unique_id: "002", state: "CC"},
 {unique_id: "002", state: "NY"}
];

var b = 
[
 {unique_id: "001", states:[]},
 {unique_id: "002", states:[]}
];

var c = _.indexBy(b, 'unique_id');

_.forEach(a, function(element1){
  if (element1.unique_id in c) {
    c[element1.unique_id].states.push(element1.state);
  }
});

document.writeln(JSON.stringify(b))
<script src="https://cdnjs.cloudflare./ajax/libs/lodash.js/3.9.3/lodash.js"></script>

groupBy() and map() are your friends:

_(a).groupBy('unique_id')
    .map(function(item, key) {
        return {
            unique_id: key,
            states: _.pluck(item, 'state')
        };
    })
    .value()

You're basically grouping the states by their unique_id, then mapping that object to an array where each item has the structure you need.

发布评论

评论列表(0)

  1. 暂无评论