I have 2 arrays mentioned below.
const arr1 = [{ Id: "a153O000001mjD1QAI", Document_ID__c: 176767 }];
const arr2 = [
{
classification: "Scientific Response",
core_san_localization_translation: ["No"],
core_san_verbal_only: "false",
id: "176767",
major_version_number: "1",
minor_version_number: "0",
name: "Advance Search Doc_General Information",
response_type: ["General"],
size: 70953,
subtype: "Local",
title: "Advance Search Doc_General Information",
type: "Medical Information",
},
{
classification: "Scientific Response",
core_san_localization_translation: ["No"],
core_san_verbal_only: "false",
id: "127462",
major_version_number: "1",
minor_version_number: "0",
name: "APPROVAL verdict APPROVED FOR USE",
response_type: ["Administration"],
size: 1362339,
subtype: "Local",
title: "ahsdjhsahdasld",
type: "Medical Information",
},
{
classification: "Scientific Response",
core_san_localization_translation: ["No"],
core_san_verbal_only: "false",
id: "127461",
major_version_number: "2",
minor_version_number: "0",
name: "APPROVAL verdict APPROVED ,MINOR CHANGES",
response_type: ["Administration"],
size: 1362339,
subtype: "Local",
title: "ahsdjhsahdasld",
type: "Medical Information",
},
{
classification: "Scientific Response",
core_san_answer: "Dosage",
core_san_localization_translation: ["No"],
core_san_verbal_only: "false",
id: "176776",
major_version_number: "1",
minor_version_number: "0",
name: "General Info Test 2",
response_type: ["Pharmacokinetics and Pharmacodynamics"],
size: 25432,
subtype: "Local",
title: "General Info Test 2",
type: "Medical Information",
},
];
I want to search the value of attribute 'Document_ID__c' from arr1 in arr2 and if it matches '176767' on any element in arr2, just create a new array and store that element.
For e.g my desired output is
const finalarray = [
{
classification: "Scientific Response",
core_san_localization_translation: ["No"],
core_san_verbal_only: "false",
id: "176767",
major_version_number: "1",
minor_version_number: "0",
name: "Advance Search Doc_General Information",
response_type: ["General"],
size: 70953,
subtype: "Local",
title: "Advance Search Doc_General Information",
type: "Medical Information",
},
];
Please help me on this.
CodePudding user response:
const arr1 = [{ Id: "a153O000001mjD1QAI", Document_ID__c: 176767 }];
const arr2 = [
{
classification: "Scientific Response",
core_san_localization_translation: ["No"],
core_san_verbal_only: "false",
id: "176767",
major_version_number: "1",
minor_version_number: "0",
name: "Advance Search Doc_General Information",
response_type: ["General"],
size: 70953,
subtype: "Local",
title: "Advance Search Doc_General Information",
type: "Medical Information",
},
{
classification: "Scientific Response",
core_san_localization_translation: ["No"],
core_san_verbal_only: "false",
id: "127462",
major_version_number: "1",
minor_version_number: "0",
name: "APPROVAL verdict APPROVED FOR USE",
response_type: ["Administration"],
size: 1362339,
subtype: "Local",
title: "ahsdjhsahdasld",
type: "Medical Information",
},
{
classification: "Scientific Response",
core_san_localization_translation: ["No"],
core_san_verbal_only: "false",
id: "127461",
major_version_number: "2",
minor_version_number: "0",
name: "APPROVAL verdict APPROVED ,MINOR CHANGES",
response_type: ["Administration"],
size: 1362339,
subtype: "Local",
title: "ahsdjhsahdasld",
type: "Medical Information",
},
{
classification: "Scientific Response",
core_san_answer: "Dosage",
core_san_localization_translation: ["No"],
core_san_verbal_only: "false",
id: "176776",
major_version_number: "1",
minor_version_number: "0",
name: "General Info Test 2",
response_type: ["Pharmacokinetics and Pharmacodynamics"],
size: 25432,
subtype: "Local",
title: "General Info Test 2",
type: "Medical Information",
},
];
let finalArray = [];
arr1.forEach(ar1 => finalArray.push(arr2.filter(ar2 => ar2.id ===ar1.Document_ID__c)));
console.log(finalArray);
CodePudding user response:
Try this:
let mArray = []
for (i in arr1) {
for (j in arr2){
if (arr1[i].Document_ID__c == arr2[j].id) {
mArray.push(arr2[j])
}
}
}
console.log(mArray);
CodePudding user response:
You can try This :
const arr1 = [{ Id: "a153O000001mjD1QAI", Document_ID__c: 176767 }];
const arr2 = [
{
classification: "Scientific Response",
core_san_localization_translation: ["No"],
core_san_verbal_only: "false",
id: "176767",
major_version_number: "1",
minor_version_number: "0",
name: "Advance Search Doc_General Information",
response_type: ["General"],
size: 70953,
subtype: "Local",
title: "Advance Search Doc_General Information",
type: "Medical Information",
},
{
classification: "Scientific Response",
core_san_localization_translation: ["No"],
core_san_verbal_only: "false",
id: "127462",
major_version_number: "1",
minor_version_number: "0",
name: "APPROVAL verdict APPROVED FOR USE",
response_type: ["Administration"],
size: 1362339,
subtype: "Local",
title: "ahsdjhsahdasld",
type: "Medical Information",
},
{
classification: "Scientific Response",
core_san_localization_translation: ["No"],
core_san_verbal_only: "false",
id: "127461",
major_version_number: "2",
minor_version_number: "0",
name: "APPROVAL verdict APPROVED ,MINOR CHANGES",
response_type: ["Administration"],
size: 1362339,
subtype: "Local",
title: "ahsdjhsahdasld",
type: "Medical Information",
},
{
classification: "Scientific Response",
core_san_answer: "Dosage",
core_san_localization_translation: ["No"],
core_san_verbal_only: "false",
id: "176776",
major_version_number: "1",
minor_version_number: "0",
name: "General Info Test 2",
response_type: ["Pharmacokinetics and Pharmacodynamics"],
size: 25432,
subtype: "Local",
title: "General Info Test 2",
type: "Medical Information",
},
];
var newObj = arr1.map(j =>{
return arr2.filter(e=>{
return e.id == j.Document_ID__c
})
})
console.log(newObj)
CodePudding user response:
const finalArray = arr2.filter((a) => arr1.some((b) => a.id == b.Document_ID__c));
console.log(finalArray);
CodePudding user response:
You can use Array.some() method to tests whether at least one element in the array passes the test implemented by the provided function.
Working Demo :
const arr1 = [{ Id: "a153O000001mjD1QAI", Document_ID__c: 176767 }];
const arr2 = [{
classification: "Scientific Response",
core_san_localization_translation: ["No"],
core_san_verbal_only: "false",
id: "176767",
major_version_number: "1",
minor_version_number: "0",
name: "Advance Search Doc_General Information",
response_type: ["General"],
size: 70953,
subtype: "Local",
title: "Advance Search Doc_General Information",
type: "Medical Information",
}, {
classification: "Scientific Response",
core_san_localization_translation: ["No"],
core_san_verbal_only: "false",
id: "127462",
major_version_number: "1",
minor_version_number: "0",
name: "APPROVAL verdict APPROVED FOR USE",
response_type: ["Administration"],
size: 1362339,
subtype: "Local",
title: "ahsdjhsahdasld",
type: "Medical Information",
}, {
classification: "Scientific Response",
core_san_localization_translation: ["No"],
core_san_verbal_only: "false",
id: "127461",
major_version_number: "2",
minor_version_number: "0",
name: "APPROVAL verdict APPROVED ,MINOR CHANGES",
response_type: ["Administration"],
size: 1362339,
subtype: "Local",
title: "ahsdjhsahdasld",
type: "Medical Information",
}, {
classification: "Scientific Response",
core_san_answer: "Dosage",
core_san_localization_translation: ["No"],
core_san_verbal_only: "false",
id: "176776",
major_version_number: "1",
minor_version_number: "0",
name: "General Info Test 2",
response_type: ["Pharmacokinetics and Pharmacodynamics"],
size: 25432,
subtype: "Local",
title: "General Info Test 2",
type: "Medical Information",
}];
const result = arr2.filter((a) => arr1.some((b) => a.id.toString() === b.Document_ID__c.toString()));
console.log(result);
CodePudding user response:
Just using the Array iteration methods built into JS is fine for this:
var result = arr2.filter(function(o1){
// filter out items in result2
return arr1.some(function(o2){
return o1['id'] == o2['Document_ID__c'];
});
});