Q:

check if array of strings matches obj keys / properties

I want to match URL queries with object keys and values.
URL: ?cost=10&name=item
const query = { cost: 10, name: 'item' }
const keys = ['cost', 'name']
const values = ['10', 'item']

How can i check if keys[] matches Object.keys(query) and values[] matches Object.values(query)? I was trying with for..in and different combinations of .map(); .filter(); .indexOf() > -1 on both keys[] and values[]. 
Also 10 == '10' should return true.

A:

In order for the given query to be identical to given keys and values, (1) the length of both must be identical and (2) each query parameter must be contained in the given keys and values:

// Compare query keys and values:
function identical(query, keys, values) {
  return Object.keys(query).length == new Set(keys).size &&
         keys.every((key, i) => query[key] == values[i]);
}

// Example:
const query = {cost: 10, name: 'item'};
const keys = ['cost', 'name'];
const values = ['10', 'item'];

console.log(identical(query, keys, values));

