0

I have two Arrays of Objects. While localDataArray is is already stored inside my app,remoteUpdateDataArray comes from the backend.

var localDataArray =   [
    { "date": "10/01/19", "category": "surf", "hash": "da39a3ee5e6b4b0d3255bfef95601890afd80709"}, 
    { "date": "10/01/19", "category": "skate", "hash": "a0f1490a20d0211c997b44bc357e1972deab8ae3"},
    { "date": "10/01/19", "category": "skate", "hash": "54fd1711209fb1c0781092374132c66e79e2241b"}
];


var remoteUpdateDataArray =   [
     { "date": "12/01/19", "category": "surf", "hash": "4a0a19218e082a343a1b17e5333409af9d98f0f5"}, 
     { "date": "11/01/19", "category": "surf", "hash": "54fd1711209fb1c0781092374132c66e79e2241b"}, 
     { "date": "10/01/19", "category": "surf", "hash": "da39a3ee5e6b4b0d3255bfef95601890afd80709"}, 
     { "date": "10/01/19", "category": "skate", "hash": "a0f1490a20d0211c997b44bc357e1972deab8ae3"},
     { "date": "10/01/19", "category": "skate", "hash": "54fd1711209fb1c0781092374132c66e79e2241b"}
];

I want to remove all duplicated objects from remoteUpdateDataArray. The unique identifier of each object is the hash.

So far, I have the following code:

let hashValue = "54fd1711209fb1c0781092374132c66e79e2241b"

var filteredResult = remoteUpdateDataArray.filter(x => x.hash !== hashValue);

Result:

var filteredResult =   [
         { "date": "12/01/19", "category": "surf", "hash": "4a0a19218e082a343a1b17e5333409af9d98f0f5"}, 
         { "date": "11/01/19", "category": "surf", "hash": "54fd1711209fb1c0781092374132c66e79e2241b"}, 
         { "date": "10/01/19", "category": "surf", "hash": "da39a3ee5e6b4b0d3255bfef95601890afd80709"}, 
         { "date": "10/01/19", "category": "skate", "hash": "a0f1490a20d0211c997b44bc357e1972deab8ae3"}
    ];

How do I manage to also get rid of the other (in this case two duplicate objects) inside the array? Keep in mind that these arrays may get pretty big.

1
  • Why did you mention localDataArray if it actually doesn't matter for your problem? Commented Jan 15, 2019 at 10:26

4 Answers 4

3

I'd build a list of hashes from your first array (to save iterations), then simply filter using includes()

const inLocalData = localDataArray.map(({hash: e}) => e);
const result = remoteUpdateDataArray.filter(({hash: e}) => ! inLocalData.includes(e));
console.log(result);
<script>
var localDataArray = [{
    "date": "10/01/19",
    "category": "surf",
    "hash": "da39a3ee5e6b4b0d3255bfef95601890afd80709"
  },
  {
    "date": "10/01/19",
    "category": "skate",
    "hash": "a0f1490a20d0211c997b44bc357e1972deab8ae3"
  },
  {
    "date": "10/01/19",
    "category": "skate",
    "hash": "54fd1711209fb1c0781092374132c66e79e2241b"
  }
];


var remoteUpdateDataArray = [{
    "date": "12/01/19",
    "category": "surf",
    "hash": "4a0a19218e082a343a1b17e5333409af9d98f0f5"
  },
  {
    "date": "11/01/19",
    "category": "surf",
    "hash": "54fd1711209fb1c0781092374132c66e79e2241b"
  },
  {
    "date": "10/01/19",
    "category": "surf",
    "hash": "da39a3ee5e6b4b0d3255bfef95601890afd80709"
  },
  {
    "date": "10/01/19",
    "category": "skate",
    "hash": "a0f1490a20d0211c997b44bc357e1972deab8ae3"
  },
  {
    "date": "10/01/19",
    "category": "skate",
    "hash": "54fd1711209fb1c0781092374132c66e79e2241b"
  }
];
</script>

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

