使用JS Underscore按键合并和排序类似的数组

时间:2017-04-26 08:34:14

标签: javascript arrays json underscore.js

我有两个数组:

"array_one":
[
    {
    "id": 1,
    "name": One
     },
     {
     "id": 2,
     "name": Two
     }
]

"array_two":
[
    {
    "id": 1,
    "name": Uno
     },
     {
     "id": 3,
     "name": Three
     }
]

我需要使用Underscore:

  1. 使用与array_two匹配的id的对象覆盖array_one中的所有对象。

  2. 如果没有其他具有id的对象,则将array_two中的所有对象附加到array_one中。

  3. 覆盖的对象应在数组中保留其索引

  4. 必须在数组末尾添加附加对象

  5. 所以最终结果如下:

    "array_final":
    [
        {
        "id": 1,
        "name": Uno
         },
        {
        "id": 2,
        "name": Two
         },
         {
         "id": 3,
         "name": Three
         }
    ]
    

2 个答案:

答案 0 :(得分:0)

您可以使用两个对象作为哈希表,一个用于跟踪添加的对象,另一个用于第二个阵列。然后使用reduce来替换第一个数组中的对象(如果在第二个数组中找到),最后再使用一个for循环来添加第二个数组中的其余对象。



var data = {
  "array_one": [{
    "id": 1,
    "name": 'One'
  }, {
    "id": 2,
    "name": 'Two'
  }],
  "array_two": [{
    "id": 1,
    "name": 'Uno'
  }, {
    "id": 3,
    "name": 'Three'
  }]
}

var obj = {}, added = {}
data.array_two.forEach(e => obj[e.id] = e)

var result = {finalArray: data.array_one.reduce(function(r, e) {
  r.push(obj[e.id] ? (added[e.id] = 1, obj[e.id]) : e);
  return r;
}, [])}

for(var i in obj) if(!added[obj[i].id]) result.finalArray.push(obj[i]) 

console.log(result)




答案 1 :(得分:0)

<script src="//cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.3/underscore-min.js"></script>
2017-04-26T08:52:25.579045+00:00 app[web.1]: bundler: failed to load command: rackup (/app/vendor/bundle/ruby/2.3.0/bin/rackup)
2017-04-26T08:52:25.579176+00:00 app[web.1]: RuntimeError: missing run or map statement
2017-04-26T08:52:25.579178+00:00 app[web.1]:   /app/vendor/bundle/ruby/2.3.0/gems/rack-1.6.5/lib/rack/builder.rb:146:in `to_app'
2017-04-26T08:52:25.579179+00:00 app[web.1]:   /app/config.ru:5:in `<main>'
2017-04-26T08:52:25.579180+00:00 app[web.1]:   /app/vendor/bundle/ruby/2.3.0/gems/rack-1.6.5/lib/rack/builder.rb:49:in `eval'
2017-04-26T08:52:25.579180+00:00 app[web.1]:   /app/vendor/bundle/ruby/2.3.0/gems/rack-1.6.5/lib/rack/builder.rb:49:in `new_from_string'
2017-04-26T08:52:25.579181+00:00 app[web.1]:   /app/vendor/bundle/ruby/2.3.0/gems/rack-1.6.5/lib/rack/builder.rb:40:in `parse_file'
2017-04-26T08:52:25.579182+00:00 app[web.1]:   /app/vendor/bundle/ruby/2.3.0/gems/rack-1.6.5/lib/rack/server.rb:299:in `build_app_and_options_from_config'
2017-04-26T08:52:25.579183+00:00 app[web.1]:   /app/vendor/bundle/ruby/2.3.0/gems/rack-1.6.5/lib/rack/server.rb:208:in `app'
2017-04-26T08:52:25.579184+00:00 app[web.1]:   /app/vendor/bundle/ruby/2.3.0/gems/rack-1.6.5/lib/rack/server.rb:336:in `wrapped_app'
2017-04-26T08:52:25.579184+00:00 app[web.1]:   /app/vendor/bundle/ruby/2.3.0/gems/rack-1.6.5/lib/rack/server.rb:272:in `start'
2017-04-26T08:52:25.579185+00:00 app[web.1]:   /app/vendor/bundle/ruby/2.3.0/gems/rack-1.6.5/lib/rack/server.rb:147:in `start'
2017-04-26T08:52:25.579186+00:00 app[web.1]:   /app/vendor/bundle/ruby/2.3.0/gems/rack-1.6.5/bin/rackup:4:in `<top (required)>'
2017-04-26T08:52:25.579187+00:00 app[web.1]:   /app/vendor/bundle/ruby/2.3.0/bin/rackup:23:in `load'
2017-04-26T08:52:25.579215+00:00 app[web.1]:   /app/vendor/bundle/ruby/2.3.0/bin/rackup:23:in `<top (required)>'
2017-04-26T08:52:25.724981+00:00 heroku[web.1]: Process exited with status 1
2017-04-26T08:52:26.928566+00:00 heroku[web.1]: Starting process with command `bundle exec rackup config.ru -p 56963`
2017-04-26T08:52:29.278571+00:00 app[web.1]: [2017-04-26 08:52:29] INFO  WEBrick 1.3.1
2017-04-26T08:52:29.278590+00:00 app[web.1]: [2017-04-26 08:52:29] INFO  ruby 2.3.1 (2016-04-26) [x86_64-linux]
2017-04-26T08:52:29.278909+00:00 app[web.1]: == Sinatra (v1.4.8) has taken the stage on 56963 for production with backup from WEBrick
2017-04-26T08:52:29.279274+00:00 app[web.1]: [2017-04-26 08:52:29] INFO  WEBrick::HTTPServer#start: pid=4 port=56963
2017-04-26T08:52:29.825875+00:00 heroku[web.1]: State changed from starting to up