Quantcast
Channel: Active questions tagged javascript - Stack Overflow
Viewing all articles
Browse latest Browse all 139953

I need to insert values into array 1 from array 2 if they match based upon two other elements

$
0
0

I know that this type of question has been asked before for I'm at a stumbling point trying to get it finished if possible and I need to ask. I have two arrays:

let arr1 = [{
  "Last_Name": "Smith"),
  "First_Name": "John,
  "MI": "I",
  "Fore_Name": ""),
  "Initials: ""
}, "Last_Name": "Doe"),
  "First_Name": "Mary,
  "MI": "A",
  "Fore_Name": "Mary A),
  "Initials: "MA"
},"Last_Name": "Johnson"),
  "First_Name": "Bob",
  "MI": "B",
  "Fore_Name": ""),
  "Initials: ""
}]


let arr2 = [{
  "Last_Name": "Smith"),
  "First_Name": "John,
  "MI": "I",
  "Fore_Name": "John I"),
  "Initials: "JI"
}, "Last_Name": "Doe"),
  "First_Name": "Mary,
  "MI": "A",
  "Fore_Name": "Mary A),
  "Initials: "MA"
},"Last_Name": "Johnson"),
  "First_Name": "Bob",
  "MI": "B",
  "Fore_Name": "Bob B"),
  "Initials: "BB"
}
}]

I want to update array1's ForeName and Initials fields with data from array2 IF they are blank and the Array1["Last_Name"] = array2["Last_Name"] AND the first character of the Array1["First_Name"].slice(0,1) = Array2["Initials"].slice(0,1). If array1's ForeName is not blank then skip it.

I've looked at Javascript update an array based on another array on matching index

and I think it is close. I understand

arra1 = arra1.map(item => {
  let item2 = arra2.find(i2 => i2.Last_Name === item.Last_Name);
 // not sure what to do here return item2 ? { ...item, ...item2 } : item;
});

And not sure how to match the first character of the First_Name with the first character of Array2 initials


Viewing all articles
Browse latest Browse all 139953

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>