0

I have two(2) arrays, beforeArray & afterArray

const beforeArray = [
   {
    id:1, 
    name:'Alice'
   },
   {
    id:2, 
    name:'Rabbit'
   },
   {
    id:3, 
    name:'Queen'
   },
   {
    id:4, 
    name:'King'
   },
];

const afterArray = [
   {
    id:3, 
    name:'Queen'
   },
];

I'm trying to get the difference of beforeArray & afterArray like this array below:

const currentArray = [
   {
    id:1, 
    name:'Alice'
   },
   {
    id:2, 
    name:'Rabbit'
   },
   {
    id:4, 
    name:'King'
   },
];

I tried using these way but it only returns the value from let currentArray = [];:

let currentArray = [];

await afterArray.map(async item => {
    await beforeArray.filter(async find => {
        if (item.id != find.id) {
            await currentArray.map(async less => {
                if (less.id == find.id) {
                    return;
                } else {
                    currentArray = await [...currentArray, find];
                }
            });
        }
    })

console.log(currentArray);

I think it console.log(currentArray) skips the await.... How can I get the value from the awaited map and filter?

4
  • based on the name property are you trying to find the difference ? what is the rule for finding difference ? Commented May 15, 2019 at 7:38
  • 1
    What is with all the async and awaits? I don't think any of this is async code. Commented May 15, 2019 at 7:40
  • Possible duplicate of How to get the difference between two arrays in Javascript? Commented May 15, 2019 at 7:42
  • @KunalMukherjee I'm trying to remove the {id:3, name:'Queen'} from the beforeArray and pass it on to the currentArray Commented May 15, 2019 at 7:43

2 Answers 2

1

Try filtering the array using Array.prototype.some like this:

const beforeArray = [{
    id: 1,
    name: 'Alice'
  },
  {
    id: 2,
    name: 'Rabbit'
  },
  {
    id: 3,
    name: 'Queen'
  },
  {
    id: 4,
    name: 'King'
  },
];

const afterArray = [{
  id: 3,
  name: 'Queen'
}];

const currentArray = beforeArray.filter(x => {
  return afterArray.some(y => y.id !== x.id && y.name !== x.name);
});

console.log(currentArray);

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

Comments

-1

You should not be using the async/await until you want to wait for a promise to get resolved.

You can filter array using Array.prototype.filter and Array.prototype.find

const beforeArray = [
	{
	 id:1, 
	 name:'Alice'
	},
	{
	 id:2, 
	 name:'Rabbit'
	},
	{
	 id:3, 
	 name:'Queen'
	},
	{
	 id:4, 
	 name:'King'
	},
];

const afterArray = [
	{
	 id:3, 
	 name:'Queen'
	},
];

let out = beforeArray.filter(({id, name}) => !afterArray.find(r => r.id === id && r.name === name));

console.log(out)

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.