compare two objects and return true if part of the object key value pair exists
You can use every()
on the array produced by Object.entries()
to test if every key/value pair in the expected matches the actual:
const expectedValue = { city: "San Jose", state: "california", zip: "95234"}
const actualObject = { address: "5543322 Adam St", city: "San Jose", state: "california", zip: "95234", country: "USA"}
let test = Object.entries(expectedValue) .every(([key, value]) => actualObject[key] === value )
console.log(test)
How to compare two different Object keys and update it's value if keys are same in Javascript?
You can do this by using the spread syntax.
Just spread the old object first followed by the new object.
Matching keys if any would be updated by the values from the new object and new keys in the new object would be added:
let oldObject = {name:"Dhanush",age:24,sex:"Male",education:"Btech"}
let newObject = {name:"Dhanush kumar S",age:23,sex:"Male"}
const merge = (oldObj, newObj) => {
return {...oldObj, ...newObj};
}
console.log(merge(oldObject, newObject));
javascript compare value of a certain key in all objects
Get the value of the first element, then use every to compare it to other values.every will return false on the first non-match. There is no need to check every element in the array after you found ONE that doesn't match.
let list =[ { name: 'foo', country: 'Foos' }, { name: 'bar', country: 'Foos' }, { name: 'baz', country: 'Foos' }];
function isCountryTheSameInAllObjects(list) { if (!(list && list.length)) return true; // If there is no list, or if it is empty, they are all the same, aren't they? let compare = list[0].country; return list.every( item => item.country === compare);}
console.log('is Country same?', isCountryTheSameInAllObjects(list));
Comparing two similar objects (holding same keys) and returning key value pairs that are different
Loop through one object, then compare its values with the corresponding element of the other.
const initialValues= { name: "Robert Terrell", email: "billy@gmail.com" };
const formData = { name: 'New Name', email: 'new email' };
for (const [key, value] of Object.entries(initialValues)) {
if (value != formData[key]) {
console.log(`${key}: ${value} != ${formData[key]}`);
}
}
Compare two objects and get common values JavaScript
Here is a simple example that uses a combination of Object.values and Object.keys as arrays that are filtered to produce your prescribed output:
let obj1 = { "val1":"test", "stream":{ "iscommisonAccount":false, "istradeAccount":true } };
let obj2 = { "val1":"test", "stream":{ "iscommisonAccount":true, "istradeAccount":true } };
let obj3 = { "val1":"test", "stream":{ "iscommisonAccount":true, "istradeAccount":true } };
let obj4 = { "val1":"test", "stream":{ "iscommisonAccount":false, "istradeAccount":true } };
let obj5 = { "val1":"test", "stream":{ "iscommisonAccount":false, "istradeAccount":false } };
let obj6 = { "val1":"test", "stream":{ "iscommisonAccount":false, "istradeAccount":false } };
let obj7 = { "val1":"test", "stream":{ "iscommisonAccount":true, "istradeAccount":false } };
let obj8 = { "val1":"test", "stream":{ "iscommisonAccount":true, "istradeAccount":false } };
interface Data {stream:{[key: string]: boolean}};
function objFilter(objA: Data, objB: Data): Data {
let out: Data = {stream:{}};
Object.keys(objA.stream).filter((value, idx) =>
Object.values(objA.stream)[idx] === Object.values(objB.stream)[idx] ?
out.stream[value] = Object.values(objA.stream)[idx] :
false
);
return out;
}
console.log(objFilter(obj1, obj2)); //istradeAccount
console.log(objFilter(obj3, obj4)); //istradeAccount
console.log(objFilter(obj5, obj6)); //iscommisonAccount && istradeAccount
console.log(objFilter(obj7, obj8)); //iscommisonAccount && istradeAccount
console.log(objFilter(obj2, obj7)); //iscommisonAccount
Comparing certain key, value pairs within an object - No Direct Matches (JavaScript)
First. Let's separate our sellers from the buyers by filtering each group.
var data = [{...}] // assume is the long list of data you posted
var buyers = data.filter(function(item) {return item.type === 'Buyer'});
var sellers = data.filter(function(item) {return item.type === 'Seller'});
Now we have 2 arrays buyers
and sellers
. We can now iterate the buyers and search for matching sellers
buyers.forEach(function(buyer) {
sellers.forEach(function(seller) {
// Here we can compare our buyers and sellers.
// For each buyer we'll iterate over all the sellers and look for a match.
if (buyer.price >= seller.price) {
// You've found a match! now do something with it.
// Of course here we are comparing only price, you may want to compare
// multiple keys, like if it's the same product.
}
})
})
How to compare elements in an object and return the higher value? *Javascript*
On each iteration check if the current or previous key value is the largest, and store. Store the largest in the largest
variable. In the end return the largest
variable, and it's value (object[largest]
):
let obj = object => { let largest; for(const i in object) { // i represents the "key" of the objects, a,b,c,d if(!largest || object[i] > object[largest]) { largest = i; } } return { [largest]: object[largest] };}
console.log(obj({a:39,b:21,c:12,d:4}));
Related Topics
Parsing Error: Unexpected Token, Expected ","
How to Call a Function When Element Is Loaded At Angular
How to Set an Iframe Src Attribute from a Variable in Angularjs
The Onclick Is Not Working on First Click
Nested Json: How to Add (Push) New Items to an Object
How to Insert Space Every 4 Characters for Iban Registering
How to Listen to the Window Scroll Event in a Vuejs Component
Vue.Js - How to Concat One Key Value to Another Key'S Value
How to Disable a Message When the Password Value Is Empty
Merge Two Json/Javascript Arrays in to One Array
Set Focus to the Next Input Element
Navigationduplicated Navigating to Current Location ("/Search") Is Not Allowed
How to Put a Variable in an Src Attribute
How to Make Mongoose Not Insert Empty Array or Object Fields into a Document