var filteredKeyItems = ['primary','gender','regular','normal']
var filteredValuesItems = ['genreal','M',true]
// Output should be:
filteredKeyItems = ['primary','gender']
filteredValuesItems = ['genreal','M']
这里有两个包含键和值的数组的片段。 如何在值数组中过滤值为true和normal的代码中表示的多个键。 最终输出应按照摘要中的指示进行。
谢谢。
答案 0 :(得分:0)
美容功能方式是:
var filteredKeyItems = ['primary','gender','regular','normal']
var filteredValuesItems = ['genreal','M',true]
function filterKeyItem(keyItem) {
return keyItem !== 'regular' && keyItem !== 'normal';
}
filteredValuesItems = filteredValuesItems.filter((valueItem, index) => filterKeyItem(filteredKeyItems[index]));
filteredKeyItems = filteredKeyItems.filter(filterKeyItem);
console.log('filteredKeyItems', filteredKeyItems);
console.log('filteredValuesItems', filteredValuesItems);
要稍微提高性能,可以使用一个简单的for
循环来填充结果数组。
答案 1 :(得分:0)
我们也可以按照以下步骤进行操作
var filteredKeyItems = ['primary','gender','regular','normal']
var filteredValuesItems = ['genreal','M',true]
let filterTwo=[]
let filterOne=filteredKeyItems.filter((element,index)=>{
//we can assign condition here
if(element=="primary" || element=="gender" ){
filterTwo.push(filteredValuesItems[index])
return element}} )
console.log(filterOne)
console.log(filterTwo)