Remove the deleted values from array

javascript remove object from array
remove specific element from array javascript
javascript remove object from array by value
remove object from json array javascript
remove element from array java
delete element from array - c++
array splice
javascript remove object from array by property

I have two arrays such as,

Array 1:

    deletedValuesfromArray =
     [
        { "property_name": "Property three", "property_type": 4, "property_required": true, "property_origin": 2 }, 
        { "property_name": "rstywrtre", "property_type": 3, "property_required": true, "property_origin": 1 }
     ]

Array 2:

  normalArray =
   [
    { "property_name": "Property one", "property_type": 4, "property_required": true, "property_origin": 1 },
     { "property_name": "Property two", "property_type": 5, "property_required": true, "property_origin": 1 }, 
     { "property_name": "Property three", "property_type": 4, "property_required": true, "property_origin": 2 }, 
     { "property_name": "rstywrtre", "property_type": 3, "property_required": true, "property_origin": 1 }
   ]

I would like to compare both arrays and filter the array to get the new one after removing the deletedValuesfromArray (Array 1).

For which i have tried the following,

let newArray = this.normalArray.filter(function (val) {
  return this.deletedValuesfromArray.indexOf(val) == -1;
});
console.log(newArray);

But it doesn't works..

Expected Output is,

New Array

   [
     { "property_name": "Property one", "property_type": 4, "property_required": true, "property_origin": 1 },
     { "property_name": "Property two", "property_type": 5, "property_required": true, "property_origin": 1 }
   ]

Stackblitz that i have tried

The values also will not be unique always it may have a complete duplicate of any object.

How to compare and remove the deleted values from the normal array and get the newarray?

Your filter predicate doesn't work because you can't just compare an object from normalArray to an object from deletedValuesfromArray:

indexOf() compares searchElement to elements of the Array using strict equality (the same method used by the === or triple-equals operator).

-- Array.indexOf()

In other words,

{ "property_name": "Property one", "property_type": 4, "property_required": true, "property_origin": 1 } === { "property_name": "Property one", "property_type": 4, "property_required": true, "property_origin": 1 }
// > false

To make the filter work, you need to implement a comparison function. See How to determine equality for two JavaScript objects? for some ideas.

Remove an array element and shift the remaining ones, How do I remove something from an array in Java? So, it returned a copy of numpy array by selecting values below 6 & greater than 10 only and we assigned this new array back to arr to have the deletion effect. Delete elements by value or condition using np.argwhere() & np.delete()

Using lodash :-

npm install --save lodash


import * as _ from "lodash";

var newArray = _.differenceWith(this.normalArray, this.deletedValuesfromArray, _.isEqual);
console.log("newArray", newArray);

Deleting array elements in JavaScript - delete vs splice, How do you delete an element from an array in C++? We can use the index value to delete any element from an array. So we all know how to add an element in an Array. But it is very hard to delete an element from an array compared to adding it So let us have a look at the ways to delete an element from an Array. Delete an Element from an Array in C++. An array is a static linear data type and it

one of simple options here you can use JSON.stringify, which will turn objects into strings and compare them as strings, because 2 objects never equal each other by value since it compares them by reference, make sure that your objects keys order are the same

const deletedValuesfromArray =
     [
        { "property_name": "Property three", "property_type": 4, "property_required": true, "property_origin": 2 }, 
        { "property_name": "rstywrtre", "property_type": 3, "property_required": true, "property_origin": 1 }
     ]

  const normalArray =
   [
    { "property_name": "Property one", "property_type": 4, "property_required": true, "property_origin": 1 },
     { "property_name": "Property two", "property_type": 5, "property_required": true, "property_origin": 1 }, 
     { "property_name": "Property three", "property_type": 4, "property_required": true, "property_origin": 2 }, 
     { "property_name": "rstywrtre", "property_type": 3, "property_required": true, "property_origin": 1 }
   ]

const result = normalArray.filter(obj => !deletedValuesfromArray.find(o => JSON.stringify(o) == JSON.stringify(obj)));

console.log(result);

