0

So I am trying to use arrays and for demonstration purposes I have condensed the code using points as examples. I want to create a point and push it to array and after doing stuff I want to remove the point from the array.

var PointArray = [];
function CreatePoint(X,Y){
    this.x=X;
    this.y=Y;
    PointArray.push(this);
}
function RemovePoint(PointObject){
     //????
}
var xPoint = CreatePoint(10,10);
//do stuff
RemovePoint(xPoint);

I was looking at the ‍Array.prototype manual and PointArray.Splice seems like a the closest but feels messy since it wants indexs. Anyone have a clean way remove objects from array to shove into function RemovePoint?

1
  • Slice is how you do it unless you want to use a library. Commented Aug 25, 2018 at 3:52

2 Answers 2

4

To find the index of something in an array, use indexOf:

function RemovePoint(PointObject){
  const index = PointArray.indexOf(PointObject);
  PointArray.splice(index, 1); // remove one item at index "index"
}

But if you're doing something like this, you might consider using a Set instead, which might be more appropriate if you want a collection, but the index of each object doesn't actually matter - then, you could just call PointSet.delete(PointObject);:

const PointSet = new Set();
function CreatePoint(X,Y){
  this.x=X;
  this.y=Y;
  PointSet.add(this);
}
function RemovePoint(PointObject){
  PointSet.delete(PointObject);
}
console.log(PointSet.size);
const xPoint = new CreatePoint(10,10);
console.log(PointSet.size);
RemovePoint(xPoint);
console.log(PointSet.size);

As comment notes, make sure to use new when calling a constructor like CreatePoint.

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

3 Comments

Ah. I didn't realize i could do that. let me go read the doc on it.
@MoonEater916 Works just fine for me - see edit. Calling CreatePoint increases the Set's size to 1, and then calling RemovePoint reduces the size to 0.
ya doing a test xPoint is undefined because I didn't put "new" in the line "var xPoint = new CreatePoint(10,10);" Works Great! Thanks @MarkMeyer
0

Why not use filter to remove elements from PointArray

var PointArray = [];
function CreatePoint(X,Y){
    this.x=X;
    this.y=Y;
    PointArray.push(this);
}
function RemovePoint(PointObject){
  PointArray = PointArray.filter(singlePoint=>!(singlePoint.x==PointObject.x && singlePoint.y==PointObject.y));
}
var xPoint = new CreatePoint(10,10);
var yPoint = new CreatePoint(20,20);
//do stuff
console.log(PointArray);
RemovePoint(xPoint);
console.log(PointArray);

5 Comments

In your particular example, because they don't want to remove all points with a specific x/y value, they want to remove a specific entry.
But even using a set would not allow to keep duplicates. So no two points have same x and y value
Depends on how equality is defined, but a set isn't necessarily the right approach-we don't know because the full problem isn't defined.
For deleting an item from a set that is a collection of points, the only equality check would be x and y. So will be the insertion check.
With the current object definition, yes, but that would be a bad way to generally represent points, since it's perfectly valid to have distinct points with the same coordinates. But that's orthogonal to the question, which is how to remove a specific array entry, which may or may not be unique-we simply don't know the problem's parameters.

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.