4 Comments

Thanks, was almost there! This one is working like a charm. Will consider the other solutions when it comes to performance optimizing.
@bambam This code works fine with the given data set. But what if there are duplicate elements in the remoteUpdateDataArray (which it is in the given data set) and that element is not present in the localDataArray? In that case, the duplicate elements are not filtered out. I just tested it. You might have to modify the code a bit.
Which of the duplicates (if any) in remoteDataArray should be kept @MarcJohnson the first or the latest?
@bambam I would got for the latest!
1

It seems U need a cache. Store the hash into the cache, and update it while U get new data.

var localDataArray =   [
    { "date": "10/01/19", "category": "surf", "hash": "da39a3ee5e6b4b0d3255bfef95601890afd80709"}, 
    { "date": "10/01/19", "category": "skate", "hash": "a0f1490a20d0211c997b44bc357e1972deab8ae3"},
    { "date": "10/01/19", "category": "skate", "hash": "54fd1711209fb1c0781092374132c66e79e2241b"}
];

var cache = {}

// The result, make a copy of local data at start
var filtered = [].concat(localDataArray)

// initialize the cache
localDataArray.forEach(item => {
    cache[item.hash] = true
})

// -----------

var remoteUpdateDataArray =   [
     { "date": "12/01/19", "category": "surf", "hash": "4a0a19218e082a343a1b17e5333409af9d98f0f5"}, 
     { "date": "11/01/19", "category": "surf", "hash": "54fd1711209fb1c0781092374132c66e79e2241b"}, 
     { "date": "10/01/19", "category": "surf", "hash": "da39a3ee5e6b4b0d3255bfef95601890afd80709"}, 
     { "date": "10/01/19", "category": "skate", "hash": "a0f1490a20d0211c997b44bc357e1972deab8ae3"},
     { "date": "10/01/19", "category": "skate", "hash": "54fd1711209fb1c0781092374132c66e79e2241b"}
];

// filter duplicated items
remoteUpdateDataArray.forEach(item => {
    // item exists
    if(cache.hasOwnProperty(item.hash)) {
        return
    }
    // append hash and item
    cache[item.hash] = true
    // just append the new data items
    filtered.push(item)
})

Comments

1

why not use uniqBy from lodash?

first, join the 2 arrays with spred opertaor(you can read more about it here):

const newArray = [...localDataArray ,...remoteUpdateDataArray];

and when you have the new array with all the duplications

const filteredResult = _.uniqBy(newObject,'hash');

Comments

0

you can use this also

var localDataArray = [{
    "date": "10/01/19",
    "category": "surf",
    "hash": "da39a3ee5e6b4b0d3255bfef95601890afd80709"
  },
  {
    "date": "10/01/19",
    "category": "skate",
    "hash": "a0f1490a20d0211c997b44bc357e1972deab8ae3"
  },
  {
    "date": "10/01/19",
    "category": "skate",
    "hash": "54fd1711209fb1c0781092374132c66e79e2241b"
  }
];
var remoteUpdateDataArray = [{
    "date": "12/01/19",
    "category": "surf",
    "hash": "4a0a19218e082a343a1b17e5333409af9d98f0f5"
  },
  {
    "date": "11/01/19",
    "category": "surf",
    "hash": "54fd1711209fb1c0781092374132c66e79e2241b"
  },
  {
    "date": "10/01/19",
    "category": "surf",
    "hash": "da39a3ee5e6b4b0d3255bfef95601890afd80709"
  },
  {
    "date": "10/01/19",
    "category": "skate",
    "hash": "a0f1490a20d0211c997b44bc357e1972deab8ae3"
  },
  {
    "date": "10/01/19",
    "category": "skate",
    "hash": "54fd1711209fb1c0781092374132c66e79e2241b"
  }
];


let data =[];

localDataArray.map(item=>{
 data= remoteUpdateDataArray.find(el=>item.hash!=el.hash)
  
})

console.log(data)

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.