1

I have two arrays one is an original array and second is the copy of an original array. I put some new items in an array and update some of one and want to compare it with copy array and want to eliminate those items which are in copy array and keep those items where Id = null

 var original = [
    {
        "Id": 1,
        "BrandConstruct": 265,
        "YearPlanData": "a"
    },
    {   "Id": 2,
        "BrandConstruct": 236,
        "YearPlanData": "c"
    },
    {   "Id": 3,
        "BrandConstruct": 376,
        "YearPlanData": "b"
    },
    {   "Id": null,
        "BrandConstruct": 476,
        "YearPlanData": "e"
    }, 
    {   "Id": null,
        "BrandConstruct": 576,
        "YearPlanData": "f"
    }

]

    var copy = [
    {
        "Id": 1,
        "BrandConstruct": 165,
        "YearPlanData": "a"
    },
    {   "Id": 2,
        "BrandConstruct": 236,
        "YearPlanData": "c"
    },
    {   "Id": 3,
        "BrandConstruct": 376,
        "YearPlanData": "b"
    }

]

These are two arrays with properties Id, BrandConstruct, YearPlanData I add the new item or may be multiple items where all will have Id = null and other properties will have any data or may be duplicate data so I want to eliminate duplicate data using copy array but keep data where Id = null so I want this type of result after comparison and it must be work on IE

    var original = [
    {
        "Id": 1,
        "BrandConstruct": 265,
        "YearPlanData": "a"
    },

    {   "Id": null,
        "BrandConstruct": 476,
        "YearPlanData": "e"
    }, 
    {   "Id": null,
        "BrandConstruct": 576,
        "YearPlanData": "f"
    }

]
2
  • Is this a specific angularjs question or a general js question? Commented Jul 6, 2018 at 9:11
  • I am using Angular1 and using handsontable table plugin so I fetch data from server side and make an array and make updates and again send this table data in the form of array so problem is I do not want those rows which are unchanged just new inserted and updated @Brakebien Commented Jul 6, 2018 at 9:14

4 Answers 4

5

const newOriginal = original.filter(obj => {
    return !copy.find(copyObj => JSON.stringify(copyObj) === JSON.stringify(obj))
});

This should work at Internet Explorer:

const newOriginal = original.filter(function(obj) {
    return !copy.filter(function(copyObj) { return JSON.stringify(copyObj) === JSON.stringify(obj)}).length
});

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

6 Comments

it is working but Arrow (=>) not working on IE so it shows error on IE @Nir Berko
Thanks, it is working perfectly on all browsers @Nir Berko
Sorry but this code also throw error on IE @Nir Berko
Check the Image I just add it @Nir Berko
If I use this code then also there is shown a "unspecified error" on <! DOCTYPE html> on IE 11
|
1

This works but I think you should wait for a better solution.

var original = [{
  "Id": 1,
  "BrandConstruct": 265,
  "YearPlanData": "a"
}, {
  "Id": 2,
  "BrandConstruct": 236,
  "YearPlanData": "c"
}, {
  "Id": 3,
  "BrandConstruct": 376,
  "YearPlanData": "b"
}, {
  "Id": null,
  "BrandConstruct": 476,
  "YearPlanData": "e"
}, {
  "Id": null,
  "BrandConstruct": 576,
  "YearPlanData": "f"
}];

var copy = [{
  "Id": 1,
  "BrandConstruct": 165,
  "YearPlanData": "a"
}, {
  "Id": 2,
  "BrandConstruct": 236,
  "YearPlanData": "c"
}, {
  "Id": 3,
  "BrandConstruct": 376,
  "YearPlanData": "b"
}];
copy = copy.map(function(el) {
  return JSON.stringify(el);
});

original = original.map(function(el) {
  return JSON.stringify(el);
}).filter(function(els) {
  return copy.indexOf(els) == -1;
}).map(function(s) {
  return JSON.parse(s);
});
console.log(original);

Comments

1

You can loop through the original array, and push the elements that are not belonged to the copy array to a new array.

function isInArray(item, array) {
  return JSON.stringify(array).indexOf(JSON.stringify(item)) > -1
}

function getFilteredArray(original, copy) {
  var newOriginal = [];

  for (var i = 0; i < original.length; i++) {
    var item = original[i];
    if (!isInArray(item, copy) || item.id === null) newOriginal.push(item);
  }

  return newOriginal;
}

var original = [{
    "Id": 1,
    "BrandConstruct": 265,
    "YearPlanData": "a"
  },
  {
    "Id": 2,
    "BrandConstruct": 236,
    "YearPlanData": "c"
  },
  {
    "Id": 3,
    "BrandConstruct": 376,
    "YearPlanData": "b"
  },
  {
    "Id": null,
    "BrandConstruct": 476,
    "YearPlanData": "e"
  },
  {
    "Id": null,
    "BrandConstruct": 576,
    "YearPlanData": "f"
  }

]

var copy = [{
    "Id": 1,
    "BrandConstruct": 165,
    "YearPlanData": "a"
  },
  {
    "Id": 2,
    "BrandConstruct": 236,
    "YearPlanData": "c"
  },
  {
    "Id": 3,
    "BrandConstruct": 376,
    "YearPlanData": "b"
  }

]

console.log(getFilteredArray(original, copy));

2 Comments

But I also want to keep those elements where Id = null @phuwin
I edited my answer. I added in the check if the id of the item is null, it will also be pushed to the new array.
1

This is for arrray of objects, you can push them into an empty array when the index of the attribute you are searching for is not found, creating a new array of the unique values:

   for (var key in array_of_objects) {
     var index = empty_array.findIndex(x => x.attribute == array_of_objects.attribute)
        if (index === -1){
            empty_array.push(d);
        }
   }

Comments

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.