2

I have a map array with objects stuffed with variables looking like this:

var map = [
[{ground:0, object:1}, {ground:0, item:2}, {ground:0, object:1, item:2}],
[{ground:0, object:1}, {ground:0, item:2}, {ground:0, object:1, item:2}]
];

Now I would like to be able to delete and add one of the variables like item:2.

1) What would I use to delete specific variables?

2) What would I use to add specific variables?

I just need 2 short lines of code, the rest like detecting if and where to execute I've figured out.

I've tried delete map[i][j].item; with no results.

Help appreciated.

5
  • I don't want to remove the entire object {ground:0, object:1}, I want to delete the property, variable, whatever it's called from inside the object that is the key. Commented Mar 21, 2012 at 19:28
  • delete map[i][j].item should do what you want Commented Mar 21, 2012 at 19:29
  • Okej, I've tried that allready, but it doesn't delete it, the rest of the code to be executed at that moment works fine though. Got any clues what I might be doing wrong? Commented Mar 21, 2012 at 19:33
  • I imagine you're re-assigning the value somewhere else in the code, so by the time you get around to checking it, the value is back Commented Mar 21, 2012 at 19:35
  • Sorry I'm retarded, changed [i][j] to X and Y positions and it fixed it, change your post to an answer and you get free credit. =) Commented Mar 21, 2012 at 19:36

1 Answer 1

1

delete map[i][j].item should be what you need. Here's my test run in the Javascript console (Chrome)

> var map = [
    [{ground:0, object:1}, {ground:0, item:2}, {ground:0, object:1, item:2}],
    [{ground:0, object:1}, {ground:0, item:2}, {ground:0, object:1, item:2}]
  ];
  undefined

> map[0][1]
  Object
    ground: 0
    item: 2
    __proto__: Object

> delete map[0][1].item
  true

> map[0][1]
  Object
    ground: 0
    __proto__: Object
Sign up to request clarification or add additional context in comments.

2 Comments

While I got you here, whats the delete version to add something in the same way?
You're overthinking it. map[0][1].item = 2 if item doesn't already exist, it gets added.

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.