Home > Software engineering >  Filter Data to match a list of keys
Filter Data to match a list of keys

Time:12-03

I have some code that will filter data to match a specified set of keys. So far, my code handles whether the data passed in is an array or an object of a specific kind. For example, a car object or an array of car objects, and the key list would be car attributes. However, I've just gotten a new dataset and I'm wondering the best way to go about handling it. Here is my code so far (don't worry too much about my array/object validation, I have custom functions that do much more checking)

const goodDataObject = {
  "make": "toyota",
  "model": "rav4",
  "color": "white"
  "mileage": "10000",
  "engine": "hybrid"
};

const goodDataArray = [
  {
    "make": "toyota",
    "model": "rav4",
    "color": "white"
    "mileage": "10000",
    "engine": "hybrid"
  },
  {
    "make": "chevrolet",
    "model": "cruze",
    "color": "black"
    "mileage": "20000",
    "engine": "gas"
  }
]

const badDataObject = {
  "dealer": "John Does Cars",
  "factory": "Michigan",
  "cars": [
    {
      "make": "toyota",
      "model": "rav4",
      "color": "white"
      "mileage": "10000",
      "engine": "hybrid"
    },
    {
      "make": "chevrolet",
      "model": "cruze",
      "color": "black"
      "mileage": "20000",
      "engine": "gas"
    }
  ]
}


// -->> results from goodDataObject:
{
  "make": "toyota",
  "model": "rav4"
}


// -->> results from goodDataArray:
[
  {
    "make": "toyota",
    "model": "rav4"
  },
  {
    "make": "chevrolet",
    "model": "cruze"
  }
]


// -->> results I WANT from badDataObject:
{
  "dealer": "John Does Cars",
  "factory": "Michigan",
  "cars": [
    {
      "make": "toyota",
      "model": "rav4"
    },
    {
      "make": "chevrolet",
      "model": "cruze"
    }
  ]
}

Here is my current code called with keys ["make", "model"].

function filterData(data, keys) {
  let filtered = null;
  
  if (typeof data === 'object') {
    for (let x of keys) {
      filtered[x] = data[x]
    }
  } else if (Array.isArray(data)) {
    filtered = [];
    for (let y of data) {
      let filteredObject = {};
      for (let key of keys) {
        filteredObject[key] = y[key];
      }
      filtered.push(filteredObject);
    }
  }
  return filtered;
}

I need to handle the badDataObject, and I've been thinking of a recursive option, but that has not been successful so far so I can't post much there

Update: The keys are examples, but I have to handle all. Also, the badDataObject is an example. cars could be vans or trucks etc.

CodePudding user response:

Should be easy to do using Array.map().

const badDataObject = {
  "dealer": "John Does Cars",
  "factory": "Michigan",
  "cars": [{
      "make": "toyota",
      "model": "rav4",
      "color": "white",
      "mileage": "10000",
      "engine": "hybrid"
    },
    {
      "make": "chevrolet",
      "model": "cruze",
      "color": "black",
      "mileage": "20000",
      "engine": "gas"
    }
  ]
};

function goodDataObject(data) {
  data.cars = data.cars.map(car => ({
    model: car.model,
    make: car.make,
  }));
  return data;
}

console.log(goodDataObject(badDataObject))
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

CodePudding user response:

If you are sure that badDataObject is going to contain the key 'cars', then you should just filter by extracting that key only.

Otherwise you can write a recursive function that determines whether any key has the data(array or object) that contains desired keys but I think that would be an overkill and difficult to manage.

  • Related