Delete object properties similiar like deleting array elements with splice?

Mihajlo Supic

We have object type variable in Jquery:

var obj = *{"1234" : "xx", "4321" : "yy", "5555" : "hh", "2321" : "aa" };*

Lets say that I want to delete every property from property name "5555" to the end of the object(that means that I want to delete obj['5555'] and delete obj['2321'] ). I am interested in smartest way, trough loop, to do that.

In array I would use splice(2, arr.length) but I am confused.

Andy

Push the obj to be searched and the value to be found into a function that returns a new object with only those properties up to the value you specified.

function returnNewobj(obj, value) {
  var newObj = {};
  for (var prop in obj) {
    if (obj.hasOwnProperty(prop)) {
      if (prop === value) return newObj;
      newObj[prop] = obj[prop];
    }
  }
  return newObj;
}

Edit: probably not necessary, but I added the hasOwnProperty line to be on the safe side.

Edit2: It's worth pointing out that new properties are added to objects in alphanumerical order, not to the end of objects like elements are added to arrays. So don't get caught out by that.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Javascript

Deleting array elements in JavaScript - delete vs splice

From Dev

deleting object elements from array using splice react native

From Dev

Removing an object's array elements using splice()

From Dev

Delete empty object after deleting properties with undefined value

From Dev

Splice nested object in array

From Dev

Does ruby have similiar multidimensional array like in php?

From Dev

.splice() keeps deleting the last item of an array

From Dev

JS array splice deleting first element

From Javascript

Delete vs splice on associative array

From Dev

Using splice to delete item in array

From Dev

Javascript splice for array of DOM elements

From Dev

Object not deleting on DELETE request

From Dev

JavaScript - delete object properties in array of objects

From Dev

Vue.js Axios delete method deleting the wrong object in the array

From Dev

Assigning array elements as object properties, with 'counter' value

From Dev

Destructure object properties inside array for all elements

From Dev

Deleting elements in an associative array

From Dev

Deleting elements in an array

From Dev

Deleting the neighbouring elements of an array

From Dev

Object/Associative Array Splice function

From Dev

Array splice removes all elements from array

From Dev

error Cannot delete property '2' of [object Array] using splice static method on my reducer NgRx

From Dev

converting object properties which are array like to array is not working

From Dev

Javascript object properties in loop not deleting

From Dev

delete operator not deleting object property

From Dev

Are JavaScript Array elements nothing more than Array object properties?

From Dev

Why is splice removing all the elements from an array?

From Dev

array.splice ignores one of the elements

From Dev

Adding Elements in An Array Incrementally Using Splice Javascript

Related Related

  1. 1

    Deleting array elements in JavaScript - delete vs splice

  2. 2

    deleting object elements from array using splice react native

  3. 3

    Removing an object's array elements using splice()

  4. 4

    Delete empty object after deleting properties with undefined value

  5. 5

    Splice nested object in array

  6. 6

    Does ruby have similiar multidimensional array like in php?

  7. 7

    .splice() keeps deleting the last item of an array

  8. 8

    JS array splice deleting first element

  9. 9

    Delete vs splice on associative array

  10. 10

    Using splice to delete item in array

  11. 11

    Javascript splice for array of DOM elements

  12. 12

    Object not deleting on DELETE request

  13. 13

    JavaScript - delete object properties in array of objects

  14. 14

    Vue.js Axios delete method deleting the wrong object in the array

  15. 15

    Assigning array elements as object properties, with 'counter' value

  16. 16

    Destructure object properties inside array for all elements

  17. 17

    Deleting elements in an associative array

  18. 18

    Deleting elements in an array

  19. 19

    Deleting the neighbouring elements of an array

  20. 20

    Object/Associative Array Splice function

  21. 21

    Array splice removes all elements from array

  22. 22

    error Cannot delete property '2' of [object Array] using splice static method on my reducer NgRx

  23. 23

    converting object properties which are array like to array is not working

  24. 24

    Javascript object properties in loop not deleting

  25. 25

    delete operator not deleting object property

  26. 26

    Are JavaScript Array elements nothing more than Array object properties?

  27. 27

    Why is splice removing all the elements from an array?

  28. 28

    array.splice ignores one of the elements

  29. 29

    Adding Elements in An Array Incrementally Using Splice Javascript

HotTag

Archive