javascript - Whats the best way of deleting a object on a array

Giuan Adauto

I want to delete a object on my objects array, but i cant find a better way than using some loop to do such a thing, on each object we have an ID

example:

var array = [{
    id         : String,
    price      : Number,
    someOtherVars : String
}] 

var element = {
    id         : "00dks",//this is a unic value
    price      : 12,
    someOtherVars : "some value"    
}

var element2 = {
    id         : "a43sdk",
    price      : 30,
    someOtherVars : "some value"    
}

var element3 = {
    id         : "0as0d",
    price      : 122,
    someOtherVars : "some value"    
}
array.push(element);
array.push(element2);
array.push(element3);

so... what is the most efficient way of doing this?

ps: i'm looking for a way to access the element by the key with out using any kind of loop, like the function .find(), example array["0as0d"], to get element3

jdphenix

Don't use an array, but a Map.

var map = new Map();

var element = {
    id         : "00dks",//this is a unic value
    price      : 12,
    someOtherVars : "some value"    
}

var element2 = {
    id         : "a43sdk",
    price      : 30,
    someOtherVars : "some value"    
}

var element3 = {
    id         : "0as0d",
    price      : 122,
    someOtherVars : "some value"    
}
map.set(element.id, element);
map.set(element2.id, element2);
map.set(element3.id, element3);

// later... 

if (map.delete("0as0d")) {
  console.log('delete successful');
}

console.log(map.size);

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Javascript

Whats the best way to update an object in an array in ReactJS?

From Dev

Whats the best way to parse a stringified array in javascript?

From Dev

Javascript Array to Object best way

From Dev

How can i search in a custom object type array list ? whats is best and fastest way

From Dev

Whats the best way to make a nested object of variable keys and depth?

From Dev

Whats the best way to cast JSON to generic Object[] in angular 2 typescript?

From

Whats the best way to retrieve value of array element from a JSON string

From Dev

Whats the best way to create an array of objects inside a class as attribute in PHP?

From Dev

Deleting an object from a array in javascript

From Dev

Best way of returning indexes on a multi-dimensional array object with Javascript

From Dev

Best way to combine data of object/array to an attribute in JavaScript

From Javascript

best way to convert array to object with prefix value as key in javascript

From Dev

Javascript : What is the best way to detect combination of two keys in array of an object

From Dev

Best way to create a copy of an object array with a new structure in javascript

From Dev

Best way to convert object into array

From Dev

best way to Convert Array into Object

From Dev

Best way to convert array to object

From Javascript

Deleting object in nested array in an immutable way

From Dev

best way to search and delete with given values, for delete from array inside the object and return new array of object javascript?

From

What's the best way to get the last element of an array without deleting it?

From Dev

Editing an object in Javascript (deleting a symbol from an array)

From Dev

Whats the best way in Swift to animate an array of NSViews to appear one after another in an NSStackView?

From Dev

Python help needed. Whats the best way to gather data from a spreadsheet into an array that can hold strings and numbers

From Dev

Whats the best way to iterate over multidimensional array and tracking/doing operations on iteration index

From Dev

Whats the best way to save a Hashtable to a file?

From Dev

Whats the best way to ensure a line in a file exists?

From Dev

Whats the best way of converting a string to a double

From Dev

Whats the best way to exclude burried dependencies in Gradle?

From Dev

Whats the best way of making a web browser in Ubuntu?

Related Related

  1. 1

    Whats the best way to update an object in an array in ReactJS?

  2. 2

    Whats the best way to parse a stringified array in javascript?

  3. 3

    Javascript Array to Object best way

  4. 4

    How can i search in a custom object type array list ? whats is best and fastest way

  5. 5

    Whats the best way to make a nested object of variable keys and depth?

  6. 6

    Whats the best way to cast JSON to generic Object[] in angular 2 typescript?

  7. 7

    Whats the best way to retrieve value of array element from a JSON string

  8. 8

    Whats the best way to create an array of objects inside a class as attribute in PHP?

  9. 9

    Deleting an object from a array in javascript

  10. 10

    Best way of returning indexes on a multi-dimensional array object with Javascript

  11. 11

    Best way to combine data of object/array to an attribute in JavaScript

  12. 12

    best way to convert array to object with prefix value as key in javascript

  13. 13

    Javascript : What is the best way to detect combination of two keys in array of an object

  14. 14

    Best way to create a copy of an object array with a new structure in javascript

  15. 15

    Best way to convert object into array

  16. 16

    best way to Convert Array into Object

  17. 17

    Best way to convert array to object

  18. 18

    Deleting object in nested array in an immutable way

  19. 19

    best way to search and delete with given values, for delete from array inside the object and return new array of object javascript?

  20. 20

    What's the best way to get the last element of an array without deleting it?

  21. 21

    Editing an object in Javascript (deleting a symbol from an array)

  22. 22

    Whats the best way in Swift to animate an array of NSViews to appear one after another in an NSStackView?

  23. 23

    Python help needed. Whats the best way to gather data from a spreadsheet into an array that can hold strings and numbers

  24. 24

    Whats the best way to iterate over multidimensional array and tracking/doing operations on iteration index

  25. 25

    Whats the best way to save a Hashtable to a file?

  26. 26

    Whats the best way to ensure a line in a file exists?

  27. 27

    Whats the best way of converting a string to a double

  28. 28

    Whats the best way to exclude burried dependencies in Gradle?

  29. 29

    Whats the best way of making a web browser in Ubuntu?

HotTag

Archive