2

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",
    ]
4
  • If you pretend to have key/value pairs in your expected output, then do not expect to use an array for it. Commented Jun 5, 2019 at 21:21
  • 5
    The "expected output" does not make sense; that's not valid syntax. Commented Jun 5, 2019 at 21:21
  • 1
    Also just delete attay.oVFYfWIUOsONE6JyMGYAbnsPMAr1' and then do whatever you want to the original object. Commented Jun 5, 2019 at 21:22
  • i thought about this but tbh i didn't try it cuz u'm using React Native. Commented Jun 5, 2019 at 21:31

5 Answers 5

4

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);

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

Comments

1

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)

Comments

1

the answer came from @Pointy

delete attay.oVFYfWIUOsONE6JyMGYAbnsPMAr1

simple and worked like charm and also worked on React Native

Comments

0

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);

Comments

0

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)

3 Comments

Why are you using map for a simple iteration?
You could just use forEach().
delete obj[key] already searches through the object to find the key specified, there is no need to write your own iterator.

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.