如何基于另一个数组中的条件从关联数组中提取?

时间:2018-11-21 16:51:19

标签: javascript jquery

我有一个包含如下数据的对象数组:

const RoomsDetails = [
    {
        "RoomId" : "37A",
        "NumberOfBeds" : "4"
    },
    {
       "RoomId" : "38A",
       "NumberOfBeds" : "5"
    },
    {   
        "RoomId" : "39A",
        "NumberOfBeds" : "4"
    }
]

以及包含有效房间ID的其他阵列:

const ActiveRoomsIds = ["37A", "39A"]

现在,我只想从RoomsDetails中提取其 RoomId的中存在的房间。

我需要一些有关如何通过短代码做到这一点的帮助

3 个答案:

答案 0 :(得分:3)

为了只获取基于ActiveRoomsIds数组的活动房间,可以使用如下过滤器:

RoomsDetails.filter( f => ActiveRoomsIds.indexOf(f.RoomId) > -1);

答案 1 :(得分:1)

也许是这样的:

const results = RoomDetails
  .map( (room) => room.RoomId ) // get the roomId
  .filter(roomId => ActiveRoomsIds.indexOf(roomId) !== -1); //exists in array of rooms

希望有帮助

答案 2 :(得分:1)

您也可以使用Array.prototype.includes()

