0

How can I to substract/delete/remove all coincident elements from an array list within another array list using .filter() of ES6 javascript?

Jquery's solution is also accepted, but any solution must be IE11+ and Chrome iOs compatible. Avoid using external libraries (except jquery). This is what I tried so far:

   var originList = [
        {"id":1,"name":"www"},
    	{"id":2, "name":"fff"},
    	{"id":3, "name":"ddd"},
    	{"id":4, "name":"aaaa"},
    	{"id":5, "name":"zzzz"},
    	{"id":6, "name":"Susane"},
        {"id":7,"name":"yyy"}
    ];

    var rest = [
        {"id":1,"name":"www"},
    	{"id":2, "name":"fff"},
    	{"id":3, "name":"ddd"},
        {"id":4, "name":"aaaa"},
        {"id":5, "name":"zzzz"},
    ];

    var newList = [];
    var list = [];

    $.each(originList, function(id,data){
       list = originList.filter(function(obj,a){
		    return data.id !== obj.id ? obj : false;
	    });
    });


    $.each(originList, function(id,data){
       	$.each(rest, function(alli,allData){
		    if(data.id === allData.id) {
  			    newList[id] = data;
		    }
		
	    });
    });

    console.log(list);	//does not exclude id:6
    console.log(newList); //this result is what I want
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

0

6 Answers 6

3

You can use filter and find for the newer version.

var originList = [
  {"id":1,"name":"www"},
  {"id":2, "name":"fff"},
  {"id":3, "name":"ddd"},
  {"id":4, "name":"aaaa"},
  {"id":5, "name":"zzzz"},
  {"id":6, "name":"Susane"},
  {"id":7,"name":"yyy"}
];
	
var rest = [
  {"id":1,"name":"www"},
  {"id":2, "name":"fff"},
  {"id":3, "name":"ddd"},
  {"id":4, "name":"aaaa"},
  {"id":5, "name":"zzzz"},
];
	
var newList = originList.filter( o => rest.find( x => o.id === x.id ) );
	
console.log( newList );

Doc: .filter(), .find()


Can use for and if for the older version.

var originList = [
  {"id":1,"name":"www"},
  {"id":2, "name":"fff"},
  {"id":3, "name":"ddd"},
  {"id":4, "name":"aaaa"},
  {"id":5, "name":"zzzz"},
  {"id":6, "name":"Susane"},
  {"id":7,"name":"yyy"}
];
	
var rest = [
  {"id":1,"name":"www"},
  {"id":2, "name":"fff"},
  {"id":3, "name":"ddd"},
  {"id":4, "name":"aaaa"},
  {"id":5, "name":"zzzz"},
];

var newList = [];
for (var i = 0; i < originList.length; i++) {
  for (var x = 0; x < rest.length; x++) {
    if ( rest[x].id === originList[i].id ) {
        newList.push( originList[i] );
        break;
    }
  }
}

console.log( newList );

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

8 Comments

Ohh.. Ill update it in a bit :)
For IE10+ compatibility, could I suggest using a transpiler & pollyfill's, writing code for older browsers lack of features is worse than a red hot poker in the eye.
Updated. Just use if and for
What I need its just the opposite the values your newList is returning
@AXL IE10 or IE11... Most or rather all ES6 features are not available in any of those browsers: kangax.github.io/compat-table/es6
|
1

var originList = [
  {"id":1,"name":"www"},
  {"id":2, "name":"fff"},
  {"id":3, "name":"ddd"},
  {"id":4, "name":"aaaa"},
  {"id":5, "name":"zzzz"},
  {"id":6, "name":"Susane"},
  {"id":7,"name":"yyy"}
];
	
var rest = [
  {"id":1,"name":"www"},
  {"id":2, "name":"fff"},
  {"id":3, "name":"ddd"},
  {"id":4, "name":"aaaa"},
  {"id":5, "name":"zzzz"},
];

function containsObject(obj, list) {
  for (var i = 0; i < list.length; i++) {
    if (list[i].id === obj.id) return true;
  }
  return false;
}
	
var newList = originList.filter(function(item){  return !containsObject(item, rest)
});
	
console.log( newList );

7 Comments

Good catch. going to change it
But IE doesn't support arrow function. So I changed the arrow function
What is the return false statement on the containsObject function for?
@AXL The function passed to .filter() has to return true (keep the element) or false (get rid of the element). Check the documentation you've linked.
|
0

you can try this

var value1s = {};
arr1.forEach(function(item) {
  value1s[item.id] = item.name;
});

arr2.forEach(function(item) {
  if (item.id in value1s) {
    theFunction(value1s[item.id], item.name);
  }
});

function theFunction(id, name) {
   console.log("{"+id + ":" + name+"}");
}

Comments

0

If you have lodash at your disposal, you can use the function _.differenceWith and pass _.isEqual as the comparator invoked per element (if you want total equality) or a custom comparator if you only want to compare some properties of the object to determine equality.

Lodash also grants you browser compatibility.

Hope it helps.

var originList = [
  {"id":1,"name":"www"},
  {"id":2, "name":"fff"},
  {"id":3, "name":"ddd"},
  {"id":4, "name":"aaaa"},
  {"id":5, "name":"zzzz"},
  {"id":6, "name":"Susane"},
  {"id":7,"name":"yyy"}
];
	
var rest = [
  {"id":1,"name":"www"},
  {"id":2, "name":"fff"},
  {"id":3, "name":"ddd"},
  {"id":4, "name":"aaaa"},
  {"id":5, "name":"zzzz"},
];

console.log(_.differenceWith(originList, rest, _.isEqual));
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.5/lodash.js"></script>

1 Comment

I don't have it, just jQuery
0

var originList = [
    {"id":1,"name":"www"},
    {"id":2, "name":"fff"},
    {"id":3, "name":"ddd"},
    {"id":4, "name":"aaaa"},
    {"id":5, "name":"zzzz"},
    {"id":6, "name":"Susane"},
    {"id":7,"name":"yyy"}
];

var rest = [
    {"id":1,"name":"www"},
    {"id":2, "name":"fff"},
    {"id":3, "name":"ddd"},
    {"id":4, "name":"aaaa"},
    {"id":5, "name":"zzzz"},
];

const newList = originList.filter(function(originListVal) {
  return rest.reduce(function(currentVal, nextRestVal) {
    return currentVal || originListVal.id === nextRestVal.id;
  }, false);
});

console.log(newList);

6 Comments

@Andreas => I will update my comment now. I always forget that they don't support it in IE, but they do in Edge.
But here says it's IE compatible
@AXL It was about arrow functions (=>) and not Array.prototype.filter()
@Andreas => Was this a solution to your question?
What question? O.o
|
0
let newList = originList.filter(ele => !rest.some(fil => ele.id === fil.id));

If cannot use arrow functions

let newList = originList.filter(function(ele) {
  return rest.some(function(fil) {
    return ele.id === fil.id;
  });
});

4 Comments

changing arrow functions is not a big deal.
updated the answer.
That is the opposite result I expect. I want a new list without ids 6 & 7
sorry about that, updated.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.