0

My objective is to remove an array[] from an object. I couldn't able to figure out how to remove the particular array from the object. Can anyone help me with this query?

Here is the following object:

experience: [
    {
        organisation: "",
        id: "",
        from: "",
        to: "",
        skills: []
    }
]

Experience array having an object, it may have multiple objects.

experience: [
    {
        organisation: "",
        id: "",
        from: "",
        to: "",
        skills: []
    },
    {
        organisationName: "",
        id: "",
        from: "",
        to: "",
        skills: []
    }
]

I want to remove the skills array from every object.

I have tried in this way but it is showing undefined.

 let skillsArray = this.state.experiences.forEach((item) => item.skills! == Array.isArray([]))

Can anyone please help me with this query?

3 Answers 3

1

To achieve what you are trying to do, you need to use the map method. This method goes through each element of the array and returns with a new array where each element is what your function returns.

forEach will go through all the items in your array and execute what you pass as a function for each element. In your code you are just doing a comparison which doesn't affects anything really.

To answer your question:

let skillsArray = this.state.experiences.map((item) => {
  delete item.skills;
  return item;
})

But notice that in this example I'm also modifying the original array, so it might be better to copy each item and modify that instead.

let skillsArray = this.state.experiences.map((item) => {
  let newItem = { ...item }
  delete newItem.skills;
  return newItem;
})

One more thing, this question is really about Javascript. React is not actually involved in any of this.

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

4 Comments

HI, Actually i want to remove the skills item when i submit the form. When i use the above answer for submitting form then it is showing error like skills are undefined Cannot read property 'map' of undefined. It is removing everywhere i think
I guess you are using the first version of my answer. On the second one I'm copying the object so it doesn't modify the original.
But we're not assigning newItem anywhere
Hi, how to delete multiple properties from object? if i give delete newItem.skills; delete newItem.to then i'm getting in this way "The operand of a 'delete' operator must be optional."
1

Map the experience array, destructure the skills property, and use rest syntax to collect the rest of the properties in an object, and return it:

const state = {
  experience: [{
    organisation: "",
    id: "",
    from: "",
    to: "",
    skills: []
  }]
};

const newState = {
  ...state,
  experience: state.experience.map(
    ({ skills, ...rest }) => rest
  )
};
console.log(newState);

6 Comments

Hi, It is giving every variable available in state. but not removing skills from experience array
It is indeed removing the skills property from every object in the array - run the snippet to see for yourself. Are you sure you copied the code properly?
Yes, i copied the code, i'm using class component. In Submit method, when i use the above one, it is showing every state variable in the console
Because that's what the code is doing basically at the last line. On this answer newState is your skillsArray and state is your this.state
@tehsis - I have done that but still getting the same output
|
0

You can filter your list by the issue you want, and it will be auto removed, for example, if you want to remove all items = 3 :

list: prevState.list.filter(x=> x != 3);

Good luck!

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.