0

Hey all I am trying to delete a few items inside my object:

if (e = j[a].apply(j, g), e !== c && e !== j)
    $.each(j.data[0], function (key, value) {
       console.log(key, value);
       j.data.splice(1, 1);
    });

return h = e, !1

enter image description here

However, that does not seem to delete the item environment sine that is #1 and bomDescription is #0.

I also tried:

j[0].pop()

j.data[0].pop()

j.data[0].splice(1, 1)

Which non of the above work or cause an error.

What am I doing incorrectly?

6
  • Possible duplicate of How to delete object property? Commented May 11, 2018 at 17:29
  • Is the content of data[0] an object or an array? Your title says 'object' but your code suggests an array. Commented May 11, 2018 at 17:42
  • @Mark_M I thought it was Object Literals Commented May 11, 2018 at 17:44
  • But you're calling splice() that's an array method. Commented May 11, 2018 at 17:45
  • Well then I'll go back to my last sentence in my OP - What am I doing incorrectly? :) Commented May 11, 2018 at 17:46

1 Answer 1

0

If your object looks something like this (as it may seem from that image).

const j = {
  data: {
    bomDescription: "hjtyj",
    environment: 2,
    ...
  }
}

Then to delete the environment property you can use delete statement like this.

const j = {
  data: {
    bomDescription: "hjtyj",
    environment: 2
  }
}

console.log(j.data);
delete j.data.environment;
console.log(j.data);

EDIT

To delete property based on its value you can do this.

const j = {
  data: {
    bomDescription: "hjtyj",
    environment: 2
  }
}

const removePropertyByValue = (obj, value) => {
  for (const prop in obj) {
    if (obj.hasOwnProperty(prop) && obj[prop] === value) {
      delete obj[prop];
    }
  }
}

console.log(j.data);
removePropertyByValue(j.data, 2);
console.log(j.data);

EDIT II

If the data property is an array of objects then you can do this.

const j = {
  data: [{
    bomDescription: "hjtyj",
    environment: 2
  }]
}

const removePropertyByValue = (obj, value) => {
  for (const prop in obj) {
    if (obj.hasOwnProperty(prop) && obj[prop] === value) {
      delete obj[prop];
    }
  }
}

console.log(j.data[0]);
removePropertyByValue(j.data[0], 2);
console.log(j.data[0]);

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

8 Comments

thanks for the example - however - I wont know the name (key) of the item I will be deleting.
@StealthRT Then based on what are you going to delete it?
It will be a number so in my example I put a static number of 1.
@StealthRT I am sorry but I don't understand. What will be a number? Object properties are not ordered. Do you mean that you want to delete property/ies that have number as thier value?
Yes @matus I will be checking to see if something = key and if it does then delete it.
|

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.