0

I have a nested object of this structure.

mainObject: {
   1: {
         id: 1,
         name:'alpha'
      },
   2: {
         id: 2,
         name:'beta'
      },
   3: {
         id: 3,
         name:'gamma'
        
      }
}

i want to delete on object from this ,say 1 but that in an immutable way . I tried the following code.

    const list =Object.values(mainObject)
    const newList=list.filter((item)=>{item.id!=1})
    newList.forEach((item)=>{
        mainObject[item.id]={
          id: item.id,
          name:item.name
        }
    })

this is not working .What am i doing wrong. Thanks in advance.

8
  • so, delete mainObject[1] doesn't work? Commented Sep 12, 2020 at 3:35
  • it is working but immutability is required Commented Sep 12, 2020 at 3:36
  • Are you wanting to remove the object with the id of 1, or the object at key 1? Commented Sep 12, 2020 at 3:38
  • explain what you mean by immutability in this context - by the way const newList=list.filter((item)=>{item.id!=1}) will ALWAYS be an empty list since your callback doesn't return anything Commented Sep 12, 2020 at 3:39
  • object at key 1 Commented Sep 12, 2020 at 3:39

2 Answers 2

2

You could use destruct assignment for deletion

const { 1: _, ...newObject } = mainObject

const mainObject = {
  1: {
    id: 1,
    name: "alpha",
  },
  2: {
    id: 2,
    name: "beta",
  },
  3: {
    id: 3,
    name: "gamma",
  },
}

const { 1: _, ...newObject } = mainObject

console.log(mainObject)
console.log(newObject)

Or clone mainObject in to an different object then apply deletion

const newObject = { ...mainObject }
delete newObject[1]

const mainObject = {
  1: {
    id: 1,
    name: "alpha",
  },
  2: {
    id: 2,
    name: "beta",
  },
  3: {
    id: 3,
    name: "gamma",
  },
}

const newObject = { ...mainObject }
delete newObject[1]

console.log(mainObject)
console.log(newObject)

Or transform object into array of key-value pairs, reject your delete pair and then transform it back to object

const newObject = Object.fromEntries(
  Object.entries(mainObject).filter(([key]) => key !== "1")
)

const mainObject = {
  1: {
    id: 1,
    name: "alpha",
  },
  2: {
    id: 2,
    name: "beta",
  },
  3: {
    id: 3,
    name: "gamma",
  },
}

const newObject = Object.fromEntries(
  Object.entries(mainObject).filter(([key]) => key !== "1")
)

console.log(mainObject)
console.log(newObject)


Reference

Destructuring assignment

Spread operator (...)

Object.prototype.entries()

Object.prototype.fromEntries()

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

4 Comments

note, in both cases newObject is a shallow copy
my key needs to be a number ..can it be modified
@Deepakmaurya this part key !== "1"?
yes my key is a number so in side newObject i need it as a number @hgb123
0

You can use delete operator and spread syntax to achieve what you want. For the object you have mentioned, let me walk you through it.

const mainObject = {
  1: {
    id: 1,
    name: 'alpha',
  },
  2: {
    id: 2,
    name: 'beta',
  },
  3: {
    id: 3,
    name: 'gamma',
  },
};

const newObj = { ...mainObject};
delete newObj["1"];

Read More on Spread syntax on MDN

Read More on delete operator on MDN

Hope it helps🙂

const mainObject = {
  1: {
    id: 1,
    name: 'alpha',
  },
  2: {
    id: 2,
    name: 'beta',
  },
  3: {
    id: 3,
    name: 'gamma',
  },
};
const newObj = { ...mainObject};
delete newObj["1"];

document.write(`<p>mainObject:</p> <pre> ${JSON.stringify(mainObject)} </pre>`)
document.write(`<p>newObj:</p> <pre> ${JSON.stringify(newObj)} </pre>`)

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.