logo
down
shadow

Array splice with unexpected results


Array splice with unexpected results

By : tahir abbas
Date : October 22 2020, 08:10 PM
With these it helps Your code starts at the last item and tries to remove four values after the last item. But there aren't four values after the last item. If you want to remove four from the end start earlier in the array:
code :
let obArr = [0, 1, 2, 3, 4, 5, 6, 7]

// Start from the 4th last and remove four:
obArr.splice(-4, 4)

console.log(obArr) 


Share : facebook icon twitter icon
Unexpected behavior of Array.prototype.splice

Unexpected behavior of Array.prototype.splice


By : user3869582
Date : March 29 2020, 07:55 AM
I wish did fix the issue. Great question. :)
In Javascript, Arrays are Objects, which means that Array indices are Object keys. And in Javascript, Object keys are strings.
code :
index === '2' 
index == 2 
JavaScript array - splice call within forEach unexpected result

JavaScript array - splice call within forEach unexpected result


By : mweb92
Date : March 29 2020, 07:55 AM
it fixes the issue I noticed a bug within my code and tried to reproduce it with simpler JavaScript shown below. The goal is to sort an array and compare element 'x' to 'y' but not waste time comparing 'y' to 'x'. Note that I am also using objects comparing properties in my other code. , Well, you start with
code :
[1, 2, 3, 5]
[2, 3, 5]
[2, 5]
[2, 5]
console.log(a.splice(ind, 0));
array.splice unexpected behavior with negative values index

array.splice unexpected behavior with negative values index


By : Song Chen
Date : March 29 2020, 07:55 AM
this will help Initially lets consider foo as ["a", "b", "c"] and foo.length is 3.
So when you are adding the element using foo.splice(-1,0,"x"), the index is calculated as foo.length - 1 which is 2.
code :
["a", "b", "c"]
  0    1    2
            ^
            |__________"x" added here 
["a", "b", "x", "c"]
  0    1    2    3
            ^
            |__________ "x" is removed
Array.splice() results in the value 'undefined'

Array.splice() results in the value 'undefined'


By : user3352908
Date : March 29 2020, 07:55 AM
it should still fix some issue I had to hard code a few values and remove your app.intent("Optionpicker", (conv, input) => { because app, conv, and input are undefined in this example.
code :
let conv = {};
//let choice = 2; // conv.parameters.choice;
// had to hard code values b/c app and conv are undefined in this example
conv.data = {};
conv.data.list = ['optionA', 'optionB', 'optionC', 'optionD'];
let selected = {};

async function listremover() {
  let index = conv.data.list.indexOf('optionB');
  //if (index > -1) {
  //conv.data.list = conv.data.list.splice(index, 1);
  //return conv.data.list;
  //}

  if (index > -1) {
    selected = conv.data.list.splice(index, 1);
    //return selected; // returns selected array
  }
  return conv.data.list; // returns remaining values of original array
}

listremover().then(function(res) {
  // removed `conv.add()` not sure where this is defined...
  //console.log(`Ok, your choice is ${selected}. The remaining options are: ${conv.data.list[0]}, ${conv.data.list[1]} and ${conv.data.list[2]}. Which one is next?`);
  console.log(res);
});
Array.splice duplicate edit and insert after , unexpected result

Array.splice duplicate edit and insert after , unexpected result


By : user3450616
Date : March 29 2020, 07:55 AM
With these it helps like said commentator above Because you're inserting the same element into the array. The object referenced by jani is in the array twice once you splice it in.
and you should create a new object, you can do it by method Object.assign() easy fix var jani = Object.assign({}, arr2[0]);
code :
var objOne = {
  x: 1,
  y: 2
};

// objOne -> { x: 1, y: 2 }

var objTwo = objOne;

// objOne -> { x: 1, y: 2 } <- objTwo

objTwo.x = 2;

// objOne -> { x: 2, y: 2 } <- objTwo (update object via objTwo variable)

objTwo = {};

// objOne -> { x: 2, y: 2 }, objTwo -> {}
Related Posts Related Posts :
  • jQuery target next of the same class in the whole DOM
  • encoding must be a valid encoding error in process encoding
  • How to do a date range to view data from selected date highcharts
  • How do I refactor this composed function with Ramda.js?
  • jQuery to serialize selected option's value, not label
  • Convert object to Chartis.js structure
  • Dynamic import with not bundled file
  • Click handler not firing for elements rendered in Ajax Callback - only in Safari
  • Math.max.apply gives an error CreateListFromArrayLike called on non-object
  • get the parent nodes to a given node in d3 dendogram
  • is-dropdown-submenu-parent makes links unclickable on Opera and Chrome
  • JS: Sum column values and updating result on change
  • Query against nested object keys
  • Fullcalendar getting its parent colspan
  • How to retrieve a dynamic number of elements by id
  • How to access redux variables and functions from deep components
  • Efficiently access Array.prorotype.filter single-item returns
  • PHP var into Javascript
  • How to parse json with javascript/jquery if content matches string
  • How can I make a pyramid based on alphabet?
  • Javascript sorting a csv rows and displaying in table
  • How to get specific data from json from api using ajax. The json looks strange to me
  • react js html5 video not working
  • Javascript Angular 4 Change ngClass from another Component
  • Hide multiple table columns with javascript
  • Convert datetime to ISO Format using moment js
  • Dynamically adjust canvas size depending on content
  • setTimeout function with document on click
  • Need advice on nested Promises.all
  • Google Captcha expiration time
  • Syntax of Javascript TAPE unit test
  • Parse php json to javascript
  • Write to Internet Explorer javascript console with vba in excel
  • 2 Forms with Jquery
  • Why the loop stops right in the middle?
  • filter system for restaurant booking
  • how to add active class in framework nav menu when (add/edit) pages are opened
  • Port a js map in dart
  • How to get value from select using javascript
  • Audio won't be muted with JavaScript - removing audio tags with mutationobserver needed
  • How to include an HTML/Javascript game into reactJS website
  • Convert Nodejs server into node module
  • Strage Vue behaviour. V-if does not watch properly
  • Is it possible to use map/filter/reduce inside a promise by returning the result with Promise.resolve?
  • Does EcmaScript have any rule where whitespace is important for the compiler during parsing
  • How is DOM and Virtual DOM represented in memory?
  • cannot display text onchange select option
  • My gulpfile can edit (change) and delete file, but dont copy new (add) files
  • How do I reset scroll position of paper-dialog-scrollable?
  • Loading unlooped gif-animation several times
  • Strange error from Google API: initializeAutocomplete is not a function
  • Array state variable is not being set during componentWillMount
  • Stop refresh on form submit when using javascript
  • Npm WARN deprecated babel-preset-es2015@6.24.1: Thanks for using Babel: we recommend using babel-preset-env now: please
  • Cant get wheel scroll delta on Firefox
  • Puppeteer with Prompt, ignoring rest of function
  • VueJS Form Input Binding
  • How To Extend mail.Chatter Widget in Javascript Odoo 10
  • How Chk two checkboxes at a time using jQuery
  • Broken "if" statement in JavaScript code…
  • shadow
    Privacy Policy - Terms - Contact Us © voile276.org