简体   繁体   中英

How to check whether all the key & value of a JS Object exist in the another JS object in JavaScript?

Suppose there are two JavaScript objects.

first = [{valueOne: "DescOne"},{valueTwo: "Desctwo"},{valueThree: "DescThree"},{valueFour: "DescFour"}];

second= [{valueTwo: "Desctwo"},{valueFour: "DescFour"}];

What I actually want that if all the key and value of second JSON Object exist in first JSON Object then it will return match = true or true else it will return match = false or false .

Note: The value of first and second will change dynamically and its index too

I have to do this in ES

Any help would be appreciated.

Apply every on second array and use Object.entries to extract key and value, then check if it is present in the first array.

 const second = [{valueTwo: "Desctwo"},{valueFour: "DescFour"}] const first = [{valueOne: "DescOne"},{valueTwo: "Desctwo"},{valueThree: "DescThree"}, {valueFour: "DescFour"}]; const output = second.every((obj) => { const [secondKey, secondVal] = Object.entries(obj)[0]; return first.some((obj) => { const [firstKey, firstVal] = Object.entries(obj)[0]; return firstKey == secondKey && firstVal == secondVal; }); }); console.log(output);

const second = [{valueTwo: "Desctwo"},{valueFour: "DescFour"}]
const first = [{valueOne: "DescOne"},{valueTwo: "Desctwo"},{valueThree: "DescThree"}, {valueFour: "DescFour"}];

const res = second.every((i) => (
  first.find(j => JSON.stringify(i) === JSON.stringify(j))
))

Here is a hack to achieve the same:

let arr1 = [{valueOne: "DescOne"},{valueTwo: "Desctwo"},{valueThree: "DescThree"},{valueFour: "DescFour"}];

let arr2 = [{valueTwo: "Desctwo"},{valueFour: "DescFour"}];

arr1 = arr1.map(x => JSON.stringify(x));
arr2 = arr2.map(x => JSON.stringify(x));

console.log(arr1);

console.log(arr2);


let intersection = arr1.filter(value => arr2.includes(value));
intersection = intersection.map(x => JSON.parse(x));

console.log(intersection);

 var first = [{valueOne: "DescOne"},{valueTwo: "Desctwo"},{valueThree: "DescThree"},{valueFour: "DescFour"}]; var second= [{valueTwo: "Desctwo"},{valueFour: "DescFour"}]; var firstObjKeys = [], firstObjValues = []; var secondObjKeys = [], secondObjValues = []; first.forEach(element => Object.keys(element).forEach(function(key) { firstObjKeys.push(key); firstObjValues.push(element[key]); })); second.forEach(item => Object.keys(item).forEach(function(key) { secondObjKeys.push(key); secondObjValues.push(item[key]); })); if (JSON.stringify(firstObjKeys.sort()) !== JSON.stringify(secondObjKeys.sort()) || JSON.stringify(firstObjValues.sort()) !== JSON.stringify(secondObjValues.sort())) { console.log('not equal'); } else { console.log('equal'); }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

How about this:

 var first = [{valueOne: "DescOne"},{valueTwo: "Desctwo"},{valueThree: "DescThree"},{valueFour: "DescFour"}]; var second= [{valueTwo: "Desctwo"},{valueFour: "DescFour"}]; var firstObjKeyStr = '', secondObjKeyStr = ''; var firstObjValueStr = '', secondObjValueStr = ''; var firstObjKeys = [], firstObjValues = []; var secondObjKeys = [], secondObjValues = []; first.forEach(element => Object.keys(element).forEach(function(key) { firstObjKeys.push(key); firstObjValues.push(element[key]); })); firstObjKeyStr = JSON.stringify(firstObjKeys.sort()); firstObjValueStr = JSON.stringify(firstObjValues.sort()); second.forEach(item => Object.keys(item).forEach(function(key) { secondObjKeys.push(key); secondObjValues.push(item[key]); })); secondObjKeyStr = JSON.stringify (secondObjKeys.sort()); secondObjValueStr = JSON.stringify(secondObjValues.sort()); if (firstObjKeyStr !== secondObjKeyStr || firstObjValueStr !== secondObjValueStr) { console.log('not equal'); } else { console.log('equal'); }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM