0

I have a school assignment and I have an issue with deleting something from an array of indexes. This is what I currently have.

Function that returns array length:

function DolzinaPolja(polje){   
    return polje.length-1;
}

new constructor

function oseba(ime, priimek, stranka) {
    this.ime=ime;
    this.priimek=priimek;
    this.stranka=stranka;
}

function that creates an object and pushes it into an array

function UstvariObjekt(ime,priimek, stranka) {
    if (ime.length == 0 || priimek.length == 0 || stranka.length == 0) {
        throw "Parametri niso popolni!";
    }
    else {
        var novaoseba=new oseba(ime, priimek, stranka);
        polje.push(novaoseba);
        console.log(novaoseba.ime, novaoseba.priimek, novaoseba.stranka);
    }
}

function that deletes an object from the array

function OdstraniIzPolja(x) {
    if(x > polje.length - 1 || x == polje.length) {
        throw"Polje ni tako veliko!";
    }
    for(var i=0; i<=polje.length-1;i++) {
        if(x==polje[i]) {
            polje.splice(x,1);
            return true;
        }
        return false;
    }
}

I am having an issue with deleting an object from the array. Here are my tests.

var polje = [];
var x=0;

UstvariObjekt("Rene","Vucko","Stranka");
UstvariObjekt("R","V","S");
UstvariObjekt("X","Y","Z");

OdstraniIzPolja(x);
console.log(polje[0]);
console.log(polje[1]);
console.log(polje[2]);
console.log(DolzinaPolja(polje));

SO my array should be the length of 2. Since I start with 0,1,2. What I don't understand is why doesn't my function that deletes an object from the array delete the object? I've played around a little bit but often it just deletes the wrong object.

Also is the if clause for x if the length is smaller than the array length written ok?

---UPDATE---- IF I write polje.slice(2,1) without the function just in the program, it deletes the right one. So obviously something is wrong with the loop. This is my updated code.

function DolzinaPolja(polje){   
    return polje.length-=1;
}

function OdstraniIzPolja(x)
{
    if(x>polje.length-1 || x==polje.length)
    {
        throw"Polje ni tako veliko!";
    }
    for(var i=polje.length-1;i>=0;i--)
    {
        if(x==polje[i]){
        polje.splice(i,1);
        return true;
        }
        return false;
    }
}

--EDIT-- here's the code https://jsfiddle.net/2y07wtkL/

6
  • 1
    DolzinaPolja is not returning the length, it's returning the length minus one. Commented Apr 19, 2016 at 16:57
  • In your OdstraniIzPolja function, since x is an index and you're checking to see if it's in the proper range... you shouldn't need the loop at all, right? Commented Apr 19, 2016 at 16:58
  • What is the console log output? Commented Apr 19, 2016 at 17:18
  • ene Vucko Stranka funkcije.js:32 R V S funkcije.js:32 X Y Z uporaba.js:17 Rene R X uporaba.js:19 oseba {ime: "Rene", priimek: "Vucko", stranka: "Stranka"} uporaba.js:20 oseba {ime: "R", priimek: "V", stranka: "S"} uporaba.js:21 undefined uporaba.js:22 1 Commented Apr 19, 2016 at 17:25
  • and here are the calls UstvariObjekt("Rene","Vucko","Stranka"); UstvariObjekt("R","V","S"); UstvariObjekt("X","Y","Z"); console.log(polje[0].ime, polje[1].ime, polje[2].ime); OdstraniIzPolja(x); console.log(polje[0]); console.log(polje[1]); console.log(polje[2]); console.log(DolzinaPolja(polje)); Commented Apr 19, 2016 at 17:27

3 Answers 3

1

It's because you splice the object with wrong index. If you splice using

polje.splice(x,1);

you remove the element at x index. That means when you remove first element from array, the second element from array becomes first, so the next iteration won't delete the second element you want to be deleted, but the second element from array will be deleted instead (it doesn't have to be the element you are currently iterating on).

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

10 Comments

To build on this.. it looks like you want to be doing polje.splice(i, 1); since you first checked that the value of index i is equal to x.
I tried switching x with i but nothing gets deleted :/
If you want to remove just one entry from array, use pojle.splice(pojle.indexOf(x, 1)) and don't make any loop.
@P0lip you have a small typo in your splice function. Change it to pojle.splice(pojle.indexOf(x), 1) and you are correct.
I don't get it. If I write polje.splice(polje.indexOf(x), 1); and I set x to 1, it deletes the last element, so the one on the index 2, and if I set the x to 0, it still deletes the one on the index 2.
|
0

try

function DolzinaPolja(polje){   
    return polje.length -= 1;
}

as in your solution you just subtract the lenght, but you dont asign it

Comments

0

Whenever you are deleting from a collection with indices you should do it from the end so that you are indices don't change after each delete. I think that's what is happening. Change the direction like this

for(var i=polje.length-1;i>=0;i--)
{
    ........
}

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.