I'm trying to filter out unwanted objects from a parent object
But I'm being thrown an error of TypeError: Cannot delete property 'children' of #<Object>
Props passed like this:
<Column df={["between", "mt-0", "around"]} xl={["center"]}>...</Column>
Given to a utility function like this:
const Column = props => {
const x = filterComponentProps(props)
return stuff
}
The inside the utility function anything within the object not matched within const condition = ["df", "xl"] is to be removed.
export const filterComponentProps = props => {
let data = props
console.log(data)
// {df: Array(3), xl: Array(1), children: Array(6)}
// children: (6) [{…}, {…}, {…}, {…}, {…}, {…}]
// df: (3) ["between", "mt-0", "around"]
// xl: ["center"]
const condition = ["df", "xl"]
Object.keys(data).forEach(key =>
!condition.includes(key) ? delete data[key] : key
) //ERROR
return data
}
I set up a test to see if its a problem with the function but it works as expected:
const data = {
df: { name: "Part 1", size: "20", qty: "50" },
xl: { name: "Part 2", size: "15", qty: "60" },
children: { name: "Part 2", size: "15", qty: "120" },
}
const condition = ["df", "xl"]
Object.keys(data).forEach(key =>
!condition.includes(key) ? delete data[key] : key
)
console.log(data)
//{df: {…}, xl: {…}}
//df: {name: "Part 1", size: "20", qty: "50"}
//xl: {name: "Part 2", size: "15", qty: "60"}