{"ScriptPreparationCode":null,"TestCases":[{"Name":"Foreach test","Code":"\r\nlet items = [\r\n {color: \u0027red\u0027, type: \u0027tv\u0027, age: 18}, \r\n {color: \u0027silver\u0027, type: \u0027phone\u0027, age: 20},\r\n {color: \u0027blue\u0027, type: \u0027book\u0027, age: 17}\r\n] \r\nconst excludes = [ \r\n {k: \u0027color\u0027, v: \u0027silver\u0027}, \r\n {k: \u0027type\u0027, v: \u0027tv\u0027}, \r\n] \r\nfunction excludeItems(items, excludes) { \r\n excludes.forEach( pair =\u003E { \r\n items = items.filter(item =\u003E item[pair.k] === item[pair.v])\r\n })\r\n \r\n return items\r\n} \r\nconsole.log(excludeItems(items, excludes))","IsDeferred":false},{"Name":"Map","Code":"let items = [\r\n {color: \u0027red\u0027, type: \u0027tv\u0027, age: 18}, \r\n {color: \u0027silver\u0027, type: \u0027phone\u0027, age: 20},\r\n {color: \u0027blue\u0027, type: \u0027book\u0027, age: 17}\r\n] \r\nconst excludes = [ \r\n {k: \u0027color\u0027, v: \u0027silver\u0027}, \r\n {k: \u0027type\u0027, v: \u0027tv\u0027}, \r\n]\r\n\r\nfunction excludeItems(items, excludes) {\r\n // m k n\r\n // n * m\r\n \r\n // change exclude to Map\u003Ckey, Set\u003Cvalue\u003E\u003E\r\n // m * k * 1\r\n \r\n // preprocess excludes array\r\n // avoid multiple for loop on items\r\n \r\n const excludeMap = new Map()\r\n for (let {k, v} of excludes) {\r\n if (!excludeMap.has(k)) {\r\n excludeMap.set(k, new Set())\r\n }\r\n excludeMap.get(k).add(v)\r\n }\r\n \r\n return items.filter(item =\u003E {\r\n return Object.keys(item).every(\r\n key =\u003E !excludeMap.has(key) || !excludeMap.get(key).has(item[key])\r\n )\r\n })\r\n}\r\nconsole.log(excludeItems(items, excludes))","IsDeferred":false}]}