0

This is my dataFilter. On basis of this I am trying to remove keys from my data. I want to remove keys that are mentioned in responseKeys and ratings array.

I am stuck and unable to remove keys from ratings array. Is there any way I can do recursion for this problem.

let dataFilter = {
  "webPage1": {
    "responseKeys": ["releaseDate", "filmName"]
  },
  "webPage2": {
    "responseKeys": ["releaseDate", "ratings"],
    "ratings": [
      "star2",
      "star3"
    ]
  }
}
let data = [{
    "filmName": "Avatar",
    "releaseDate": "2021-09-24",
    "day": "24",
    "month": "Sep",
    "ratings": [{
        "star1": "40",
        "star2": "23",
        "star3": "56",
        "star4": "45"
      },
      {
        "star1": "40",
        "star2": "23",
        "star3": "56",
        "star4": "45"
      },
      {
        "star1": "40",
        "star2": "23",
        "star3": "56",
        "star4": "45"
      }
    ]
  },
  {
    "filmName": "Morgan",
    "releaseDate": "2021-03-24",
    "day": "24",
    "month": "Mar",
    "ratings": [{
        "star1": "40",
        "star2": "23",
        "star3": "56",
        "star4": "45"
      },
      {
        "star1": "40",
        "star2": "23",
        "star3": "56",
        "star4": "45"
      },
      {
        "star1": "40",
        "star2": "23",
        "star3": "56",
        "star4": "45"
      }
    ]
  }
]

const filterResponse = (data, dataFilter = '') => {
  if (dataFilter) {
    let filterArray = [];
    data.forEach((item) => {
      let response = Object.keys(item)
        .filter(key => dataFilter.responseKeys.includes(key))
        .reduce((obj, key) => {
          obj[key] = item[key];
          return obj;
        }, {});
      filterArray.push(response);
    });
    return filterArray;
  }
  return data;
};

console.log(filterResponse(data, dataFilter['webPage1']));

trying to filter my data depending upon my json config.

Expected Output

//webPage1
[{
    "filmName": "Avatar",
    "releaseDate": "2021-09-24"
  },
  {
    "filmName": "Morgan",
    "releaseDate": "2021-03-24"
  }
]

//webPage2
[{
    "releaseDate": "2021-09-24",
    "ratings: [{
    "star2": "23",
    "star3": "56"
  },
  {
    "star2": "23",
    "star3": "56"
  },
  {
    "star2": "23",
    "star3": "56"
  }
]
}, {
  "releaseDate": "2021-03-24",
  "ratings": [{
    "star2": "23",
    "star3": "56"
  }, {
    "star2": "23",
    "star3": "56"
  }, {
    "star2": "23",
    "star3": "56"
  }]
}]

2 Answers 2

1

You tried well, but you did not implement ratings filter...

let data = [{ "filmName": "Avatar", "releaseDate": "2021-09-24", "day": "24", "month": "Sep", "ratings": [{ "star1": "40", "star2": "23", "star3": "56", "star4": "45" }, { "star1": "40", "star2": "23", "star3": "56", "star4": "45" }, { "star1": "40", "star2": "23", "star3": "56", "star4": "45" }] }, { "filmName": "Morgan", "releaseDate": "2021-03-24", "day": "24", "month": "Mar", "ratings": [{ "star1": "40", "star2": "23", "star3": "56", "star4": "45" }, { "star1": "40", "star2": "23", "star3": "56", "star4": "45" }, { "star1": "40", "star2": "23", "star3": "56", "star4": "45" }] }]
let filterResponse = (data, dataFilter) => data.map(v => {
    let o = {}
    dataFilter.forEach(item => o[item] = v[item])
    return o
})

console.log("webPage1", filterResponse(data, ["releaseDate", "filmName"]));
console.log("webPage2", filterResponse(data, ["releaseDate", "ratings"]).map(item => {
    item.ratings = filterResponse(item.ratings, ["star2", "star3"])
    return item
}))

Sign up to request clarification or add additional context in comments.

4 Comments

Problem is rating is dynamic. That can be in any object or not.
what do you mean by "rating is dynamic"?
Like for webPage2 you added map method. We will not know which object will have rating or not.
this is not a problem, its just an abstraction... now use that abstraction for your need,
0

Steps


I destructure the filter argument to seperate the keys that don't need any more filtering (responseKeys) from the ones that'll do (like the ratings)

const filterResponse = (data, { responseKeys, ...dataFilter }) =>
  ...

Using Array#map to loop through the data and using Object.entries to get the keys, I filter through them with Array#includes to get the ones you want back.

data.map((item) =>
  ...
  Object.entries(item).filter(([key]) => responseKeys.includes(key))

Looping through the filtered data again, if that key does not exist in the dataFilter var, it doesn't need to be filtered again so return the prop.

(...).map((prop) => {
  const [key, value] = prop;
  if (dataFilter[key] === undefined) return prop;

Else, I return an array with the key and value which I filter by getting the keys and checking if the key is required with Array#includes

  return [key, value.map((cur) => Object.fromEntries(Object.entries(cur).filter(([curKey]) => dataFilter[key].includes(curKey))))];
});

I then use Object.fromEntries to convert the [key, value] pairs back to objects

let dataFilter = {"webPage1":{"responseKeys":["releaseDate","filmName"]},"webPage2":{"responseKeys":["releaseDate","ratings"],"ratings":["star2","star3"]}};

let data = [{"filmName":"Avatar","releaseDate":"2021-09-24","day":"24","month":"Sep","ratings":[{"star1":"40","star2":"23","star3":"56","star4":"45"},{"star1":"40","star2":"23","star3":"56","star4":"45"},{"star1":"40","star2":"23","star3":"56","star4":"45"}]},{"filmName":"Morgan","releaseDate":"2021-03-24","day":"24","month":"Mar","ratings":[{"star1":"40","star2":"23","star3":"56","star4":"45"},{"star1":"40","star2":"23","star3":"56","star4":"45"},{"star1":"40","star2":"23","star3":"56","star4":"45"}]}];

const filterResponse = (data, { responseKeys, ...dataFilter }) =>
  data.map((item) =>
    Object.fromEntries(
      Object.entries(item)
        .filter(([key]) => responseKeys.includes(key))
        .map((prop) => {
          const [key, value] = prop;
          if (dataFilter[key] === undefined) return prop;
          return [
            key,
            value.map((cur) => Object.fromEntries(Object.entries(cur).filter(([curKey]) => dataFilter[key].includes(curKey))))
          ];
        })
    )
  );

console.log(filterResponse(data, dataFilter['webPage2']))

1 Comment

of course, use cases of this code is limited... just try to solve a simple problem to solve much bigger problem...

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.