Ackman
Ackman

Reputation: 1592

removing elements from an array within an array

****************UPDATED*********************************************************

I have an array under an array: enter image description here

I want to remove an element from children array where type="tag" and name="style". children array is an array within the array of objects printed below.

My aim is also to keep the original array of objects intact.

enter image description here

I am using the following code but it is giving me undefined:

// ARRAY of Objects
const responseText = html.parse(businessResponseText);


console.log('responseText',responseText);

// Needs work ---- how do I transform this original object?
const transformedObject = {
...responseText, 
children: responseText.map((children)=>{
  children.children.filter(
    el => el.type !== "tag" && el.name !== "style" 
)
})

}

console.log('transformedobject',transformedObject);

this is the output I get: original vs transformed

enter image description here

Upvotes: 1

Views: 82

Answers (2)

Travis James
Travis James

Reputation: 1939

const transformedList = styleFree.map(obj => ({
    ...obj, 
    children: obj.children.filter(
        el => el.type !== "tag" && el.name !== "style" 
    )
})

Upvotes: 2

Premkumar chalmeti
Premkumar chalmeti

Reputation: 1018

Copy and paste the code snippet as it would be easy for others to try it. Make the world better place! ;)

stylefree = stylefree.map((children) => {
    children.children = children.children.filter((nested)=>{
        return (nested.type!="tag" && nested.name!="style");
    });
    return children;
});

Upvotes: 1

Related Questions