循环通过过滤器函数内的第二个数组

时间:2019-04-28 22:49:13

标签: javascript arrays

我在遍历import collections import random def min_and(seq): lst = list(seq) zero_index = collections.defaultdict(lambda: set(lst)) for x in lst: y = x while y: zero_index[y & ~(y - 1)].discard(x) y &= y - 1 visited = set() fringe = set(lst) i = 0 while 0 not in fringe: visited.update(fringe) fringe = { x & y for x in fringe for y in zero_index[x & ~(x - 1)] if x & y not in visited } i += 1 return i + len(lst) - 1 print(min_and( random.randrange(2**18) | random.randrange(2**18) | random.randrange(2**18) for i in range(100))) 数组函数内部的数组tabData时遇到麻烦。有什么建议吗?

这是我当前的代码行

filter

数组const filteredData = allData.filter(({ existingLabel }) => existingLabel === tabData[/* Im having trouble iterating through this array */].label);

的结构
allData

数组0: {existingLabel: "exams", x: "xyz", a:12345, ...many fields} 1: {existingLabel: "notes", x: "ska", a:67333, ...many fields} 2: {existingLabel: "quiz", x: "sf3", a:34355, ...many fields} 3: {existingLabel: "notes", x: "xhi", a:34253, ...many fields} 4: {existingLabel: "exams", x: "ojk", a:98527, ...many fields} 5: {existingLabel: "quiz", x: "qid", a:65287, ...many fields}

的结构
tabData
  

我尝试执行以下操作,但出现此错误“预期分配或函数调用,而是看到了表达式”

0:{tab:1 , label:'notes'} 
1:{tab:2 , label:'exams'}
2:{tab:3 , label:'quiz'}

3 个答案:

答案 0 :(得分:0)

您可以使用下面的代码来解决您的问题。当此代码循环通过allData时,它将检查当前元素的选项卡是否等于指定的选项卡。如果是,它将保留。如果不是,它将删除它。

let allData = [{existingLabel: "exams", x: "xyz", a:12345}, {existingLabel: "notes", x: "ska", a:67333}, {existingLabel: "quiz", x: "sf3", a:34355}, {existingLabel: "notes", x: "xhi", a:34253}, {existingLabel: "exams", x: "ojk", a:98527}, {existingLabel: "quiz",  x: "qid", a:65287}];
let tabData = [{tab:1 , label:'notes'}, {tab:2 , label:'exams'}, {tab:3 , label:'quiz'}];
let tab = 2;

const filteredData = allData.filter(({ existingLabel }) => tabData.filter(e => e.label == existingLabel)[0].tab == tab);

console.log(filteredData);

答案 1 :(得分:0)

使用当前提供的信息,这是不可能的。但是有了另外一条信息,这很简单。如果您已经知道要查找的标签的标签,则可以执行以下操作:

const filterToLabel = (allData) => (tabLabel) => 
  allData.filter(({existingLabel}) => existingLabel === tabLabel)

const allData = [{existingLabel: "exams", x: "xyz", a:12345}, {existingLabel: "notes", x: "ska", a:67333}, {existingLabel: "quiz",  x: "sf3", a:34355}, {existingLabel: "notes", x: "xhi", a:34253}, {existingLabel: "exams", x: "ojk", a:98527}, {existingLabel: "quiz",  x: "qid", a:65287}]
const tabData = [{tab:1 , label:'notes'}, {tab:2 , label:'exams'}, {tab:3 , label:'quiz'}]

console.log(filterToLabel(allData)('notes')) //=> ska/67333, xhi/34253

如果您不知道标签,但是知道标签号,这几乎就很简单:

const filterToTabId = (allData, tabData) => (tabId) => {
  const tab = tabData.find(({tab}) => tab == tabId)
  // if (!tab) // throw error or return empty array
  return allData.filter(({existingLabel}) => existingLabel === tab.label)
}

const allData = [{existingLabel: "exams", x: "xyz", a:12345}, {existingLabel: "notes", x: "ska", a:67333}, {existingLabel: "quiz",  x: "sf3", a:34355}, {existingLabel: "notes", x: "xhi", a:34253}, {existingLabel: "exams", x: "ojk", a:98527}, {existingLabel: "quiz",  x: "qid", a:65287}]
const tabData = [{tab:1 , label:'notes'}, {tab:2 , label:'exams'}, {tab:3 , label:'quiz'}]

console.log(filterToTabId(allData, tabData)(2)) //=> xyz/1234, ojk/98527

如果您知道当前选项卡数组中的索引,则可以执行类似的操作。

答案 2 :(得分:0)

  

谢谢大家的帮助:)这就是我解决问题的方式

const filteredData = allData.filter(({ existingLabel }) => {
            let returnedData = [];
            for (let i = 1; i < tabData.length; i++) {
                if (tab === i) {
                    returnedData = existingLabel === tabData[i].label;
                }
            }
            return returnedData;
});

const finalData = tab === 0 ? allData : filteredData;