Home > database >  cross match 2 arrays and do something
cross match 2 arrays and do something

Time:11-22

How can I check for a value in an array and if it exists, then add something in another array?

I have two very large arrays, one with people and another one that is an array of blogs. I need to use js in order to do this. My arrays look something like this(i shortened the arrays for the example):

arr2 = 
[
    {
        "post_title": "In-House Counsel’s Role in Optimizing Intellectual Property",
        "post_date": "@1667970000",
        "custom":
        {
            "author":
            [
                {
                    "type": "attorney",
                    "identifier": "Deborah Stehr"
                }
            ]
        }
    },
    {
        "post_title": "Understanding the Privacy Right to Be Forgotten",
        "post_date": "@1667797200",
        "custom":
        {
            "author":
            [
                {
                    "type": "attorney",
                    "identifier": "Lori S. Ross"
                }
            ]
        }
    },
    {
        "post_title": "3 Creative Ideas for Writing a More Compelling Collection Letter",
        "post_date": "@1667361600",
        "custom":
        {
            "author":
            [
                {
                    "type": "attorney",
                    "identifier": "Brian Heller"
                }
            ]
        }
    },
    {
        "post_title": "FTC Shines a Light on Digital Dark Patterns",
        "post_date": "@1666670400",
        "custom":
        {
            "author":
            [
                {
                    "type": "attorney",
                    "identifier": "Lori S. Ross"
                }
            ]
        }
    },
    {
        "post_title": "New California Law Requires Physicians to Notify Patients about Open Payments Database",
        "post_date": "@1666584000",
        "custom":
        {
            "author":
            [
                {
                    "type": "attorney",
                    "identifier": "Deborah Stehrn"
                }
            ]
        }
    },
    {
        "post_title": "Exclusive Use Provisions in Commercial Leases",
        "post_date": "@1663819200",
        "custom":
        {
            "author":
            [
                {
                    "type": "attorney",
                    "identifier": "Brian Heller"
                }
            ]
        }
    }
]

and my other array

arr1 = 
[
    {
        "post_title": "Deborah Stehr",
        "custom":
        {
            "title": "Senior Counsel"
        }
    },
    {
        "post_title": "Lori S. Ross",
        "custom":
        {
            "title": "Attorney, Member of the Firm"
        }
    },
    {
        "post_title": "Brian Heller",
        "custom":
        {
            "title": "Attorney, Member of the Firm"
        }
    }
]

I want the arr1 to output this in the end

[
    {
        "post_title": "Deborah Stehr",
        "custom":
        {
            "title": "Senior Counsel",
            "related_posts":
            [
                {
                    "type": "blog",
                    "identifier": "In-House Counsel’s Role in Optimizing Intellectual Property"
                },
                {
                    "type": "blog",
                    "identifier": "New California Law Requires Physicians to Notify Patients about Open Payments Database"
                }
            ]
        }
    },
    {
        "post_title": "Lori S. Ross",
        "custom":
        {
            "title": "Attorney, Member of the Firm",
            "related_posts":
            [
                {
                    "type": "blog",
                    "identifier": "FTC Shines a Light on Digital Dark Patterns"
                },
                {
                    "type": "blog",
                    "identifier": "Understanding the Privacy Right to Be Forgotten"
                }
            ]
        }
    },
    {
        "post_title": "Brian Heller",
        "custom":
        {
            "title": "Attorney, Member of the Firm",
            etc.
        }
    }
]

this is what ive tried. i was going to make a new loop each time for each author...

     for(var i=0, len = arr2.length; i < len; i  ){
          if(arr2[i].custom.author[0].identifier == 'Deborah Stehr'){
            for(var t=0, len = arr1.length; t< len; t  ){
              if(arr1[t].post_title == 'Deborah Stehr'){
                  arr1[t].custom.related_posts = [];
                  arr1[t].custom.related_posts.push({
                    "type": "blog",
                    "identifier": arr2[i].post_title
                  })
                }
          }
        }
      }


     for(var i=0, len = arr2.length; i < len; i  ){
          if(arr2[i].custom.author[0].identifier == 'Lori S. Ross'){
            for(var t=0, len = arr1.length; t< len; t  ){
              if(arr1[t].post_title == 'Lori S. Ross'){
                  arr1[t].custom.related_posts = [];
                  arr1[t].custom.related_posts.push({
                    "type": "blog",
                    "identifier": arr2[i].post_title
                  })
                }
          }
        }
      }

this works but it doesnt get each object from arr2. it only gets the first one. how can i loop through both arrays to achieve this?

CodePudding user response:

You're only getting the first one because you do

arr1[t].custom.related_posts = [];

before adding the related post. You should do this once, before the loop that finds all the related posts.

You don't need separate loops for each post title. Just loop through arr1, then find all the matching elements in arr2.

Using forEach() will make the code much easier to read, since you don't need all those long names with lots of array indexes.

const arr1 = [{
    "post_title": "Deborah Stehr",
    "custom": {
      "title": "Senior Counsel"
    }
  },
  {
    "post_title": "Lori S. Ross",
    "custom": {
      "title": "Attorney, Member of the Firm"
    }
  },
  {
    "post_title": "Brian Heller",
    "custom": {
      "title": "Attorney, Member of the Firm"
    }
  }
];

const arr2 = [{
    "post_title": "In-House Counsel’s Role in Optimizing Intellectual Property",
    "post_date": "@1667970000",
    "custom": {
      "author": [{
        "type": "attorney",
        "identifier": "Deborah Stehr"
      }]
    }
  },
  {
    "post_title": "Understanding the Privacy Right to Be Forgotten",
    "post_date": "@1667797200",
    "custom": {
      "author": [{
        "type": "attorney",
        "identifier": "Lori S. Ross"
      }]
    }
  },
  {
    "post_title": "3 Creative Ideas for Writing a More Compelling Collection Letter",
    "post_date": "@1667361600",
    "custom": {
      "author": [{
        "type": "attorney",
        "identifier": "Brian Heller"
      }]
    }
  },
  {
    "post_title": "FTC Shines a Light on Digital Dark Patterns",
    "post_date": "@1666670400",
    "custom": {
      "author": [{
        "type": "attorney",
        "identifier": "Lori S. Ross"
      }]
    }
  },
  {
    "post_title": "New California Law Requires Physicians to Notify Patients about Open Payments Database",
    "post_date": "@1666584000",
    "custom": {
      "author": [{
        "type": "attorney",
        "identifier": "Deborah Stehrn"
      }]
    }
  },
  {
    "post_title": "Exclusive Use Provisions in Commercial Leases",
    "post_date": "@1663819200",
    "custom": {
      "author": [{
        "type": "attorney",
        "identifier": "Brian Heller"
      }]
    }
  }
];

arr1.forEach(post => {
  let title = post.post_title;
  post.custom.related_posts = [];
  arr2.forEach(post2 => {
    if (post2.custom.author[0].identifier == title) {
      post.custom.related_posts.push({
        type: "blog",
        identifier: post2.post_title
      });
    }
  });
});

console.log(arr1);

  • Related