0

I have the below type of two arrays, I want to check both array(array1 and array2) and create another array(array3), which has a value, which are not available into array1.

For ex - 'mob2', 'pin', 'address2', 'city' are available in array1 but not in array2. I want to those array keys and values in array3.

let array3 = [];
let array1 = {
    'fname': 'test text',
    'lname': 'test text',
    'pin': 856,
    'address1': 'test text',
    'address2':'test text',
    'city': 'test text',
    'mob1': 35343434343,
    'mob2': 34343434343
};
let array2 = ['fname','lname','mob1','address1'];

Expected Output:

array3['mob2'] = 34343434343;
array3['pin'] = 856;
array3['address2'] = 'test text';
array3['city'] = 'test text';
7
  • Is there a reason why you're using an array instead of an object literal for arr1? Commented Mar 30, 2021 at 9:43
  • Does this answer your question? How to get the difference between two arrays in JavaScript? Commented Mar 30, 2021 at 9:43
  • array1 and array3 should be objects. Commented Mar 30, 2021 at 9:43
  • can i achieve using array or I have to use objects only? Commented Mar 30, 2021 at 9:46
  • you can use an array of array3 and array1, but it doesn't really make sense to do so. Array 2 is fine as an array Commented Mar 30, 2021 at 9:47

1 Answer 1

2

You can first get all the keys from your first object into an array

['fname', 'lname', 'pin', ...] // keys from `obj`

You can then use .filter() on this array to remove any key values that appear in your arr of keys to exclude. This will give you an array of keys to keep, which you can map over to create an array of [key, value] pairs. This key-value pair array can then be used in a call to Object.fromEntries(), which will build your resulting object for you:

const obj = {
  'fname': 'test text',
  'lname': 'test text',
  'pin': 856,
  'address1': 'test text',
  'address2':'test text',
  'city': 'test text',
  'mob1': 35343434343,
  'mob2': 34343434343
};
const arr = ['fname', 'lname', 'mob1', 'address1']; // keys to exclude

const keys = Object.keys(obj).filter(key => !arr.includes(key));
const res = Object.fromEntries(keys.map(key => [key, obj[key]]));
console.log(res);

If the number of keys to exclude is large, consider making a Set first, and then using .has() instead of .includes(). This will allow for efficient lookup within your .filter() callback:

const obj = {
  'fname': 'test text',
  'lname': 'test text',
  'pin': 856,
  'address1': 'test text',
  'address2':'test text',
  'city': 'test text',
  'mob1': 35343434343,
  'mob2': 34343434343
};
const set = new Set(['fname', 'lname', 'mob1', 'address1']); // keys to exclude

const keys = Object.keys(obj).filter(key => !set.has(key));
const res = Object.fromEntries(keys.map(key => [key, obj[key]]));
console.log(res);

Lastly, if the keys in array2 are static, you use destructuring to pull out the keys you want to exclude, and then use the rest pattern ... to obtain an object without those keys. This only works with static keys though:

const obj = { 'fname': 'test text', 'lname': 'test text', 'pin': 856, 'address1': 'test text', 'address2':'test text', 'city': 'test text', 'mob1': 35343434343, 'mob2': 34343434343 };

const {fname, lname, mob1, address1, ...res} = obj;
console.log(res);

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

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.