Tags down


array.splice() deleting wrong element

By : Shirley Smith
Date : August 02 2020, 10:00 AM
I wish this helpful for you Probably the index you're passing is incorrect,
Array.filter would be more appropriate for this kind of task :
code :
removePara = index => {
  let array = this.state.paras.filter(e => e.props.id !== index);
  this.setState({ paras: array });

Share : facebook icon twitter icon

Array splice removes the wrong element in AngularJS

By : Gavin Pon
Date : March 29 2020, 07:55 AM
With these it helps splice expects start/count integers. $scope.coffees[el] is an object but you're passing in the $index to the method. Update your remove method as follows:
code :

Polymer 1.0 array splice is deleting the wrong item in array

By : Bhavesh Akhade
Date : March 29 2020, 07:55 AM
hope this fix your issue The first issue is that you always insert the same reference to the object this.todo in your _addTodo() method. Instead you should make a copy of the object. You sould also add an ID to differentiate it from the other items.
code :
  _addTodo: function() {
    var copy = Object.assign({}, this.todo);    
    copy.id = this.push('todos', copy);
    // this.todo = {};
<paper-card class="todos" id="{{item.id}}">
  _destroyAction: function(ev) {
    var index = this.todos.findIndex( function(item) { 
        return item.id == ev.target.parentElement.id 
    } );
    this.splice('todos', index, 1);

Save class in array + splice deleting wrong object

Date : March 29 2020, 07:55 AM
wish helps you Try using the
  • index for your splice since the actual element is not what is in the array:
    code :
    taskListArray.splice($.inArray($(this), taskListArray, 1));

  • Splice() deletes wrong element in array

    By : user2729798
    Date : March 29 2020, 07:55 AM
    Hope that helps The problem is with the chooseAddress method, you're not passing the index from the onChange callback, that's why the state is not updating, and also you have not added value prop to the input, that's why rendering was wrong, because of input's internal state
    code :
    class TodoApp extends React.Component {
      constructor(props) {
        this.state = {
        "id": 1,
        "address": "address 1",
        "id": 2,
        "address": "address 2",
        "id": 3,
        "address": "address 3",
        "id": 4,
        "address": "address 4",
            arrayOfAddresses: [
              datetime: "10.05.2019 14:00",
              position_id: 1,
              contact: "",
              address_id: "",
              new_address: "",
      addNewInputs() {
        let newInputValues = {
          datetime: "10.05.2019 14:00",
          position_id: 1,
          contact: "",
          address_id: "",
        this.setState(prevState => ({
          arrayOfAddresses: [...prevState.arrayOfAddresses, newInputValues],
    deleteInput(idx) {
      this.setState(prevState => {
        let tempObj = [...prevState.arrayOfAddresses]
        tempObj.splice(idx, 1)
        return {
          arrayOfAddresses: tempObj,
    onChooseAddress(e, idx) {
    const {value} = e.target;
        let tempObj = [...this.state.arrayOfAddresses]
        tempObj[idx].new_address = value
          return {
              arrayOfAddresses: tempObj,
      render() {
        return (
                onChange={(e) => this.onChooseAddress(e, idx)}
              <button onClick={() => this.deleteInput(idx)}> x</button>
            <button  onClick={() => this.addNewInputs()}> Add new input </button>
    ReactDOM.render(<TodoApp />, document.querySelector("#app"))

    Splice in javascript is not deleting the correct element

    By : Lomax
    Date : March 29 2020, 07:55 AM
    it should still fix some issue As a rule you probably don't want to splice an array directly from the state (react works using an immutable philosophy). In addition, you probably shouldn't constantly modify a global array. Using the spread function you can create a copy of your this.state.singleCheckedList and play with it freely.
    code :
    constructor(props) { 
      super(props); // I know you aren't using any props in this class but as a best practice, you should pass the props in your ctor so you won't forget
        singleCheckedList: [],
        items: [
          {MAP: "RTO", PNR: "OCYH", ID: "7311ba56-730a-478e-af6e-607d8dee6b0c", Number: "76809", checked: false},
          {MAP: "RTO", PNR: "OCYH", ID: "280515e8-22e1-4365-92a5-20b8e5b8b62d", Number: "87661821", checked: false},
          {MAP: "RTO", PNR: "ISMP", ID: "80f71c18-b530-46ca-9acb-798f73dcdde3", Number: "6343279", checked: false},
          {MAP: "RTO", PNR: "ISMP", ID: "a9c607a3-d2f9-4643-822b-b9f25c229b92", Number: "5483292", checked: false},
          {MAP: "RTO", PNR: "ISMP", ID: "e938c2c9-6eb9-472f-8e3a-d4529fd55f16", Number: "67698094", checked: false},
          {MAP: "RTO", PNR: "ISMP", ID: "55ea118b-beea-4407-84cf-3aebc98bffd2", Number: "15201901", checked: false},
          {MAP: "RTO", PNR: "TNAT", ID: "387b3710-901d-45f2-a38b-b875237e1210", Number: "15201902", checked: false}
        ] // You tried to reference your items list from the state but you didn't actually declare it as part of your class
      1. In your code you referenced transactionId so I assume you meant to put that here instead of Id
      2. I also added the itemIndex here so you won't need to traverse the entire items array again to change the checkedState
    handleCheckbox = (transactionId, checked, itemIndex) => {
      const items = [...this.state.items]; // Using the spread function (...) I can crate a duplicate of the original array to manipulate freely
      const arrayToManipulate = [...this.state.singleCheckedList]; // Again, using the spread function
      items[itemIndex].checked = checked; // Now that we have the passed index you don't need to check that you are at the right item in order to change the checked status, just insert the new status
    if (checked) {
      } else {
        const index = arrayToManipulate.findIndex(item => item.ID === transactionId); // You are trying to find the matching index, so you should be checking for a matching pair ===
        arrayToManipulate.splice(index, 1);
      this.setState({ singleCheckedList: arrayToManipulate, items }); // React is smart, it will try to minimize the amount of times it touches the state, so combining your changes together will make sure you get the expected result
    render() {
      const { items } = this.state; // You referenced items in here while never actually bringing it from the state like you did in your handle function
                  <div class="row">
                    <div class="col-md-2">
                items.map((item, i) => (
                  <tr key={i}>
                    <td id="col1" align="center">
                        checked={item.checked} // the checked propery is a boolean, you might as well just pass it seeing as you are changing it in your handle function
                        onChange={this.handleCheckbox.bind(this, item.ID, !item.checked, i)} // Notice I added the item index prop
                    <td id="col1" align="center">  
                    <td id="col2" align="center">
                    <td id="col5" align="center">
                    <td id="col6" align="center">
    Related Posts Related Posts :
  • Why doesn't the .remove() method also affect the variable it's being set on?
  • React hooks error: Rendered more hooks than during the previous render
  • How to create a completely new Keyword in JavaScript?
  • When summing values from 2 arrays how can I cap the value in the new array?
  • A pop up window to login in
  • How to push spacebar action to array
  • How do i highlight a specific table row depending on the url
  • Truly Weak Reference Event Emitter / Dispatcher: is it possible?
  • Conditionally render a a background image based on current view using React Router
  • Can not retrieve component template while routing
  • How to keep the checkbox status after refreshing the page
  • Array of Object - divide information as per values in it
  • Calculating body style height (for horizontal scroll) in javascript not working
  • How to exclude certain values from randomly generated array
  • Change colour of selected <li>
  • Formatting date object in an Array with moment is giving an unexpected result
  • combineLatest operator alternative
  • 'object' is never reassigned. Use 'const' instead
  • What does the spread operator in ES6 convert to in older JavaScript? Is it costlier than array.concat?
  • Get duplicates in array of strings and count number of duplicates
  • How to fix React Redux and React Hook useEffect has a missing dependency: 'dispatch'
  • Call a ajax request only if there are ajax requests added to the list
  • Using the jQuery each function to count divs with the same class
  • FInding out the Harshad number
  • angular route is not working and not changing current view
  • Callback executed before function finishes execution
  • How to create subfolder and document on Firestore web?
  • Scroll algorithm -- improving fetch and display of data
  • New To Programming World
  • Using mongorestore to insert many documents into a temp collection
  • How to extend localStorage across devices (without DB)
  • How to do pre increment without using ++I?
  • Retrieve filterViewId from batchUpdate request addFilterView
  • What is the corresponding instanceof target for text nodes?
  • How to Test the API by Token on Postman?
  • How to set Bootstrap dropdown menu on hover?
  • In Slate.js editor.apply(operation) is not applying "split_node" operations correctly
  • No data in GET response
  • Javascript using if else to determine array value
  • Error when I run npm install Error: 404 Not Found: 7zip-bin@~4.1.0
  • I want to modified my JSON output using JS
  • Error in Entry module not found and in webpack
  • Submit button is not submitting the form after changing the button type
  • Why is the function created is not working.?
  • How can I do day timer with javascript?
  • Tap screen to trigger autofocus with getUserMedia
  • Unable to bind html table data to mvc controller Model
  • How can I simply work around a missing JS property in an object in an object?
  • How to add anything at a specific position in a string using vanilla javascript
  • using async methods inside of array.map() in javascript
  • Math.random() vs random() in Khan Academy Computer Programming
  • Call two functions with onPress in react native
  • Use a global variable inside promise javascript
  • Turn array of objects into array of properties
  • Would having a Pure Class concept make sense or not?
  • Why catch invoked with success promise?
  • Match whole word if it meets a condition
  • Inserting items into array with bracket syntax doesnt affect length?
  • adding value inside an input - React
  • How does setInterval() run independently of sequential execution?
  • shadow
    Privacy Policy - Terms - Contact Us © voile276.org