I'm studying a bit of js objects lately (I'm still learning). I'm stucked on a project of mine where I need to check if 2 objects inside an array (of objects) are identical between them and between them and another object in a different array. These two initial objects will have to behave differently if certain instances occurs:
I'll try to be as clear as possible. My code has 2 arrays of objects:
let array1 = [{name: "New York", value: "A", price: 43, status: "not checked"},
{name: "Not Specified", value: "A", price: 333, status: "not checked"},
{name: "Barry", value: "C", price: 48, status: "not checked"},..etc.];
let array2 = [{name: "John", value: "A", price: 23, status: "not checked"},
{name: "Jerry", value: "A", price: 67, status: "not checked"},
{name: "Barry", value: "C", price: 48, status: "not checked"},
{name: "Tom", value: "F", price: 23, status: "not checked"},
{name: "Barry", value: "C", price: 48, status: "not checked"},...etc.];
As you can see initial status of every objects is "not checked". Now, part of my code loops one array (array2, the bigger of the two) to check if, at the same position "i", they have objects inside with different properties/values:
for (let i = 0; i < array2.length; i++){
if (array1[i].value != array2[i].value){
...do something...}.
Now, as you can see there is also one result in common in array1 and array2:
{name: "Barry", value: "C", price: 48, status: "not checked"}
What I would like to do is this:
I'm sorry for the wall of text, but being pretty noob is hard to describe sometimes what I'm trying to do. Thanks for any suggestion!
You will need to call a find
function for each element in the first array. Something like this:
array1.forEach(item => {
var found = array2.find(item2 => item2.value === item.value)
if (found) {
// do something
}
}
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.