0

I am having an array of objects with the following structure:

const timeOptions = [
  { key: "all", value: "all", text: "All" },
  { key: "month", value: "month", text: "Month" },
  { key: "year", value: "year", text: "Year" }
];

I am creating a new object with this timeOptions being a value of a field in it. How to remove the object with key "all" in-place while creating the object itself

const values = {
  timeVal: {
    field: "timestamp",
    label: "Timestamp",
    options: timeOptions
  }
};

Code that I tried


const values = {
  timeVal: {
    field: "timestamp",
    label: "Timestamp",
    options: timeOptions.forEach((k) => delete k["key"] === "all")
  }
};

2 Answers2

1

Using Array#filter:

options: timeOptions.filter(({ key }) => key !== 'all')
Majed Badawi
  • 27,616
  • 4
  • 25
  • 48
1

If I understand you correctly, you need filter

const values = {
 timeVal: {
 field: "timestamp",
 label: "Timestamp",
 options: timeOptions.filter((k) =>  k["key"] !== "all")
}

};

This will copy all except the one with key "all"

delalluyah
  • 67
  • 4