How to delete a value from an array in JavaScript, delete will delete the object property, but will not reindex the array or update its length. This makes it appears as if it is undefined: > myArray = ['a', 'b', 'c', 'd'] ["a",  Move to the specified location which you want to remove in given array. Copy the next element to the current element of array. Which is you need to perform array[i] = array[i + 1]. Repeat above steps till last element of array. Finally decrement the size of array by one. Program to delete element from array

You can proceed with ES6 operators (filter and some) :

 ngOnInit() {
    let newArray = this.normalArray.filter( (val) => {
      return !this.deletedValuesfromArray.some((newval) => {
       return JSON.stringify(newval) === JSON.stringify(val) // check if two objects are the same
      });
    });
    console.log(newArray);
  }

Delete an element from array (Using two traversals and one , How do I remove an element from an array? Is the delete operator of any use? There also exists funny named splice() and slice() , what about those? Deleting Elements in an Array if Element is a Certain value VBA. to delete elements in an Array wih certain condition, you can code like this

This might also help you.

function display()
{
  let newArray = this.normalArray.filter(function (val) {
  count = 0;
  var flag = true;
  return this.deletedValuesfromArray.find(function(item, i){
  count++;

     if(val.property_name == item.property_name && val.property_type == item.property_type && val.property_required == item.property_required && val.property_origin == item.property_origin ){
       flag =  false;
     }
     if(count == deletedValuesfromArray.length) {
     return flag;
     }
  });

}); }

Remove elements from a JavaScript Array, Given an array and a number 'x', write a function to delete 'x' from the given array. We assume that array maintains two things with it, capacity and size. So when  To allocate a collection (creates a new array), then delete an element (which the collection will do using arraycopy) then call toArray on it (creates a SECOND new array) for every delete brings us to the point where it's not an optimizing issue, it's criminally bad programming.

JavaScript: Four Ways to Remove a Value From an Array, You can add or remove elements from array in any position. In this article, we will discuss different Delete element at index 2. var deleted = delete array[2];. There are various ways to delete an object from a list: my_list = [1,2,4,6,7] del my_list[1] # Removes index 1 from the list print my_list # [1,4,6,7] my_list.remove(4) # Removes the integer 4 from the list, not the index 4 print my_list # [1,6,7] my_list.pop(2) # Removes index 2 from the list.

Different ways to remove elements from an array in JavaScript, If you want to go simple, the easiest method for clearing a value in an array is to use the delete keyword. Simply use the variable name along  If you know the value you want to remove from an array you can use the splice method. First you must identify the index of the target item. You then use the index as the start element and remove just one element.

How to Remove Elements from an Array in JavaScript, The delete operator only erases the value of the element and makes the element as undefined. The length of the array stays unaffected. One way to remove an object from a fixed-size array is to create a new array that includes only selected objects from the original array. In this command, we use a ForEach loop go through every letter in the $letters array. We add all of the objects in $letters to the $newLetters array, except for “c”.

Comments
  • FYI, lodash is swiss army knife of Javascript.
  • what if the order of keys is different in two same-looking objects?
  • @shkaper the order is important in my example, I will update my answer
  • I can explain the down vote. Using stringify is a terrible solution and the guy that made the question didn't say that he wants to check that the two objects are exactly identical. You should NEVER expect that the order of keys is the same.
  • @itsundefined from the question it is easy to get that he wants to check that the two objects are exactly identical. Almost all answers contain the same logic, as accepted answer too which marked by question creator, it means we got question right, and I noted that in my example order is important, so I don't see any reason for down vote, it seems you got question wrong and added down votes to answers which have upvote
  • @itsundefined I'm not using this method for removing objects from array, I'm using this method for comparing objects, 2 different things
  • Here you're assuming that property_name values are unique. The OP never said that
  • Yes this solution seems to be close to me but the thing is as @shkaper said the values may not be unique at all time..
  • this is potentially dangerous since object's property order is not guaranteed in JavaScript
  • If you need an explanation about why this solution is terrible, please consider how much cpu time is wasted to stringify all those objects. You are also stringifying the same object way too many times with the way this loop works. You are also not taking into account that there is no spec in javascript that says that the order of object properties stays the same.