根据键过滤出对象

时间:2019-08-07 15:20:10

标签: javascript ecmascript-6

const dontPickFrom = ['item3', 'item4'];

const data = {'item1': 1, 'item2': 2, 'item3': 3, 'item4': 4, 'item5': 5};

我有上述数据,并且希望不使用item3而摆脱item4delete。因此,最终产品将是 {'item1': 1, 'item2': 2, 'item5': 5}。我在下面做了,但是没有输出我想要的结果。

removeSomeValues() {
  return Object.keys(data).reduce((acc, key) => {
      if (!dontPickFrom.includes(key)) {
        return {...acc, [key]: data[key]};
      }
    }, {})
}

4 个答案:

答案 0 :(得分:2)

您需要一个else分支:

   if (!dontPickFrom.includes(key)) {
    return {...acc, [key]: data[key]};
  } else return acc; // <<<

答案 1 :(得分:0)

var dontPickFrom = ['item3', 'item4'];

var data = {
  'item1': 1,
  'item2': 2,
  'item3': 3,
  'item4': 4,
  'item5': 5
};


for (let item in data) {
  if (dontPickFrom.includes(item)) {
    data[item] = undefined;
  }
}

console.log(JSON.stringify(data));

答案 2 :(得分:0)

问题是您只返回一个地方,所以当它返回时,您将返回未定义的地方。

removeSomeValues() {
  return Object
           .keys(data)
             .reduce((acc, key) => 
               dontPickFrom.includes(key)
                 ? acc
                 : {...acc, [key]: data[key]}
}

并不是所有的js引擎都还支持formEntries,但是如果您使用的话,可以将其写为

const filteredObj = Object.fromEntries(Object.entries(data).filter(([key]) => !dontPickFrom.includes(key)))

答案 3 :(得分:0)

首先过滤掉它们吗?

import android.os.Bundle
import android.support.v4.app.Fragment
import android.support.v7.widget.Toolbar
import dagger.android.AndroidInjection
import dagger.android.DispatchingAndroidInjector
import dagger.android.support.HasSupportFragmentInjector
import javax.inject.Inject

class ConnectedActivity : AppCompatActivity(),HasSupportFragmentInjector {

@Inject
lateinit var dispatchingAndroidInjector: DispatchingAndroidInjector<Fragment>

override fun supportFragmentInjector(): DispatchingAndroidInjector<Fragment> {
    return dispatchingAndroidInjector
}


private fun configureDagger() {
    AndroidInjection.inject(this)
}
override fun onCreate(savedInstanceState: Bundle?) {
    configureDagger()
    super.onCreate(savedInstanceState)
    setContentView(R.layout.layout)
    if (savedInstanceState == null) {
        supportFragmentManager.beginTransaction()
                .replace(R.id.fragment, ConnectedFragment.newInstance())
                .commitNow()
    }
}

}

class ConnectedFragment : Fragment() {


companion object {
    fun newInstance() = ConnectedFragment()
}
@Inject
var factoryViewModel: ViewModelFactory?=null
lateinit var connectedViewModel: ConnectedViewModel

private fun configureDagger() {
    AndroidSupportInjection.inject(this)
}

override fun onCreate(savedInstanceState: Bundle?) {
    this.configureDagger()
    super.onCreate(savedInstanceState)

}
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
                          savedInstanceState: Bundle?): View {
    var rootView:View = inflater.inflate(R.layout.connected_fragment, container, false)
    connectedViewModel = ViewModelProviders.of(this,factoryViewModel).get(ConnectedViewModel::class.java)

    return rootView
}

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
    super.onViewCreated(view, savedInstanceState)
}


}