NameError:
  undefined local variable or method `app' for #<Devise::Mailer:0x00007fd3271928f8>
# /Users/melsatar/.rvm/gems/ruby-2.4.5/gems/rack-test-1.1.0/lib/rack/test/methods.rb:30:in `build_rack_mock_session'
# /Users/melsatar/.rvm/gems/ruby-2.4.5/gems/rack-test-1.1.0/lib/rack/test/methods.rb:26:in `rack_mock_session'
# /Users/melsatar/.rvm/gems/ruby-2.4.5/gems/rack-test-1.1.0/lib/rack/test/methods.rb:41:in `build_rack_test_session'
# /Users/melsatar/.rvm/gems/ruby-2.4.5/gems/rack-test-1.1.0/lib/rack/test/methods.rb:37:in `rack_test_session'
# /Users/melsatar/.rvm/gems/ruby-2.4.5/gems/rack-test-1.1.0/lib/rack/test/methods.rb:45:in `current_session'
# /Users/melsatar/.rvm/gems/ruby-2.4.5/gems/actionview-5.1.6/lib/action_view/helpers/controller_helper.rb:18:in `assign_controller'
# /Users/melsatar/.rvm/gems/ruby-2.4.5/gems/actionview-5.1.6/lib/action_view/base.rb:211:in `initialize'
# /Users/melsatar/.rvm/gems/ruby-2.4.5/gems/actionpack-5.1.6/lib/action_dispatch/routing/url_for.rb:106:in `initialize'
# /Users/melsatar/.rvm/gems/ruby-2.4.5/gems/actionview-5.1.6/lib/action_view/rendering.rb:72:in `new'
# /Users/melsatar/.rvm/gems/ruby-2.4.5/gems/actionview-5.1.6/lib/action_view/rendering.rb:72:in `view_context'
# /Users/melsatar/.rvm/gems/ruby-2.4.5/gems/actionview-5.1.6/lib/action_view/rendering.rb:97:in `_render_template'
# /Users/melsatar/.rvm/gems/ruby-2.4.5/gems/actionview-5.1.6/lib/action_view/rendering.rb:83:in `render_to_body'
# /Users/melsatar/.rvm/gems/ruby-2.4.5/gems/actionpack-5.1.6/lib/abstract_controller/rendering.rb:24:in `render'
# /Users/melsatar/.rvm/gems/ruby-2.4.5/gems/actionmailer-5.1.6/lib/action_mailer/base.rb:945:in `block in collect_responses_from_templates'
# /Users/melsatar/.rvm/gems/ruby-2.4.5/gems/actionmailer-5.1.6/lib/action_mailer/base.rb:942:in `each'
# /Users/melsatar/.rvm/gems/ruby-2.4.5/gems/actionmailer-5.1.6/lib/action_mailer/base.rb:942:in `each'
# /Users/melsatar/.rvm/gems/ruby-2.4.5/gems/actionmailer-5.1.6/lib/action_mailer/base.rb:942:in `map'
# /Users/melsatar/.rvm/gems/ruby-2.4.5/gems/actionmailer-5.1.6/lib/action_mailer/base.rb:942:in `collect_responses_from_templates'
# /Users/melsatar/.rvm/gems/ruby-2.4.5/gems/actionmailer-5.1.6/lib/action_mailer/base.rb:927:in `collect_responses'
# /Users/melsatar/.rvm/gems/ruby-2.4.5/gems/actionmailer-5.1.6/lib/action_mailer/base.rb:830:in `mail'
# /Users/melsatar/.rvm/gems/ruby-2.4.5/gems/devise-4.5.0/lib/devise/mailers/helpers.rb:19:in `devise_mail'
# ./app/mailers/devise/mailer.rb:12:in `confirmation_instructions'
# /Users/melsatar/.rvm/gems/ruby-2.4.5/gems/actionpack-5.1.6/lib/abstract_controller/base.rb:186:in `process_action'
# /Users/melsatar/.rvm/gems/ruby-2.4.5/gems/actionpack-5.1.6/lib/abstract_controller/callbacks.rb:20:in `block in process_action'
# /Users/melsatar/.rvm/gems/ruby-2.4.5/gems/activesupport-5.1.6/lib/active_support/callbacks.rb:97:in `run_callbacks'
# /Users/melsatar/.rvm/gems/ruby-2.4.5/gems/actionpack-5.1.6/lib/abstract_controller/callbacks.rb:19:in `process_action'
# /Users/melsatar/.rvm/gems/ruby-2.4.5/gems/actionpack-5.1.6/lib/abstract_controller/base.rb:124:in `process'
# /Users/melsatar/.rvm/gems/ruby-2.4.5/gems/actionmailer-5.1.6/lib/action_mailer/rescuable.rb:23:in `block in process'
# /Users/melsatar/.rvm/gems/ruby-2.4.5/gems/actionmailer-5.1.6/lib/action_mailer/rescuable.rb:15:in `handle_exceptions'
# /Users/melsatar/.rvm/gems/ruby-2.4.5/gems/actionmailer-5.1.6/lib/action_mailer/rescuable.rb:22:in `process'
# /Users/melsatar/.rvm/gems/ruby-2.4.5/gems/actionview-5.1.6/lib/action_view/rendering.rb:30:in `process'
# /Users/melsatar/.rvm/gems/ruby-2.4.5/gems/actionmailer-5.1.6/lib/action_mailer/base.rb:609:in `block in process'
# /Users/melsatar/.rvm/gems/ruby-2.4.5/gems/activesupport-5.1.6/lib/active_support/notifications.rb:166:in `block in instrument'
# /Users/melsatar/.rvm/gems/ruby-2.4.5/gems/activesupport-5.1.6/lib/active_support/notifications/instrumenter.rb:21:in `instrument'
# /Users/melsatar/.rvm/gems/ruby-2.4.5/gems/activesupport-5.1.6/lib/active_support/notifications.rb:166:in `instrument'
# /Users/melsatar/.rvm/gems/ruby-2.4.5/gems/actionmailer-5.1.6/lib/action_mailer/base.rb:608:in `process'
# /Users/melsatar/.rvm/gems/ruby-2.4.5/gems/actionmailer-5.1.6/lib/action_mailer/message_delivery.rb:105:in `block in processed_mailer'
# /Users/melsatar/.rvm/gems/ruby-2.4.5/gems/actionmailer-5.1.6/lib/action_mailer/message_delivery.rb:104:in `tap'
# /Users/melsatar/.rvm/gems/ruby-2.4.5/gems/actionmailer-5.1.6/lib/action_mailer/message_delivery.rb:104:in `processed_mailer'
# /Users/melsatar/.rvm/gems/ruby-2.4.5/gems/actionmailer-5.1.6/lib/action_mailer/message_delivery.rb:95:in `deliver_now'
# /Users/melsatar/.rvm/gems/ruby-2.4.5/gems/devise-4.5.0/lib/devise/models/authenticatable.rb:200:in `send_devise_notification'
# /Users/melsatar/.rvm/gems/ruby-2.4.5/gems/devise-4.5.0/lib/devise/models/confirmable.rb:121:in `send_confirmation_instructions'
# /Users/melsatar/.rvm/gems/ruby-2.4.5/gems/devise-4.5.0/lib/devise/models/confirmable.rb:183:in `send_on_create_confirmation_instructions'
# /Users/melsatar/.rvm/gems/ruby-2.4.5/gems/activesupport-5.1.6/lib/active_support/callbacks.rb:413:in `block in make_lambda'
# /Users/melsatar/.rvm/gems/ruby-2.4.5/gems/activesupport-5.1.6/lib/active_support/callbacks.rb:260:in `block in conditional'
# /Users/melsatar/.rvm/gems/ruby-2.4.5/gems/activesupport-5.1.6/lib/active_support/callbacks.rb:511:in `block in invoke_after'
# /Users/melsatar/.rvm/gems/ruby-2.4.5/gems/activesupport-5.1.6/lib/active_support/callbacks.rb:511:in `each'
# /Users/melsatar/.rvm/gems/ruby-2.4.5/gems/activesupport-5.1.6/lib/active_support/callbacks.rb:511:in `invoke_after'
# /Users/melsatar/.rvm/gems/ruby-2.4.5/gems/activesupport-5.1.6/lib/active_support/callbacks.rb:132:in `run_callbacks'
# /Users/melsatar/.rvm/gems/ruby-2.4.5/gems/activesupport-5.1.6/lib/active_support/callbacks.rb:827:in `_run_commit_callbacks'
# /Users/melsatar/.rvm/gems/ruby-2.4.5/gems/activerecord-5.1.6/lib/active_record/transactions.rb:343:in `committed!'
# /Users/melsatar/.rvm/gems/ruby-2.4.5/gems/activerecord-5.1.6/lib/active_record/connection_adapters/abstract/transaction.rb:86:in `commit_records'
# /Users/melsatar/.rvm/gems/ruby-2.4.5/gems/activerecord-5.1.6/lib/active_record/connection_adapters/abstract/transaction.rb:178:in `block in commit_transaction'
# /Users/melsatar/.rvm/gems/ruby-2.4.5/gems/activerecord-5.1.6/lib/active_record/connection_adapters/abstract/transaction.rb:168:in `commit_transaction'
# /Users/melsatar/.rvm/gems/ruby-2.4.5/gems/activerecord-5.1.6/lib/active_record/connection_adapters/abstract/transaction.rb:207:in `block in within_new_transaction'
# /Users/melsatar/.rvm/gems/ruby-2.4.5/gems/activerecord-5.1.6/lib/active_record/connection_adapters/abstract/transaction.rb:191:in `within_new_transaction'
# /Users/melsatar/.rvm/gems/ruby-2.4.5/gems/activerecord-5.1.6/lib/active_record/connection_adapters/abstract/database_statements.rb:235:in `transaction'
# /Users/melsatar/.rvm/gems/ruby-2.4.5/gems/activerecord-5.1.6/lib/active_record/transactions.rb:210:in `transaction'
# /Users/melsatar/.rvm/gems/ruby-2.4.5/gems/activerecord-5.1.6/lib/active_record/transactions.rb:381:in `with_transaction_returning_status'
# /Users/melsatar/.rvm/gems/ruby-2.4.5/gems/activerecord-5.1.6/lib/active_record/transactions.rb:308:in `block in save'
# /Users/melsatar/.rvm/gems/ruby-2.4.5/gems/activerecord-5.1.6/lib/active_record/transactions.rb:323:in `rollback_active_record_state!'
# /Users/melsatar/.rvm/gems/ruby-2.4.5/gems/activerecord-5.1.6/lib/active_record/transactions.rb:307:in `save'
# /Users/melsatar/.rvm/gems/ruby-2.4.5/gems/activerecord-5.1.6/lib/active_record/suppressor.rb:42:in `save'
# /Users/melsatar/.rvm/gems/ruby-2.4.5/gems/activerecord-5.1.6/lib/active_record/persistence.rb:34:in `create'
# ./spec/mailers/user_mailer_messages_spec.rb:91:in `block (2 levels) in <top (required)>'
# ./spec/mailers/user_mailer_messages_spec.rb:89:in `block in <top (required)>'
# ./spec/mailers/user_mailer_messages_spec.rb:3:in `<top (required)>'
# /Users/melsatar/.rvm/gems/ruby-2.4.5/gems/activesupport-5.1.6/lib/active_support/dependencies.rb:286:in `load'
# /Users/melsatar/.rvm/gems/ruby-2.4.5/gems/activesupport-5.1.6/lib/active_support/dependencies.rb:286:in `block in load'
# /Users/melsatar/.rvm/gems/ruby-2.4.5/gems/activesupport-5.1.6/lib/active_support/dependencies.rb:258:in `load_dependency'
# /Users/melsatar/.rvm/gems/ruby-2.4.5/gems/activesupport-5.1.6/lib/active_support/dependencies.rb:286:in `load'

编辑:

如果只想返回对象数组中的export default connect(mapStateToProps, { setWarning })(WarningModal); 属性(没有const RoomsDetails = [ { "RoomId" : "37A", "NumberOfBeds" : "4" }, { "RoomId" : "38A", "NumberOfBeds" : "5" }, { "RoomId" : "39A", "NumberOfBeds" : "4" } ] const ActiveRoomsIds = ["37A", "39A"] const Active = RoomsDetails.filter( room => ActiveRoomsIds.includes(room.RoomId) ) console.log(Active);),则可以这样做:

RoomId