Sandy Smarue
Sandy Smarue

Reputation: 71

Javascript Object to array then remove element

I have this object

Object {
  "87291": "valid",
  "1873681927": "valid",
  "nwq89j8jw0qj9": "valid",
  "oVFYfWIUOsONE6JyMGYAbnsPMAr1": "valid",
}

what i want to do is remove object based on key value and return as array.

i used this code :

    let attay = {
      "87291": "valid",
      "1873681927": "valid",
      "nwq89j8jw0qj9": "valid",
      "oVFYfWIUOsONE6JyMGYAbnsPMAr1": "valid",
    }


    let aar = Object.entries(attay)
    attay = Object.keys(attay)

    for(var i = 0; i < attay.length; i++) {
      if(attay[i] == 'oVFYfWIUOsONE6JyMGYAbnsPMAr1'){
        console.log("found "+attay[i]+" at "+i)
        aar.splice(i, 1);
        console.log(aar)
      }else{
        console.log("NOT found at "+i)        
      }
    }

this code worked okay but the output after remove element is like this

Array [
  Array [
    "87291",
    "valid",
  ],
  Array [
    "1873681927",
    "valid",
  ],
  Array [
    "nwq89j8jw0qj9",
    "valid",
  ],
]

expected output should be like

Array [
      "87291": "valid",
      "1873681927": "valid",
      "nwq89j8jw0qj9": "valid",
    ]

Upvotes: 2

Views: 150

Answers (5)

Aziz.G
Aziz.G

Reputation: 3721

you could use delete this way if the key match with the propriety:

const obj = {
  "87291": "valid",
  "1873681927": "valid",
  "nwq89j8jw0qj9": "valid",
  "oVFYfWIUOsONE6JyMGYAbnsPMAr1": "valid",
}

const key = "87291";

delete obj[key]



console.log(obj)

Upvotes: 0

TheWandererLee
TheWandererLee

Reputation: 1032

Maybe I'm misunderstanding but it seems like you're over-complicating this. You want to remove the property from the object?

let arr = {
  "87291": "valid",
  "1873681927": "valid",
  "nwq89j8jw0qj9": "valid",
  "oVFYfWIUOsONE6JyMGYAbnsPMAr1": "valid",
}

let removeKey = "oVFYfWIUOsONE6JyMGYAbnsPMAr1"
delete arr[removeKey]; // Remove the property

console.log(arr);

Upvotes: 4

Sandy Smarue
Sandy Smarue

Reputation: 71

the answer came from @Pointy

delete attay.oVFYfWIUOsONE6JyMGYAbnsPMAr1

simple and worked like charm and also worked on React Native

Upvotes: 1

Jack Bashford
Jack Bashford

Reputation: 44107

You can avoid the less supported fromEntries by using a simple reduce.

let attay = {
  "87291": "valid",
  "1873681927": "valid",
  "nwq89j8jw0qj9": "valid",
  "oVFYfWIUOsONE6JyMGYAbnsPMAr1": "valid",
};
const remove = "oVFYfWIUOsONE6JyMGYAbnsPMAr1";
const res = Object.entries(attay).reduce((acc, [k, v]) => k == remove ? acc : (acc[k] = v, acc), {});
console.log(res);

Upvotes: 0

danh
danh

Reputation: 62686

If your runtime is okay with the relatively modern entries (and fromEntries), then you can get what you want simply. (Guessing that you're really aiming for an object output, not an array based on the syntax in your question).

let attay = {
  "87291": "valid",
  "1873681927": "valid",
  "nwq89j8jw0qj9": "valid",
  "oVFYfWIUOsONE6JyMGYAbnsPMAr1": "valid",
}

let removeKey = "oVFYfWIUOsONE6JyMGYAbnsPMAr1"

let pairs = Object.entries(attay).filter(e => e[0] != removeKey)
const obj = Object.fromEntries(pairs);
console.log(obj)

Upvotes: 1

Related Questions