0

I want to make few tables from an Array using dropdown list. List will be Daily, weekly, Monthly, yearly.

I have written the code. Below is the sample array.

 var allPoints = records.map(
      (
        { id, sector_name, f_day_lot1, f_day_lot2, f_day_lot3, f_day_lot4 }
      ) => (
          { id, sector_name, f_day_lot1, f_day_lot2, f_day_lot3, f_day_lot4 }
        )
    );

The thing is I want to change the above day to weekly, monthly or yearly depends on the dropdown. I can write same code for multiple times changing day to weekly, monthly or yearly. But then it will be alot of code. So, is there any other better way to do.

Or how to make day as a variable and change value depending upon menu.

2
  • Do you have day state known everytime? Commented Sep 9, 2020 at 12:47
  • i need to make it dynamic. so it can be a variable . Commented Sep 9, 2020 at 12:54

1 Answer 1

1

I'm not sure what is the exact data structure of your records. But I think you can use a dynamic key name:

function getPoints(records, dropdownValue) {
  return records.map((item) => {
    let mapedItem = {};
    for (const key in item) {
      if (key.indexOf(`${dropdownValue}_lot`) !== -1) {
        mapedItem[key] = item[key];
      }
    }
    mapedItem["id"] = item["id"];
    mapedItem["sector_name"] = item["sector_name"];
    return mapedItem;
  });
}
const data = [
  {
    id: 1,
    sector_name: 2,
    f_day_lot1: "f_day_lot1",
    f_day_lot2: "f_day_lot2",
    f_day_lot3: "f_day_lot3",
    f_day_lot4: "f_day_lot4",
    f_month_lot1: "f_month_lot1",
    f_month_lot2: "f_month_lot2",
    f_month_lot3: "f_month_lot3",
    f_month_lot4: "f_month_lot4",
    f_week_lot1: "f_week_lot1",
    f_week_lot2: "f_week_lot2",
    f_week_lot3: "f_week_lot3",
    f_week_lot4: "f_week_lot4"
  }
];
console.log(getPoints(data, "day"));
console.log(getPoints(data, "week"));
console.log(getPoints(data, "month"));

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.