logo
Tags down

shadow

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 :
$scope.removeCoffee=function(el){       
    $scope.coffees.splice(el,1);
}

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


By : EDWARD
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:
    Change:
    code :
    taskListArray.splice($.inArray($(this), taskListArray, 1));
    
    taskListArray.splice($(this).index(),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) {
        super(props)
        this.state = {
        adresses:[
      {
        "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: "",
          new_address:"",
    
        }
        this.setState(prevState => ({
          arrayOfAddresses: [...prevState.arrayOfAddresses, newInputValues],
        }))
      }
    
    deleteInput(idx) {
    
      this.setState(prevState => {
        let tempObj = [...prevState.arrayOfAddresses]
        tempObj.splice(idx, 1)
        console.log(tempObj) 
        return {
          arrayOfAddresses: tempObj,
        }
      })
    
    }
    
    onChooseAddress(e, idx) {
    const {value} = e.target;
        this.setState(state=>{
        let tempObj = [...this.state.arrayOfAddresses]
        tempObj[idx].new_address = value
          return {
              arrayOfAddresses: tempObj,
          }
        })
      }
    
      render() {
    
        return (
          <div>
            {this.state.arrayOfAddresses.map((item,idx)=>
        <div>
    
              <input
                name="contact"
                value={item.new_address}
                onChange={(e) => this.onChooseAddress(e, idx)}
              />
              <button onClick={() => this.deleteInput(idx)}> x</button>
        </div>
            )}
            <button  onClick={() => this.addNewInputs()}> Add new input </button>
          </div>
        )
      }
    }
    
    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
      this.state={
        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) {
        arrayToManipulate.push(items[itemIndex]);
      } 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
      return(
        <div>
          <Table>
            <thead>
              <tr>
                <th>
                  <div class="row">
                    <div class="col-md-2">
                      <input
                        onChange={this.selectAll}
                        type="checkbox"
                        checked={this.state.selectAll}
                      />
                    </div>
                  </div>
                </th>
                <th>TransactionId</th>
                <th>PNR</th>
                <th>Source</th>
                <th>InvoiceNo</th>
              </tr>
            </thead>
            <tbody>
              {
                items.map((item, i) => (
                  <tr key={i}>
                    <td id="col1" align="center">
                      <input
                        type="checkbox"
                        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>
                    <td id="col1" align="center">  
                      {item.ID}
                    </td>
                    <td id="col2" align="center">
                    {item.PNR}
                    </td>
                    <td id="col5" align="center">
                    {item.MAP}
                    </td>
                    <td id="col6" align="center">
                    {item.Number}
                    </td>
                </tr>))
              }
            </tbody>
          </Table>
        </div>
      )
    }
    
    Related Posts Related Posts :
  • How to decrypt the encrypted UID using firestore security rules?
  • javascript - if and or (||) statement
  • How to get overlay to position on an image?
  • cannot set checked of undefined (saving checkboxs w/ localStorage)
  • how can I send a data by ajax and the url has a variable value in the middle of the link
  • React Click Counter: Updating State of just one element
  • Having trouble with undefined output from this code below
  • Showing an image based on a number range in Javascript
  • Check which condition in IF statement is true
  • How do I reset the numbers after calculating the Total
  • Site loads slowly
  • How to insert href URL to Javascript var?
  • How can I use an array across multiple files?
  • Difference between $('.huyen') and $('.huyen')[0]
  • Using two array values in foreach loop
  • navigator.mediaDevices.enumerateDevices() returns empty labels
  • JavaScript positional property access in Objects
  • Is this possible to setInterval with custom ID so I can clear it later on from another services?
  • How to focus bootstrap select picker
  • Calling firebase functions returns null
  • Set a variable equal to null if it is not included in an array?
  • Remove Duplicate object from Array Deep Check
  • Check if array of objects key is empty and splice it
  • How to find next birthday date from list of date from date now?
  • Reorder array of objects based on array of strings?
  • Replace ISO-Date with normal Date in HTML Document via Widget
  • React setState not changing the state
  • Bitwise operations in JavaScript
  • react slide out menu state = not setting state?
  • .map() function and deleting a row from a table with ReactJS
  • Vanilla JavaScript in Svelte
  • Filter My Array / JSON Data (including multi values) Based on Multiple Criteria in JavaScript?
  • Unnecessary rerenders with useContext
  • Implement HTML input phone mask XXX-XXX-XXXX
  • create variable by matching variable value to object value?
  • Parsing Data in Google Sheets From an Object
  • How to obtain a user input value from an image input in Javascript?
  • Is it posible to implement functions that use non blocking setTimeout synchronically?
  • What would be consider as my local scope in this function?
  • regex expression for string.split() that splits a string on urls
  • Vue props become null on $router.go(-1)
  • How can I set my local date in javaScript
  • Why my state does not change on click event with setState
  • How to fetch data from API using id in reactJS?
  • What is the problem in this recursive javascript function? [JS]
  • How to store & retrieve an object as a value in option of select element?
  • Select Text in DIV with specific range using JavaScript
  • JavaScript error executing a function: 2 arguments required but only 1 present
  • How to remove a specific character from array in JS?
  • Get Text content of 3rd <p> element inside <div> element
  • HTML li element will not except
  • How can I render nested objects and arrays in JSX?
  • using .find or .index of inside .find
  • Unable to add to an array of objects copied from state
  • Find an object properties by searching for keys by a string, and if one is true, return as true
  • Why does awaiting a setInterval promise inside of a while loop cause the amount of calls to stack up?
  • How to send data attribute to php variable using jquery?
  • In Javascript or jQuery how to scroll so that an element inside another div is centered vertically and horizontally
  • How to create an animated stackoverflow icon
  • Why is my javascript function being printed as undefined?
  • shadow
    Privacy Policy - Terms - Contact Us © voile276.org