-1

I have the below object, and want to remove the element("virAddrSeq": "345").

var state={
  "todos": [
    {
      "accountNo": "50190000",
      "name": "Sarkar",
      "vpainfo": [
        {
          "virAddrSeq": "345"
        },
        {
          "virAddrSeq": "34775"
        }
      ]
    }
  ]
}

I have tried the below way but getting all the records with out removing the element.

const newObj = Object.assign({}, state, {
        todos: state.todos.filter(todoObj => (todoObj.vpainfo.filter(({virAddrSeq}) => (virAddrSeq != "345"))))
        })

console.log(newObj)
jabaa
  • 5,844
  • 3
  • 9
  • 30
vishnu
  • 4,377
  • 15
  • 52
  • 89
  • [What is the difference between JSON and Object Literal Notation?](https://stackoverflow.com/questions/2904131/what-is-the-difference-between-json-and-object-literal-notation) – jabaa Sep 28 '21 at 08:18

2 Answers2

3

var state = {
  "todos": [{
    "accountNo": "50190000",
    "name": "Sarkar",
    "vpainfo": [{
        "virAddrSeq": "345"
      },
      {
        "virAddrSeq": "34775"
      }
    ]
  }]
}

console.log(

state.todos.map(todo => ({...todo, vpainfo: todo.vpainfo.filter(({virAddrSeq}) => virAddrSeq!= 345)}))

)
Saeed
  • 5,413
  • 3
  • 26
  • 40
0

var state = {
  "todos": [{
    "accountNo": "50190000",
    "name": "Sarkar",
    "vpainfo": [{
      "virAddrSeq": "345"
    },
    {
      "virAddrSeq": "34775"
    }
    ]
  }]
}
for (const s of state.todos) {
 let findKey =s.vpainfo.find(x => x.virAddrSeq == '345')
 let index = s.vpainfo.indexOf(findKey)
 if(findKey && index > -1) s.vpainfo.splice(index,1)
}
console.log(state)
Reza Salari
  • 101
  • 2
  • 8